gpt4 book ai didi

javascript - 关于刷新MySQL上自动提交的浏览器数据

转载 作者:行者123 更新时间:2023-11-29 12:20:14 25 4
gpt4 key购买 nike

这是我的 PHP 代码

<?php
/* Attempt MySQL server connection. Assuming you are running MySQLServer with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "root", "", "login");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$fname = mysqli_real_escape_string($link, $_POST['fname']);
$lname = mysqli_real_escape_string($link, $_POST['lname']);
$address = mysqli_real_escape_string($link, $_POST['address']);
$phone = mysqli_real_escape_string($link, $_POST['phone']);
// attempt insert query execution
$sql = "INSERT INTO persons (firstname, lastname, address, phone) VALUES ('$fname', '$lname', '$address', '$phone')";
if(mysqli_query($link, $sql))
{
echo "Records added successfully.";

}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

此代码运行良好,但刷新表单后,它会自动提交数据库上的旧数据。请帮我解决这个问题。

最佳答案

在插入数据之前,首先检查数据库以查看该行是否已经存在。例如从人员中选择*,其中名字 = $fname AND 姓氏 = $lname AND 地址 = $address AND 电话 = $phone如果返回一行,则不应再次添加它。

关于javascript - 关于刷新MySQL上自动提交的浏览器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140491/

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