我希望edit.php页面不会给我任何错误
大家好,我发现了一个很棒的CRUD文件,可以用php更新到sql数据库。每件事都很好,但非常恼人的是index.php页面上有一个链接到edit.php?post_id=的按钮,这是正确的,但是任何我喜欢测试页面的方式,因为我知道会有一个无聊的、不合逻辑的人对他们说,如果我这样做会发生什么,等等。。我没有把edit.php?post_id=和mysqli数据库中的一篇文章的id放在url中,而是把它放在url中,就像edit.php一样简单,它说了这个错误警告:mysqli_fetch_array()期望参数1是mysqli_result,布尔值在等中给出。。在第43行,基本上我只想在有人直接键入时修复这个edit.php页面,我的意思是,我不希望在有人键入www.example.com/edit.php时显示错误,因为www.example.com/edit.php?post_id=没有显示错误 这就是我的意思 视觉上 代码呢我希望edit.php页面不会给我任何错误,php,mysqli,Php,Mysqli,大家好,我发现了一个很棒的CRUD文件,可以用php更新到sql数据库。每件事都很好,但非常恼人的是index.php页面上有一个链接到edit.php?post_id=的按钮,这是正确的,但是任何我喜欢测试页面的方式,因为我知道会有一个无聊的、不合逻辑的人对他们说,如果我这样做会发生什么,等等。。我没有把edit.php?post_id=和mysqli数据库中的一篇文章的id放在url中,而是把它放在url中,就像edit.php一样简单,它说了这个错误警告:mysqli_fetch_arra
<?php
include("/fake_path/instructions/php/session_and_connection.php");
if(isset($_POST['update']))
{
$post_id = mysqli_real_escape_string($connect, $_POST['post_id']);
$user_id = $row['user_id'];
$topic_id = mysqli_real_escape_string($connect, $_POST['topic_id']);
$post_title = mysqli_real_escape_string($connect, $_POST['post_title']);
$post_content = mysqli_real_escape_string($connect, $_POST['post_content']);
// checking empty fields
if(empty($post_title) || empty($post_content)) {
if(empty($post_title)) {
echo "<font color='red'>post_title field is empty.</font><br/>";
}
if(empty($post_content)) {
echo "<font color='red'>post_content field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($connect, "UPDATE posts SET user_id='$user_id',topic_id='$topic_id',post_title='$post_title',post_content='$post_content' WHERE post_id=$post_id");
//redirectig to the display page. In our case, it is index.php
header("Location: index.php");
}
}
?>
<?php
//getting post_id from url
$post_id = $_GET['post_id'];
//selecting data associated with this particular post_id
$result = mysqli_query($connect, "SELECT * FROM posts WHERE post_id=$post_id");
while($res = mysqli_fetch_array($result))
{
$user_id = $res['user_id'];
$topic_id = $res['topic_id'];
$post_title = $res['post_title'];
$post_content = $res['post_content'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<a href="index.php">Home</a>
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>post_title</td>
<td><input type="text" name="post_title" value="<?php echo $post_title;?>"></td>
</tr>
<tr>
<td>post_content</td>
<td><input type="text" name="post_content" value="<?php echo $post_content;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="post_id" value=<?php echo $_GET['post_id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
编辑数据
职位
>
如果您没有post\u id,为什么不将db查询更新为:
SELECT*FROM posts\u id=$post\u id
到SELECT*FROM posts
并删除第36行和第37行,即
//getting post_id from url
$post_id = $_GET['post_id'];
1st:在
header()函数之后使用exit()
header("Location: index.php"); exit();
2nd:当get参数为空时,不应运行查询,因此请使用isset
和!empty()
如下所示
if(!empty($_GET['post_id '])) {
//getting post_id from url
$post_id = $_GET['post_id'];
.......
</html>
}
4th:尝试使用prepared语句或pdo
进行avaoid sql注入
5th:无需对单行记录集执行while循环。当$post\u id
为空时,不要调用SQL?您不需要两者,!empty()
就是您所需要的;-)我按照你的建议做了,但是现在我犯了和以前一样的错误。我按照你的建议做了。但它给了我奇怪的结果,换句话说,当我直接进入页面时,它有一个来自数据库的随机信息,即使我试图编辑其他信息,这些信息也保持不变。是的,它没有按我希望的那样工作。我不知道你为什么要删除post\u id
。对于正常的crud操作,需要从数据库中识别一条记录,从而使我们能够在以后更新该记录。
<form name="form1" method="post" action="edit.php<?php if(isset($_GET['post_id'])){ echo '?post_id='.$_GET['post_id']; ?>">