简体   繁体   中英

Can't update mySql database

Please help me, I'm new to mySql. I'm trying to update the database, 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:您没有获得/设置$name$lastname$email$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM