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

标签 javascript php html mysql

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/

相关文章:

javascript - iOS safari 浏览器中录音的音频质量很差

php - base 64编码表单文件

PHP Iconv 添加额外字符

php - Ubuntu 和 Macports 版本的 PHP 之间奇怪的性能差异

html - 使用 css 和两个图像的灰度悬停

html - 如何让 overflow-x 滚动条出现在位置为 : absolute? 的 <div> 内的 <ul> 上

javascript - 使用 google-passport-oauth 登录后 req.user 未定义

javascript - Angular 4 - 检测到循环依赖。两个组件导入了另一个组件的 .ts 文件

php - 从 PHP 使用 linux 'dialog' 命令

html - 如何从 Firefox 中的选择元素中删除箭头