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 代码:
<?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?