Can a text link pass php variables?

Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Can a text link pass php variables?

 
0
  #1
Jan 9th, 2007
Hey, say I have a table that spits out the information from a mysql table. Say it gives you the names of users, and certain information about each user, say address, phone number, whatever. Doesn't matter.

What I want to do is make each of the persons' name a hyperlink that you can click on to edit their information. When you click on their name, it would pull up a form with their current information already filled out in it. Then, you would be free to edit any of that information, and submit it to update the database.

I'm sure I've seen it done before, but how do I pass a record id onto that form so it knows which record to pull the information about?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 44
Reputation: MCP is an unknown quantity at this point 
Solved Threads: 3
MCP MCP is offline Offline
Light Poster

Re: Can a text link pass php variables?

 
0
  #2
Jan 9th, 2007
Just build each link like:

<a href='editform.php?id=53'>John Doe</a>
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Can a text link pass php variables?

 
0
  #3
Jan 10th, 2007
Originally Posted by MCP View Post
Just build each link like:

<a href='editform.php?id=53'>John Doe</a>
Dude, it's that easy? Thanks! Oh, then how do I call it on the edit page, once I'm there? I need to some how grab that id number on the edit page to fill it with all the info.
Last edited by nathanpacker; Jan 10th, 2007 at 12:16 am.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Can a text link pass php variables?

 
0
  #4
Jan 10th, 2007
Hey, never mind on how to call the variable. I just realized that it is a variable. I can just use it like any other variable! Man, you saved my life. This is great. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 44
Reputation: MCP is an unknown quantity at this point 
Solved Threads: 3
MCP MCP is offline Offline
Light Poster

Re: Can a text link pass php variables?

 
0
  #5
Jan 10th, 2007
No problem! FYI, you can pass multiple variables by doing:

whatever.php?id=5&variable2=whatever&xyz=aaa
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Can a text link pass php variables?

 
0
  #6
Jan 10th, 2007
Originally Posted by MCP View Post
No problem! FYI, you can pass multiple variables by doing:

whatever.php?id=5&variable2=whatever&xyz=aaa
Sweet, thanks, I was just wondering that.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 60
Reputation: cpickering is an unknown quantity at this point 
Solved Threads: 1
cpickering's Avatar
cpickering cpickering is offline Offline
/dev/null

Re: Can a text link pass php variables?

 
0
  #7
Jan 10th, 2007
If i was doing this, I would use a form.

Passing variable's to the URL is problematic and open to abuse. By changing the URL at the top, people would be able to see others information.
You can protect this obviously, by employing sessions, and when they log in, their session ID is inserted into the db and tied to their account. You would have to code the edit page to first check the session, then run a mysql statement to confirm that the session ID ties to that account, if it does, render the rest of the page. If it doesn't redirect them to the login page again and kill their session using session_destroy()

Form:
By using a form, you've locked down the ability to change the ID of the page that people will be editing. Using the following code would be a very simple implementation of this.

[php]
<form action="edituser.php" method="post" enctype="multipart/form-data" name="edit" id="form">
<input name="id" type="hidden" value="<?php echo "$uID"; ?>">
<input type="submit" value="Edit" name="submit">
[/php] You would have to deploy an SQL statement at the top of this page to get the $uID, again, I'd use sessions and insert the sessionID into the user table at point of login.

Your edit page would then use the following code to get the information

[php]

<?
//insert session checker here and use header to redirect

// Get userID
$user = $_POST['uID'];

// get user detals mysql here
// select user,name from table where user = $user
?>

[/php]
HTH
Last edited by cpickering; Jan 10th, 2007 at 10:03 am.
Carl Pickering
Line3 Internet

I speak 1011 languages
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: Can a text link pass php variables?

 
0
  #8
Jan 10th, 2007
Originally Posted by nathanpacker View Post
[...] I just realized that it is a variable. I can just use it like any other variable![...]
Note that register_globals can be turned off, and is off by default in later PHP versions as it's considered a bad practice. Use $_GET superglobal instead.

Also don't forget to validate the user input. In this case id is integer value so the correct access should be:
[php]
$id=(int)$_GET['id'];
[/php]

If it's not numerical, use mysql_real_escape_string() before putting them into your queries.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 8
Reputation: loopylouis is an unknown quantity at this point 
Solved Threads: 0
loopylouis loopylouis is offline Offline
Newbie Poster

Re: Can a text link pass php variables?

 
0
  #9
Jan 10th, 2007
This is something that will pull variables from the database and link the username to a file called "edit.php"

[php]echo "
<Table cellspacing=2><tr><td width=30><b>Line number</b></td><td width=180><center><b>ID</b></center></td><td width=80><b>Username</b></td></tr>" ;

//select a table from the database order by id in descending order

$var1 = Mysql_query("SELECT * FROM `table` ORDER BY id DESC");

//Fill table with variables

while ($var2 = Mysql_fetch_array($var1))
{
$num++ ;
echo "<tr><td width=30><b>$num:</b></td><td width=180><center>$var2[id]</center></td><td width=50> <a href=edit.php?id=$var2[id]>$var2[username]</a></td></tr>";

}
echo "</table>";[/php]
"edit.php" should look something along the lines of this:

[php]
if (!$id){
//no id present
echo"there was no id to edit";
}else{
/*put all your edit script here editting via $id
for example the queries should run:
mysql_query("update `field` set 'feild'='$formvariable' where 'id'='$id' ");
*/
}
[/php]
Hope this helps you out.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Can a text link pass php variables?

 
0
  #10
Jan 11th, 2007
This is all really great, and I would just use a form, but that would get a little bit out of hand, seeing that I would have ot have so many different forms, because each name in the list would be a link to edit it's account. And I realize that using the link like the first person replied could be insecure, I will have to keep that in mind for future projects, whereas this one does not require a whole lot of security, as it's a small little church thing, and probably won't have more personal info than a phone number stored in it. But I could use cookies and sessions to verify it.

When I get back around to it, I'll try to remember to let you all know what I've decided.
Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC