问题描述
我的网站上有一个管理面板,管理员可以在其中创建新页面.他提供了页面名称,然后空格或其他字符被PHP代码删除,并声明为名为$ new_p_id的变量.这是mysql表示例:
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:
php代码检查页面ID是否存在,如果存在,则PHP代码返回错误.问题是,即使我在表单中键入"home"或"about",mysqli_num_rows也会返回0.我不知道这是怎么回事.我已经尝试了mysqli_error($ con),但是它没有返回任何错误.这是PHP代码:
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);
}
}
}
?>
这是html表单代码:
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>
有人可以解释怎么了吗?为什么即使页面ID存在,mysqli_num_rows仍返回0?
can somebody please explain what's wrong? why does the mysqli_num_rows return 0 even if the page id exists?
推荐答案
http://php.net/manual/ro/mysqli- stmt.num-rows.php
在执行后添加它
mysqli_stmt_store_result($stmte);
这篇关于mysqli_num_rows无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!