gpt4 book ai didi

php - 当我点击提交按钮时数据没有更新

转载 作者:行者123 更新时间:2023-11-29 16:21:14 25 4
gpt4 key购买 nike

当我单击提交按钮时,它不会更新数据库中的数据。我看过多次去找出问题所在。我认为问题不在于代码。可能是因为 XAMPP。我正在使用 phpstorm,并且我在两个文件中都给出了数据库连接。如果您需要更多信息,请告诉我。谢谢

“这是供应商列表文件”

<table class="table-bordered table-striped table-highlight text-center">
<tr class="bg-dark text-white">
<th>Sid</th>
<th>Name</th>
<th>Contact</th>
<th>Action</th>
</tr>
<?php
$q = "SELECT * FROM supplier";
$result = mysqli_query($db, $q) or die(mysqli_error($db));
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td data-toggle="modal" data-target="#<?php echo $row[0];?>"><?php echo $row[0]; ?></td>
<td data-toggle="modal" data-target="#<?php echo $row[0];?>"><?php echo $row[1]; ?></td>
<td data-toggle="modal" data-target="#<?php echo $row[0];?>"><?php echo $row[3]; ?></td>
<td>
<button id="action" class="btn-primary"><a href="update.php?id=<?php echo $row[0]; ?>"> update </a></button>
<button id="danger" class="btn-danger"><a href="delete.php?id=<?php echo $row[0]; ?>"> delete </a></button>
</td>
</tr>
</table>

“这是更新文件”在第一个 php 标签的更新文件中,单击保存按钮后将提交数据,它应该更新数据库中的数据,但没有发生,这是主要问题。在第二个 php 标签中,代码实际上是从数据库中获取数据并显示在 texatbox 中。

<?php
include ('../../../includes/connection.php');
ob_start();
if (isset($_POST['save'])){
$id = $_POST['id'];
$newName = $_POST['newname'];
$newAddress = $_POST['newaddress'];
$newContact = $_POST['newcontact'];

$q = "UPDATE supplier SET name='$newName', address='$newAddress', contact='$newContact' WHERE sid='$id' ";
$result = mysqli_query($db, $q) or die(mysqli_error($db));
header('location:viewSupplierList.php');
}
ob_end_clean();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Update</title>
<link type="text/css" rel="stylesheet" href="../../../css/menu.css">
<link type="text/css" rel="stylesheet" href="../../../css/hover.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" media="all">
</head>

<body>
<form class="form" method="POST" action="update.php">
<h3>Update Supplier Details</h3>
<?php
if (isset($_GET['id'])){
$id = $_GET['id'];
$q = "SELECT * FROM supplier WHERE sid='$id'";
$result1 = mysqli_query($db, $q) or die(mysqli_error($db));
$row = mysqli_fetch_array($result1);
}
?>

<label for="id">ID</label>
<input type="text" id="id" name="id" value="<?php echo $row[0];?>" disabled>
<label for="name">Name</label>
<input type="text" id="name" name="newname" value="<?php echo $row[1];?>" title="Enter at least 5 characters!" required pattern="[a-zA-Z ]{5,20}" oninvalid="this.setCustomValidity('Only alphabets are allowed!')" oninput="this.setCustomValidity('')">
<label for="address">Address</label>
<input type="text" id="address" name="newaddress" value="<?php echo $row[2];?>" title="Enter at least 10 characters!" required pattern="[a-zA-Z0-9- ]{10,100}" oninvalid="this.setCustomValidity('Only alphanumerics are allowed!')" oninput="this.setCustomValidity('')">
<label for="contact">Contact</label>
<input type="text" id="contact" name="newcontact" value="<?php echo $row[3];?>" title="Enter at least 12 number!" required pattern="[0-9-]{11,12}" oninvalid="this.setCustomValidity('Numbers with or without hyphen are allowed!')" oninput="this.setCustomValidity('')">

<input class="hvr-bubble-float-top" type="submit" name="save" value="save">
<input class="hvr-bubble-float-top" type="submit" name="cancel" value="cancel">
</form>
</body>
<script type="text/javascript" src="../../../js/menu.js" ></script>
</html>

供应商表架构

sid|    name     |      address      | contact
--------------------------------------------------
1 | Supplier A | street abc blah.. | 038157575714
2 | Supplier B | street abc blah.. | 038157575714
3 | Jhon | street abc blah.. | 038157575714
4 | Smith | street abc blah.. | 038157575714
5 | Michael | street abc blah.. | 038157575714

最佳答案

所以这里的问题是你禁用了ID输入。当归档被禁用时,它不会被发布。您的更新查询收到一个空 ID,并且不会执行保存。你应该做的是隐藏输入。

<label for="id">ID: <?php echo $row[0];?></label>
<input type="text" id="id" name="id" value="<?php echo $row[0];?>" style="display:none">

另一方面,您确实应该使用浏览器开发人员工具。当您发送 POST/GET 请求时,它会保存在网络选项卡中。您可以使用它来查看请求 header 中提交的值,并且您会发现 id 丢失,这就是您的查询不起作用的原因;-)

关于php - 当我点击提交按钮时数据没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496802/

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