【问题标题】:Extra blank records being added to Mysql database on insert插入时将额外的空白记录添加到 Mysql 数据库
【发布时间】:2013-12-30 11:57:24
【问题描述】:

提交时,记录被插入到 MySQL 数据库中,但也插入了 4 个额外的空白行。 phpmyadmin 中的空白行只有“id”,但表中的其他列是空的/空白的。我在下面附上我的代码。感谢您的帮助。

<!DOCTYPE html>
<head>
    <title>Title</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
    <style type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Page Title</title>
        <style type="text/css" charset="utf-8"/>
        *{background:#eee;padding:2px;box-sizing:border-box;
          -moz-box-sizing:border-box; /* Firefox */}
    </style>
</head>
<body>

    <?php
    $dbhost = 'host';
    $dbuser = 'user';
    $dbpass = 'passw';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    //test if the connection is established successfully then it will proceed in next process else it will throw an error message
    if (!$conn) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('cato'); //we specify here the Database name we are using


    if (isset($_POST['submit'])) {
        $i = 0;
        foreach ($_POST as $val) {
            $business_name = trim(mysql_real_escape_string($_POST["business_name"][$i]));
            $business_type = trim(mysql_real_escape_string($_POST["business_type"][$i]));
            $country = trim(mysql_real_escape_string($_POST["country"][$i]));
            $province = trim(mysql_real_escape_string($_POST["province"][$i]));
            $address = trim(mysql_real_escape_string($_POST["address"][$i]));
            $postcode = trim(mysql_real_escape_string($_POST["postcode"][$i]));
            $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"][$i]));
            $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"][$i]));
            $category = trim(mysql_real_escape_string($_POST["category"][$i]));
            $email = trim(mysql_real_escape_string($_POST["email"][$i]));
            $i++;
            mysql_query("SET NAMES 'utf8'");
            mysql_query("INSERT INTO company_details (company_id, business_name, business_type, country, province, address, postcode, telephone1, telephone2, category, email) VALUES ('','$business_name', '$business_type', '$country', '$province', '$address', '$postcode', '$telephone1', '$telephone2', '$category', '$email')");
        }
    }
    ?>
    <script type="text/javascript">
        alert("Successs!! records have been added to database");
        window.location = "index.php?db=cato";
    </script>

    <?php
    //close of connection
    mysql_close($conn);
    ?>

</body>
</html>

【问题讨论】:

  • 你为什么要使用:foreach ($_POST as $val) ??
  • 因为在我的表单中有许多条目共享相同的名称。例如:";
  • 只需删除那个 foreach 循环 ..
  • 我做了,但没有插入任何东西。
  • ok.. 不同的数组 business_name、business_type、country 等是否具有相同数量的值?如果是这样,请尝试将 foreach 替换为 foreach ($_POST["business_name"] as $val) ..

标签: php mysql database insert rows


【解决方案1】:

使用以下假设 HTML 字段名称与 POST 中的相同

if (isset($_POST['submit'])) {
        $business_name = trim(mysql_real_escape_string($_POST["business_name"]));
        $business_type = trim(mysql_real_escape_string($_POST["business_type"]));
        $country = trim(mysql_real_escape_string($_POST["country"]));
        $province = trim(mysql_real_escape_string($_POST["province"]));
        $address = trim(mysql_real_escape_string($_POST["address"]));
        $postcode = trim(mysql_real_escape_string($_POST["postcode"]));
        $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"]));
        $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"]));
        $category = trim(mysql_real_escape_string($_POST["category"]));
        $email = trim(mysql_real_escape_string($_POST["email"]));
}

然后使用插入查询。换句话说,取出 forach $_POST

【讨论】:

    【解决方案2】:

    你的问题是在

    foreach ($_POST as $val) 
    

    它根本不存在(您正在尝试对每个字段执行查询)

    并使用

    ($_POST["business_name"]) instead of ($_POST["business_name"][$i])
    

    【讨论】:

    • 对不起,如果我的问题不太清楚,我在上面发布的每个字段都有不同的值。比如 business_name 会有多个值。
    • 而不是在 for 循环 for($i=0;i
    【解决方案3】:

    从代码中删除 for 循环,

      if (isset($_POST['submit'])) {
    
                $business_name = trim(mysql_real_escape_string($_POST["business_name"][$i]));
                $business_type = trim(mysql_real_escape_string($_POST["business_type"][$i]));
                $country = trim(mysql_real_escape_string($_POST["country"][$i]));
                $province = trim(mysql_real_escape_string($_POST["province"][$i]));
                $address = trim(mysql_real_escape_string($_POST["address"][$i]));
                $postcode = trim(mysql_real_escape_string($_POST["postcode"][$i]));
                $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"][$i]));
                $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"][$i]));
                $category = trim(mysql_real_escape_string($_POST["category"][$i]));
                $email = trim(mysql_real_escape_string($_POST["email"][$i]));
                mysql_query("SET NAMES 'utf8'");
                mysql_query("INSERT INTO company_details (company_id, business_name, business_type, country, province, address, postcode, telephone1, telephone2, category, email) VALUES ('','$business_name', '$business_type', '$country', '$province', '$address', '$postcode', '$telephone1', '$telephone2', '$category', '$email')");
          }
        ?>
    

    【讨论】:

    • 我只是这样做了,没有任何东西被插入到数据库中。
    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多