dongxian3852 2019-02-02 19:37
浏览 55
已采纳

单击“提交”按钮时,数据不会更新

When i click on submit button it does not update data in database. I have looked many times to figure out what the problem is. I think problem is not with the code. It may be because of XAMPP. I'm using phpstorm and i have given database connection in both files. If you need additional information please let me know. Thanks

"This is supplier list file"

<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>

"This is update file" In update file in first php tag the data will be submitted upon clicking the save button and it should update data in database which is not happening and it is the main problem. In second php tag the code is actually fetching the data form database and is being displayed in 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>

Supplier table schema

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
  • 写回答

1条回答 默认 最新

  • dtz88967 2019-02-02 20:47
    关注

    So the problem here is that you have disabled the ID input. When a filed is disabled, it's not posted. Your update query receives an empty ID and there for does not execute the save. What you should do is make the input hidden instead.

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

    On a side not, You should really use the browser developper tools. When you send a POST/GET request, it's saved in the network tab. You can use that to see the submitted values in the request headers and there for, you would have seen that id is missing and that's why your query is not working ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配