【发布时间】:2016-04-24 09:08:06
【问题描述】:
我在尝试使用表单更新数据库时遇到问题。
所以,我的问题是当我点击更新时没有任何反应。数据库没有更新。
另一个问题是当我在表单和 SQL 查询中使用 POST 时。它不会将信息拉到编辑页面,它是空白的。
如果这很难阅读等,我很抱歉,但这是我第一次发帖。另外,我知道我的 GET/POST 查询中存在安全漏洞,我只是想在开始使用准备好的语句或它们被调用的任何东西之前让事情正常工作。
仅供参考,如果我回显查询并定义 if/else 语句,我可以看到它不起作用,但我不知道为什么。我已经花了 3 天时间,并使用我在互联网上找到的示例多次更改代码。
索引.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");
}
?>
【问题讨论】:
-
尝试在 UPDATE 查询中对 $title 和 $content 使用串联(用 . 连接字符串)。
-
对不起,我的链接是什么意思?
-
让某些东西工作并然后修复安全漏洞没有任何价值。无论如何,您必须学会在某个时候使用准备好的语句才能修复它。为什么不先节省时间并学习安全方法,而不是学习如何制作不安全的代码,然后再学习如何保护它?
-
James,如果你是我 3 年前开始编写平面 php 的地方,那么你现在能为自己做的最好的事情就是开始研究 CMS,如果你想更接近代码然后是 CMF。我个人喜欢贴近代码,所以我使用 Symfony
标签: php html mysql sql-update