Jump to content

[SOLVED] Using $id to call up record


bano6010

Recommended Posts

Hello everyone,

 

I am trying to edit records in my database through php. I am currently using MySQL version 5.0.67. On one page I have all of the records I want to be able to edit properly listing. I made a link to my edit page using the ID for the record in the url. The link looks like this:

 

<a href=edit.php?id=<? echo $id; ?>

 

Where $id is set to: $id = $row[id];

 

On the edit page, this is my code:

 

<?

$query = "SELECT * FROM `officers` WHERE id='$id'";

$result = mysql_query($query) or die("No Record Found!");

 

while ($row = mysql_fetch_array($result))

{

$type=$row['type'];

$firstname=$row['firstname'];

$lastname=$row['lastname'];

}

 

?>

 

<form action="change.php" method="post">

 

<table border="1" bordercolor=FFFFFF cellpadding="3">

 

<tr>

<td>Officer:</td>

<td><? echo "$type"; ?></td>

</tr>

 

<tr>

<td>First Name:</td>

<td bordercolor=EEEEEE><input type="text" value="<? echo "$firstname"; ?>" name="firstname" /></td>

</tr>

<tr>

<td>Last Name:</td>

<td bordercolor=EEEEEE><input type="text" value="<? echo "$lastname"; ?>" name="firstname" /></td>

</tr>

<tr>

<td> </td>

<td><input type="submit" value="Save Changes" /></td>

</tr>

 

</table>

</form>

 

After clicking the link it brings me to the edit page but the form is empty. I want the form to show the record for the ID that is used in the link on the first page. I think I need to define $id somewhere on the edit page but am unsure how or where to.

 

Sorry my code is not in a code block, I do not know how to do that.

 

Any help would be greatly appreciated.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/143788-solved-using-id-to-call-up-record/
Share on other sites

1) Never use short tags to start PHP, always use <?php.

2) You need to read up on the GET method.

3) You need to grab the value from the URL before you use it in your query:

 

$id = $_GET['id'];
$query = "SELECT * FROM `officers` WHERE id='$id'";
$result = mysql_query($query) or die("No Record Found!");

 

4) Use


tags next time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.