mysqli_num_rows doesn't work correctly

2020-05-01 08:31发布

I have an admin panel in my website in which the admin creates new pages. he provides the page name, and then the spaces or other characters gets removed by PHP code and is declared to a variable called $new_p_id. here's the mysql table example: mysql table

The php code checks if the page id exists, and then if it exists, the PHP code returns error. The problem is that even when I type "home" or "about" in the form, the mysqli_num_rows return 0. I don't know what's wrong. I've tried mysqli_error($con) but it doesn't return any errors. here's the PHP code:

<?php
if(isset($_POST['pagesubmitted'])){
if($_SESSION['a_role']!="administrator"){die("Please log in");}
$new_p_name=preg_replace("/[^A-Za-z0-9 ]/", '', $_POST['new-page-name']);
$new_p_id=strtolower($new_p_name);
$new_p_id=str_replace(" ", "", $new_p_id);
if(empty($new_p_id)){$errorexists=true;echo "<p class=\"red\">Page name cannot be empty!</p>";}
$new_p_url=$new_p_id;

if ($stmte = mysqli_prepare($con, "SELECT p_id FROM site_pages where p_id=?")) {
    mysqli_stmt_bind_param($stmte,"s", $new_p_id);
    mysqli_stmt_execute($stmte);
if(mysqli_stmt_num_rows($stmte)!=0){
$errorexists=true;echo "<p class=\"red\">Page name already exists!</p>";}
mysqli_stmt_close($stmte);
    }

$new_p_location=$_POST['new-page-location'];
$new_p_content=$_POST['new-page-contents'];
if($errorexists){echo "error!";}
if(!$errorexists){
if ($stmt = $con->prepare("INSERT INTO site_pages(p_id,p_name,p_url,p_location,p_content)VALUES(?,?,?,?,?)")){
            $stmt->bind_param('sssss',$new_p_id,$new_p_name,$new_p_url,$new_p_location,$new_p_content);     
            $stmt->execute();

            $stmt->close(); ?>
            <script>alert("Saved. reload the page to see it in header or sidebar.");</script>
   <?php }
    else {
        printf("Prep statment failed: %s\n", $mysqli->error);
    } 
}
}
    ?>

here's the html form code:

<h1>Create a new page</h1>
<form method="post" action="<?php echo DOMAIN ; ?>/enterprise/?edit=page">
<label for="new-page-name">Page name: </label><input type="text" name="new-page-name" id="new-page-name" value="" maxlength="20">
<br />
<label for="new-page-location">Location: </label>
<select name="new-page-location" id="new-page-location">
<option value="header">Header</option>
<option value="footer">Footer</option>
<option value="header,footer">Header and Footer</option>
<option value="sidebar">Sidebar</option>
<option value="none" selected>None</option>
</select><br />
<label for="new-page-contents">Content:</label><textarea name="new-page-contents" id="new-page-contents"></textarea>
<input type="submit" name="pagesubmitted" class="button" value="Save"/>
</form>

can somebody please explain what's wrong? why does the mysqli_num_rows return 0 even if the page id exists?

标签: php mysql
1条回答
何必那么认真
2楼-- · 2020-05-01 08:32

The use of mysqli_stmt_num_rows() depends on whether or not you used mysqli_stmt_store_result() to buffer the entire result set in the statement handle.

http://php.net/manual/ro/mysqli-stmt.num-rows.php

Add this after execute

mysqli_stmt_store_result($stmte);
查看更多
登录 后发表回答