我的网站上有一个管理面板,管理员在其中创建新页面。他提供了页面名称,然后PHP代码删除了空格或其他字符,并将其声明为名为$ new_p_id的变量。 这里是mysql表示例:
php代码检查页面ID是否存在,然后如果存在,则PHP代码返回错误。 问题是,即使我输入" home"或"关于"在形式上,mysqli_num_rows返回0.我不知道什么是错的。我已经尝试了mysqli_error($ con),但它没有返回任何错误。这是PHP代码:
<?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表单代码:
<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?
答案 0 :(得分:3)
mysqli_stmt_num_rows()的使用取决于您是否使用过 mysqli_stmt_store_result()缓冲整个结果集 陈述句柄。
http://php.net/manual/ro/mysqli-stmt.num-rows.php
执行后添加
mysqli_stmt_store_result($stmte);