我想在每次在數據庫表中設置新值或相同值時覆蓋數據。我已經閱讀了很多文章,但是我還沒有退出對UPDATE命令的理解。使用php覆蓋數據庫中的每條記錄使用php
這只是我項目中的示例測試的基礎測試文件。
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>
誰能幫助?
也不要忘記WHERE命令。否則ALL Collumns會更新爲該名稱:) $ insertsql =「UPDATE user SET name ='$ name',lastname ='$ lastname'WHERE id ='$ id'」; –