INSERT查询不插入数据,但执行状态INSERT成功的回显

时间:2016-03-26 23:33:43

标签: php mysql

我有以下代码段:

$get_data = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$username'");
    $row2 = mysqli_fetch_assoc ($get_data);
            $age = $row2['age'];

$get_data2 = mysqli_query ($connect, "SELECT * FROM user_bio WHERE username = '$username'");
    $row3 = mysqli_fetch_assoc ($get_data2);
            $bio = $row3['about_me'];   
            $studying = $row3['studying'];
            $lang = $row3['language'];
            $rel_status = $row3['relationship_status'];

$about_me = htmlentities(trim(strip_tags(@$_POST['biotextarea'])));
        $new_studying = htmlentities(trim(strip_tags(@$_POST['studying'])));
        $new_lang = htmlentities(trim(strip_tags(@$_POST['lang'])));
        $new_rel = htmlentities(strip_tags(@$_POST['rel']));

        if(isset($_POST['update_data'])){
            // need to check if the username has data already in the db, if so, then we update the data, otherwise we insert data.
                $get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$username'");
                $row_returned = mysqli_num_rows($get_bio);

                if ($row_returned == 0){
                    $insert_query = mysqli_query ($connect, "INSERT INTO user_bio (id, age, studying, language, relationship_status, username, about_me) 
                                                    VALUES ('', '$age','$new_studying','$new_lang','$new_rel', '$username', '$about_me'");                                                          
                        echo "  <div class='details_updated'>
                                    <p> Details added successfully! </p>
                                </div>";
                } else {    
                    $update_details_query = mysqli_query ($connect, "UPDATE user_bio SET studying ='$new_studying', language ='$new_lang', 
                                                                relationship_status = '$new_rel', about_me = '$about_me' WHERE username ='$username'");
                        echo "  <div class='details_updated'>
                                    <p> Details updated successfully! </p>
                                </div>";
                }
            }

我们的想法是,当用户上传bio时,INSERTUPDATE的查询将基于username是否已存在行。例如,我为用户名Conor手动插入了一行:

id: 1
age: 30
studying: Business
language: English
relationship_status: Single
username: conor
about_me: This is conor's bio.

如果我以Conor身份登录,并转到account_settings_bio.php,其中conor可以编辑他们的bio,UPDATE查询(else语句)将会运行,因为db中有一行用于username { {1}}。但是,如果我以conor身份登录并转到Alice,则应运行INSERT查询,因为account_settings_bio.php表中不存在alice的数据。

现在,问题在于,当我以Alice身份登录时,我转到user_bio,我添加了所有详细信息并单击“提交”,它回显了当应该发生的消息时account_settings_bio.php查询成功,即INSERT - 但数据库中没有添加新行?

1 个答案:

答案 0 :(得分:0)

我没有看到导致mysqli在发生SQL错误时抛出异常的设置。

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

如果没有设置,那么mysqli不会抛出错误,代码必须检查SQL语句是否成功。

之后:

  $insert_query = mysqli_query ($connect, "INSERT INTO 

您需要检查$insert_query是否计算为TRUE,以了解语句执行是否成功。

否则,代码将它的小拇指放在嘴角,Dr.Evil风格,并说“我只是假设一切都去计划。什么?”