gpt4 book ai didi

php - 使用php覆盖数据库中的每条记录

转载 作者:行者123 更新时间:2023-11-28 23:51:52 24 4
gpt4 key购买 nike

每次在数据库表中设置新值或相同值时,我都想覆盖数据。我已经阅读了很多文章,但我还没有完全理解 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>

有人能帮忙吗?

最佳答案

更新您的代码:

$insertsql = "UPDATE user SET name ='$name' lastname='$lastname' ";

$insertsql = "UPDATE user SET name ='$name', lastname='$lastname' ";

如果要更新多列,请使用逗号(,) 符号

关于php - 使用php覆盖数据库中的每条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32476621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com