Jump to content

[SOLVED] Stuck updating records by 'id'


imacdoug

Recommended Posts

Hi, I am a total newbie to php and am totally stuck with this problem. I am trying to constuct a very basic page which will allow users to update a record in a MySQL database the fields are:

 

id :: name :: email ::tel

 

the page (update.php) reads the values from the table "phonebook" and should allow the user to change a value when they press "update". This does not happen and the same result is shown again.. if I modify the UPDATE line in the code to a '1' rather than the ['id'] then the code works for that record.. what am I doing wrong..

Any help very gratefully received, I am really wanting to start learning but this is seriously annoying me as it should be very simple!

 

<?php
include("connectdb.php");

if($_POST['Submit']){
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];
mysql_query("UPDATE phonebook SET name='$name', email='$email', tel='$tel' WHERE id = '$id'");
header("location:select.php");
exit;
}

$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from phonebook where id='$id'");

$row=mysql_fetch_assoc($result);

mysql_close();
?>

<html>
<body>
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name : 

<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>
<br />
Email : 
<input name="email" type="text" id="email" value="<? echo $row['email']; ?>"/>
<br />
Tel : 
<input name="tel" type="text" id="tel" value="<? echo $row['tel']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form> 
</body>
</html>

 

Best wishes and thanks for any help!

 

Ian.

Link to comment
https://forums.phpfreaks.com/topic/151913-solved-stuck-updating-records-by-id/
Share on other sites

you dont have $_POST['ID'] set anywhere.  there is no ID field in your form so this is going to be empty, therefore your SQL query is failing

 

you may want to do something like this in your form, then it should work

<input type="hidden" name="id" value="<?php echo $row['id']; ?>">

 

also, do this

<?php
$sql = "UPDATE phonebook SET name='$name', email='$email', tel='$tel' WHERE id = '$id'";
mysql_query($sql) or die("Error with $sql: " . mysql_error());
?>

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.