无法用php更新mysql数据库吗?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法用php更新mysql数据库吗?相关的知识,希望对你有一定的参考价值。
我正在尝试更新数据库中的日期,但是由于某种原因它无法工作,这是代码。
这是我的php页面,我在其中放置代码以显示数据,并且有一个EDIT按钮可以将我发送到page_edit,我可以在其中键入另一个名称并更新它。我会选择一个随机行并在其中编辑名称。
主页(main.php)
<?php
if ($mysqli->connect_error) {die("Connection failed: " . $mysqli->connect_error);}
$sql = "SELECT * FROM users ORDER BY name";
$result = $mysqli->query($sql);
while ($rows = mysqli_fetch_array($result)) {
echo '
<div class="admin-content-con">
<header class="clearfix">
</header>
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Link</th>
<th>Actions</th>
</tr>
</thead>
<tbody>';
echo "<tr>";
echo "<td>" .$rows ['id']."</td>";
echo "<td>" .$rows ['name']."</td>";
echo "<td>" .$rows ['link']."</td>";
echo "<td>
<a href='page_edit.php?id=".$rows["id"]."'class='btn btn-xs btn-warning ' role='button'>edit</a>
<a href='delete.php?id=".$rows["id"]."'class='btn btn-xs btn-danger' role='button' onclick='return checkDelete()'>del</a></td>";
echo "</tr>";
echo '</tbody>
</table>
</div>';} ?>
页面编辑:(page_edit.php)。
<div class="settings-row">
<h3>Name</h3>
<p>This is permanently show on the left corner of navigation</p>
<form action="name.php" method="post" >
<div class="form-group">
<input type="text" class="form-control" name="name">
<input type="submit" value="Submit" name="submit">
</div>
</form>
</div>
更新数据库(name.php)的PHP后端代码。
<?php
include '*';
$dbname = "*";
$conn = mysqli_connect("localhost", "root", "root", $dbname);
if (!$conn) {die("Connection failed: " . mysqli_connect_error());}
$sql ="UPDATE users SET name='".$_POST['name']."' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: main.php');
exit;
} else {
echo "Error update record";
}
?>
答案
您需要将id
传递到编辑页面。然后,您需要在更新页面上输入id
。
<div class="settings-row">
<h3>Name</h3>
<p>This is permanently show on the left corner of navigation</p>
<form action="name.php" method="post" >
<div class="form-group">
<input type="text" class="form-control" name="name">
<input type="submit" value="Submit" name="submit">
<input type="hidden" value="<?php echo htmlspecialchars($_GET['id']);?>" name="id">
</div>
</form>
</div>
然后
<?php
include '*';
$dbname = "*";
$conn = mysqli_connect("localhost", "root", "root", $dbname);
if (!$conn) {die("Connection failed: " . mysqli_connect_error());}
$id = $_POST['id'];
$sql ="UPDATE users SET name='".$_POST['name']."' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: main.php');
exit;
} else {
echo "Error update record";
}
?>
请参见How can I prevent SQL injection in PHP?,了解如何编写SQL。
大约,
$sql ="UPDATE users SET name=? WHERE id=?";
然后准备,绑定和执行。
另外为PHP和mysqli
驱动程序使用错误报告。您的$id
应该已报告为未定义变量。
另一答案
page_edit.php应该看起来像下面-
//Add hidden input type in form for get value of id
<div class="settings-row">
<h3>Name</h3>
<p>This is permanently show on the left corner of navigation</p>
<form action="name.php" method="post" >
<input type="hidden" class="form-control" name="id" value="<?php echo $_GET['id']; ?>">
<div class="form-group">
<input type="text" class="form-control" name="name">
<input type="submit" value="Submit" name="submit">
</div>
</form>
</div>
而且page.php应该是这样的
<?php
include '*';
$dbname = "*";
$conn = mysqli_connect("localhost", "root", "root", $dbname);
if (!$conn) {die("Connection failed: " . mysqli_connect_error());}
$id = $_POST['id'];
$sql ="UPDATE users SET name='".$_POST['name']."' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: main.php');
exit;
} else {
echo "Error update record";
}
?>
以上是关于无法用php更新mysql数据库吗?的主要内容,如果未能解决你的问题,请参考以下文章