每次在数据库表中设置新值或相同值时,我都想覆盖数据。我已经阅读了很多文章,但我还没有放弃了解UPDATE命令。
这只是我项目中示例测试的基础测试文件。
php
<?php
$servername = "localhost";
$username = "root";
$password = "00000000";
$dbname = "test";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST["name"];
$lastname = $_POST["lastname"];
//echo $name," ",$lastname;
$insertsql = "UPDATE user SET name ='$name' lastname='$lastname' ";
if($conn->query($insertsql) === TRUE){
echo "update ok";
}
else{
echo "Error :" . $insertsql . "<br>" . $conn->error;
}
$conn->close();
?>
HTML
<html>
</body>
<form action="insert.php" method="post">
First name:<br>
<input type="text" value="" name="name">
<br>
Last name:<br>
<input type="text" value="" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
有人可以帮忙吗?
答案 0 :(得分:1)
代码更新:
$insertsql = "UPDATE user SET name ='$name' lastname='$lastname' ";
到
$insertsql = "UPDATE user SET name ='$name', lastname='$lastname' ";
如果您想更新多个列,请使用comma(,) sign
。
答案 1 :(得分:0)
使用它
$insertsql = "UPDATE user SET name ='$name', lastname='$lastname' ";
您查询中的where
子句在哪里。
因为此查询更新了表中的所有记录。