mysqli_stmt_num_rows保持返回0

时间:2018-03-18 20:06:59

标签: php mysql

if (isset($_POST['submit'])) {
    //Naam
    if (isset($_POST['anoniem'])) {
        $nameOK = true;
        $name = "Anoniem";
    }
    else if (empty($_POST['name'])) {
        $nameErr = "Vul uw naam in of vink het vakje 'Anoniem' aan.";
    }
    else {
        $name = $_POST['name'];
        $nameOK = true;
    }

    //Review
    if (empty($_POST['review'])) {
        $reviewErr = "Vul uw review in";
    }
    else if (preg_match("/(kut|fuck|fucking)/i", $_POST['review'])) {
        $reviewErr = "Gelieve niet te vloeken in uw review";
    }
    else {
        $review = $_POST['review'];
        $reviewOK = true;
    }

    //Rating
    if (empty($_POST['rating'])) {
        $ratingErr = "Geef een cijfer";
    }
    else if (!is_numeric($_POST['rating'])) {
        $ratingErr = "Vul een getal in"; 
    }
    else {
        $rating = $_POST['rating'];
        $ratingOK = true;
    }
    //Opslaan naar DB

    if ($nameOK && $reviewOK && $ratingOK) {
        date_default_timezone_set('Europe/Amsterdam');
        $date = date('d-m-Y G:i');
        $Dupe = false;
        $stmt = NULL;

        if ($name != "Anoniem") {
            $stmt = mysqli_prepare($conn, "SELECT * FROM reviews WHERE reviewer_name = ? AND review = ?");
            mysqli_stmt_bind_param($stmt, "ss", $name, $review);

            if (mysqli_stmt_execute($stmt)) {
                mysqli_stmt_store_result($stmt);
                mysqli_stmt_fetch($stmt);
                echo mysqli_stmt_num_rows($stmt);

                if (mysqli_stmt_num_rows($stmt) > 0) {
                    $Dupe = true;
                    $DBnoti = "Dit review bestaat al, Review is niet opgeslagen.";
                }
                mysqli_stmt_close($stmt);
            }
            else {
                echo "Er is een fout opgetreden.";
            }
        }

        if (!$Dupe) {
            $stmt = mysqli_prepare($conn, "INSERT INTO reviews (rating, review, reviewer_name, review_date) VALUE (?,?,?,?)");
            mysqli_stmt_bind_param($stmt, "isss", $rating, $name, $review, $date);

            if (mysqli_stmt_execute($stmt)) {
                $DBnoti = "Review succesvol opgeslagen";
            } 
            else {
                $DBnoti = "Er is een fout opgetreden tijdens het opslaan. Review niet opgeslagen.";
            }
            mysqli_stmt_close($stmt);
        }
    }

每当我尝试检查用户输入是否已插入数据库时​​,mysqli_stmt_num_rows($stmt)一直返回0并且我不知道如何修复它。它在我使用预准备语句之前就已经工作了,但是当我尝试实现准备好的语句来保护数据库时,它就再也不能工作了。
$conn是我与数据库连接的变量名称。 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你可以尝试将评级字符串s更改为 i ,你要求它为数字if (!is_numeric($_POST['rating'])) {,并在你的代码中使用4次字符串..你的代码:

mysqli_stmt_bind_param($stmt, "ssss", $rating, $name, $review, $date);

检查此mysqli_stmt_bind_param