如何避免mysqli :: query():空查询

时间:2015-10-26 08:45:56

标签: php mysqli

他们,

我对PHP很新,并想知道如何保持这个" mysqli :: query():空查询"警告了。问题是,当我在表单中输入评论或评级(而不是两者)时,似乎要么评论'或者评级'查询尝试使用空字符串运行,因此它会返回警告。

如何设置它以便当用户选择不输入评论或评级时,它不会重新发出警告? 我试着这样做:

//Check if comment is entered else set it to an empty string
if (isset($_POST['comments'])) {
    $commentText = $conn->real_escape_string($_POST['comments']);
} else {
    $commentText = "";
}

然后检查它是否为空字符串,如果是,则只发送另一个查询。

//If user enters either rate or comment (or both) send a query to table.
//Else only send the query with the users input which the user has entered.
if (($rate >= 1) && ($rate <= 5)) {
    if ($commentText === "") {
        $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
    }
    else {
        $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
        $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
    }   
}
else {
    $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
}

但它不起作用。

<!DOCTYPE html>
    <?php
        include("dbconnect.php");
        error_reporting(E_ALL);



        if(isset($_GET['id'])) {
            $id = (int)$_GET['id'];

            //Place all data out of the database, with the ID number retrieved out of the url in $result.
            $game = $conn->query("SELECT * FROM beoordeling WHERE id = '" . $id . "'");
        }
    ?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Dusk Drive</title>
    </head>
    <body>

    <?php        
            /*  
             * 
             * @ToDo: Make point system show avarage rating
             * 
             * @ToDo: Create a webmaster page where you can add games to the list
             * and automaticly create game page.
             * 
             */




            //CommentList retrieves all comments with id = x.
            $commentList = $conn->query("SELECT * FROM comments WHERE form_id = '" . $id . "'");  



            //While a row of data exists, put that row in $data as an associative array.
            while($data = $game->fetch_assoc()) {
                //Retrieve the file name from the database and place it in the <embed> tags as src="...".
                echo "<embed width='800' height='512' src='" . $data['file'] . "'; type='application/x-shockwave-flash'></embed><br />";
            }  


        //Echo the form with a text box and a rating box.
        echo '<div id="game_form"><form method="POST">
            <a>Leave a comment</a><br />
          <input type="text" name="comments" />
          <br /><a>Rate this game</a><br />  
        <select name="rategame">
          <option value="">Select...</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
        </select>
            <input type="submit" name="submit" value="submit" />   
        </form></div>';

            //Create a table with all the comments  
            echo "<table>";

            while($cdata = $commentList->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $cdata["comment"] . "</td> <br /><br />";
                echo "</tr>";
            }
            echo "</table>";

        //Submit functionality    
        if (isset($_POST['submit'])) {

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        //Check if comment is entered else set it to an empty string
        if (isset($_POST['comments'])) {
            $commentText = $conn->real_escape_string($_POST['comments']);
        } else {
            $commentText = "";
        }

        $rate = $_POST['rategame'];

        echo $rate;

        //If user enters either rate or comment (or both) send a query to table.
        //Else only send the query with the users input which the user has entered.
        if (($rate >= 1) && ($rate <= 5)) {
            if ($commentText === "") {
                $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
                $sqlcomment = ""; //Initializing to null to avoid error.
            }
            else {
                $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
                $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
            }   
        }
        else {
            if ($commentText !== "") {
            $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
            $sqlrate = ""; //initializing to null to avoid error.
            }
        }

        //Check if query succeeded or return error
        if (($conn->query($sqlcomment) === TRUE) || ($conn->query($sqlrate) === TRUE)) {
            echo "<script type= 'text/javascript'>alert('Thank you! Your comment or vote has been received.');</script>";
        } 
        else {
            echo "<script type= 'text/javascript'>alert('Thank you for your vote and comment!);</script>";
        } 


        //Close connection to free up resources.
        $conn->close();
        }
    ?>      
    </body>
</html>

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

in else [部分也检查$commentText

if ($commentText === "") {
    $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
}

if (($rate >= 1) && ($rate <= 5)) {
    if ($commentText === "") {
        $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
        $sqlcomment = "";//initializing to null to avoid error.
    }
    else {
        $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('".$rate."','".$id."')";
        $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
    }   
}
else {
    if ($commentText !== "") {// this is the condition before insert.
         $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('".$commentText."','".$id."')";
         $sqlrate = "";initializing to null to avoid error.
    }
}

修改

if($sqlcomment) {
     $conn->query($sqlcomment) 
}

if($sqlrate) {
     $conn->query($sqlrate) 
}