我在尝试使用表单更新数据库时遇到问题。
所以,我的问题是当我单击更新时什么也没发生。数据库不会更新。
另一个问题是当我在表单和SQL查询中使用POST时。它不会将信息拉到编辑页面,而是空白。
很抱歉,这很难读,等等。但这是我第一次发布。另外,我知道我的GET / POST查询中存在安全漏洞,我只是想让事情开始起作用,然后再开始使用准备好的语句或任何被调用的语句。
仅供参考,如果我回显查询并定义一个if / else语句,我可以看到它不起作用,但是我不知道为什么。我花了三天的时间,并使用我在互联网上找到的示例对代码进行了很多次更改。
Index.php
<?php
$servername = "localhost";
$username = "***";
$password = "****";
$dbname = "****";
$link = new mysqli("$servername", "$username", "$password", "$dbname");
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
echo "Connected successfully";
mysqli_select_db($link,"jamesrph_myphp");
$sql = "SELECT * FROM article";
$result = mysqli_query($link,$sql);
$id = 'id';
$title = 'title';
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title>PHP </title>
</head>
<body>
<h1> My title </h1>
<table>
<tr>
<?php
while($row=mysqli_fetch_array($result)){
?>
<td><?php echo $row["title"]; ?> </td>
<td><a href="article_detail.php?id=<?php echo $row["id"]; ?>">Read More</a></td>
<td><a href="edit.php?id=<?php echo $row["id"]; ?>">Edit</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
edit.php
<?php
$link = mysqli_connect("localhost","******","******", "*****");
$query = "SELECT * FROM article WHERE id=".mysqli_real_escape_string($link, $_GET['id'])." LIMIT 1";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
$title = $row['title'];
$content = $row['content'];
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<p> Edit Article </p>
<form method="get" action="processarticle.php">
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>" />
<input id="titlearea" type="text" name="title" value="<?php echo $row["title"]; ?>"/>
<textarea id="contentarea" name="content" rows="10" cols="40"><?php echo $row["content"];?></textarea>
<input type="submit" name="submit" id="update_article"/>
</form>
</body>
</html>
processarticle.php
<<?php
//Database Connection
include 'connection.php';
//Get ID from Database
if(isset($_GET['edit_id'])){
$sql = "SELECT * FROM article WHERE id =" .$_GET['edit_id'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
}
//Update Information
if(isset($_POST['btn-update'])){
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];
$update = "UPDATE article SET title=?, content=? WHERE id=?";
$up = mysqli_query($link, $update);
if($stmt = $mysqli->prepare($update)){
$stmt->bind_param("ssi" ,$title ,$content ,$id);
$stmt->excute();
}
header("location: disp.php");
}
?>
好的,您的edit.php表单具有GET方法,但是您正在processarticle.php中使用POST变量,并且在那里具有GET变量。
可以说表单只能执行GET或POST的一项操作
然后,您在表单中指定的URL将基于表单方法访问GET或POST变量
因此,如果要基于表单更新文章,请先查看id =这应该是$ POST ['id']表单中的隐藏字段,尽管不是隐藏的
$update = "UPDATE article SET title='$title', content='$content' WHERE id=". $_POST['id'];
我看得越多,这将成为3部分迷你系列的更多内容
好的,对于初学者来说,您在processarticle.php上,我将使用准备好的语句来更新http://php.net/manual/en/mysqli.prepare.php
process.php
//Update Information
if(isset($_POST['Update_Article'])){
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];
$SQL = "UPDATE
article
SET title=?, content=?
WHERE id=?";
if ($stmt = $mysqli->prepare($SQL)) {
$stmt->bind_param("sss", $title ,$content ,$id );
$stmt->execute();
}
}
从这里开始http://www.w3schools.com/php/default.asp,从爬行到步行,从诚实到上帝,从上到下依次完成,然后到这里https://symfony.com/,并希望乘坐F1汽车
然后尝试http://www.w3schools.com/bootstrap/default.asp,因为您希望页面看起来很酷
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句