gpt4 book ai didi

php - mysqli_num_rows 不能正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:11 24 4
gpt4 key购买 nike

我的网站上有一个管理面板,管理员可以在其中创建新页面。他提供页面名称,然后空格或其他字符被 PHP 代码删除,并声明给一个名为 $new_p_id 的变量。这是 mysql 表示例: mysql table

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?

最佳答案

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

执行后添加

mysqli_stmt_store_result($stmte);

关于php - mysqli_num_rows 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25468207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com