Can't update mySql database
Please help me, I'm new to mySql. I'm trying to update the data开发者_开发技巧base, and it doesn't work. I've checked the code, and it seems to be correct, but nothing gets updated.
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
Thank you
@Tamara: You're not getting / setting $name
, $lastname
, $email
or $id
...
If the id is INT
(and it should be) you should not use single quotes. That's not for INT
fields.
Consider the following code:
// sets the variables
$id = $_GET['id']; // lets assume you get the id with GET
$name = 'Some Name';
$lastname = 'Some Last Name';
$email = 'email@domain.com';
$sql="UPDATE ".$tbl_name." SET name='".$name."', lastname='".$lastname."', email='".$email."' WHERE id=".$id;
精彩评论