gpt4 book ai didi

javascript - PHP - 运行1插入查询同时运行两次记录

转载 作者:行者123 更新时间:2023-11-29 20:39:48 24 4
gpt4 key购买 nike

addcustomer.php

<?php
include_once('functions.php');
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8s" />
<title>Add Customer</title>
<script type="text/javascript">
function CheckFields()
{
var cusname = document.getElementById("cusname").value;
var cusadd = document.getElementById("cusadd").value;

if(cusname == '')
{
alert("Please enter your customer name");
return false;
}

if(cusadd == '')
{
alert("Please enter your customer address");
return false;
}

return true;
}
</script>
</head>

<body onLoad="focus();add.name.focus()">
<?php
if(array_key_exists('submit_check', $_POST))
{
if($_POST['submit'] == 'Add')
{
if(!AddCustomer(trim($_POST['cusname']), trim($_POST['cusgender']), trim($_POST['cusadd'])))
{
echo "Add Customer Error, Please try it again later";
}
else
{
echo "Customer Information had been successfully added into the database";
}
}
}
else
{
AddCustomerForm();
}
?>
</body>
</html>

dbconnect.php

<?php
$host = "localhost";
$user = "root";
$passwd = "";
$dbname = "customertest";
?>

functions.php

<?php
include('dbconnect.php');

function AddCustomerForm()
{
?>
<form id="add" name="add" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<b><u>Customer Details</u></b><br />
<input type="hidden" name="submit_check" value="1" />
Customer Name : <input type="text" id="cusname" name="cusname" maxlength="50" /><br />
Gender : <input type="radio" name="cusgender" value="M" checked />Male
<input type="radio" name="cusgender" value="F" />Female<br /><br />
<b><u>Customer Address</u></b><br />
Address : <input type="text" id="cusadd" name="cusadd" maxlength="100" size="50" /><br /><br />
<input type="submit" name="submit" value="Add" onclick="return CheckFields();" />
<input type="reset" name="reset" value="Reset" />
</form>
<?php
}

function ConnectToDb()
{
global $host, $user, $passwd, $dbname;
@$db = mysql_pconnect($host, $user, $passwd);
if(!$db)
{
echo "Couldn't connect to the database";
exit;
}
mysql_select_db($dbname);
}

function AddCustomer($cusname, $cusgender, $cusadd)
{
ConnectToDb();

$query = "INSERT INTO customer (cusname, cusgender, cusjoindate) VALUES (\"".$cusname."\", '".$cusgender."', NOW())";
$result = mysql_query($query);

if($result)
{
$cusID = mysql_insert_id();

$query = "INSERT INTO customer_address (cusID, cusaddress) VALUES ('".$cusID."', '".$cusadd."')";
$result = mysql_query($query);

return 1;
}
else
{
return 0;
}
}
?>

从上面的代码中,当我填写客户表单并单击“添加”按钮时,我在数据库中获得了双记录,如下图所示:
enter image description here

我实在不明白为什么数据库会同时存储2条记录。
我写错代码了吗?有人可以帮助我吗?

最佳答案

您是否正确分配了主键和外键。表示客户地址表中的 cusID,它引用客户表中的 cusID(主键)。我尝试了与下面相同的操作,效果很好。

更改表 customer_address 添加外键 (cusID) 引用 customertest.customer(cusID) 删除限制更新限制;

关于javascript - PHP - 运行1插入查询同时运行两次记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669501/

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