Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有人能告诉我为什么这一行被两次插入我的数据库吗?_Php_Mysql - Fatal编程技术网

Php 有人能告诉我为什么这一行被两次插入我的数据库吗?

Php 有人能告诉我为什么这一行被两次插入我的数据库吗?,php,mysql,Php,Mysql,当我单击“创建”按钮时,我希望将记录添加到我的类别表中,但是由于某些原因,它被添加了两次-即使我只单击了一次按钮。你知道为什么吗?我看不到还有什么别的地方 if(isset($\u POST['create']){可以从中调用。我整个项目中只有4页 <?php require('dbConnect.php'); //use the variables we created in volleyLogin.php session_start(); $

当我单击“创建”按钮时,我希望将记录添加到我的类别表中,但是由于某些原因,它被添加了两次-即使我只单击了一次按钮。你知道为什么吗?我看不到还有什么别的地方
if(isset($\u POST['create']){
可以从中调用。我整个项目中只有4页

<?php require('dbConnect.php'); 

    //use the variables we created in volleyLogin.php
        session_start();
        $username = $_SESSION['username'];
        $user_id = $_SESSION['user_id'];
        echo "user name is " . $username . "<br>";
        echo "user id is " . $user_id . "<br>"; 

    if (isset($_POST['create'])) {

        $category = ($_POST['category']);
        $name = ($_POST['name']);
        $phonenumber = ($_POST['phonenumber']);
        $address = ($_POST['address']);
        $comment = ($_POST['comment']);

    //check if the category being entered is already there
        $check="SELECT COUNT(*) FROM category WHERE cat_name = '$_POST[category]'";
        $get_value = mysqli_query($con,$check);
    //check the number of values of the category being posted
        $data = mysqli_fetch_array($get_value, MYSQLI_NUM);
    //if the category name already exists in the category table
        if($data[0] >= 1) {
        echo "This Already Exists<br/>";
         }

        else if ($data[0] < 1)
        {
    //if it's not in there, then add the category in the category table.

        $sql = "INSERT INTO category VALUES(NULL, '{$category}', '$user_id')";
        $rs1=mysqli_query($con, $sql); 

        if ($con->query($sql) === TRUE) {
        echo "Yes, it's been added correctly";

        } else {
        echo "Error: " . $sql . "<br>" . $con->error;
        }

        }
    $con->close();
        }



    ?>

        <!doctype html>
        <html>
        <body>
        <h2>Create new Contact</h2>
        <form method="post" action="" name="frmAdd">
        <p><input type="text" name = "category" id = "category" placeholder = "category"></p>
        <p><input type="text" name = "name" id = "name" placeholder = "name"></p>
        <p><input type="text" name = "phonenumber" id = "phonenumber" placeholder = "phone number"></p>
        <p><input type="text" name = "address" id = "address" placeholder = "address"></p>
        <p><input type="text" name = "comment" id = "comment" placeholder = "comment"></p>

        <p><input type="submit" name = "create" id = "create" value = "Create new Contact"></p>
        <a href="exit.php">Exit</a>

        </form>

        </body>
        </html>

您使用两种不同的方法运行了
$sql
查询两次:

$rs1=mysqli_query($con, $sql); 

        if ($con->query($sql) === TRUE) {
这就是为什么你会得到重复的条目


您应该删除
$rs1
,因为它没有被使用,或者在条件上验证它的值,而不是再次运行该函数。

谢谢。我不明白为什么它可以工作,但它确实可以。我现在会在新书《PHP for the Web》中阅读这些部分。显然,我会在9分钟内接受您的答案。没关系。您可能会喜欢只是混淆了两种不同的东西。当你这样做时,请检查:。你的代码非常容易受到攻击。永远不要将输入数据直接放入查询中。永远不要相信它们,它们可能会被黑客攻击。使用
mysqli\u real\u escape\u string
或使用准备好的语句。