Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.