Phpmyadmin中的MySQL数据库不会更新并显示无错误

时间:2017-07-30 19:46:17

标签: php mysql phpmyadmin

我试图在显示我网站上的数据库的表格中创建一个编辑选项。 问题是,即使单击“保存”,数据库也不会更新。 没有错误显示。 还有一个EditComplaint.php,它从数据库中提取数据并在输入框中显示,以便可以编辑它和Ecomp.php,单击保存时会调用它。

<?php

        $id = $_GET['id'];

        $db_host = 'localhost'; // Server Name
        $db_user = 'Username'; // Username
        $db_pass = 'Username'; // Password
        $db_name = 'Database'; // Database Name

        $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
        if (!$conn) {
            die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
        }

        $sql = "SELECT `id`, `ref_no`, `type`, `comp_name`, `comp_no`, `station`, `pertains`, `user_remarks`, `to_whom`, `concern`, `brief_fct`, `sec_remarks`, `depart`, `cisf_remarks`, `generalcomment`, `status` FROM `Complaintstable` WHERE `Complaintstable`.`id` = '$id'";

        $query = mysqli_query($conn, $sql);

        if (!$query) {
            die ('SQL Error: ' . mysqli_error($conn));
        }

?>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link href="https://fonts.googleapis.com/css?family=Titillium+Web|Roboto+Condensed:400,300|Pathway+Gothic+One|ABeeZee:400,400italic" rel="stylesheet" type="text/css">
    <link href="master-stylesheet.css" rel="stylesheet">
    <link href="custom-stylesheet.css" rel="stylesheet">
    <link href="complaintstable.css" rel="stylesheet">
    <link href="dmrc-favicon.png" rel="shortcut icon" type="image/x-icon">
    <title>DMRC/Login</title>
</head>
<body>
    <form action="Ecomp.php" id="form1" method="post" name="form1">
        <!-- header section -->
        <div class="header">
            <div class="header-inner">
                <div class="header-top">
                    <div class="welcome-guest">
                        <span class="align-right float-right" id="lblusername" style="color:Black;">Guest</span><em><span class="welcome-cont float-right fontfamily_2" id="lblwelcome" style="padding-left:5px;padding-right:5px;color:black;">Welcome</span></em>
                    </div>
                    <div class="header-top-links">
                        <ul>
                            <li>
                                <a href="" id="active">Home</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="header-bottom">

                </div>
            </div>
        </div>

<div class="container clearfix">
                <br><br>
            <table class="data-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Referral/No.</th>
                        <th>Type</th>
                        <th>Complainant Name</th>
                        <th>Station</th>
                        <th>Pertains To</th>
                        <th>User Remarks</th>
                        <th>To Whom</th>
                        <th>Concern</th>
                        <th>Brief Fact</th>
                        <th>Security Comments</th>
                        <th>Deptt</th>
                        <th>CISF Comments</th>
                        <th>General Comment</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $i = 0;
                        while($row = $query->fetch_assoc())
                        {
                            $id = $row['id'];
                            $ref_no = $row['ref_no'];
                            $type = $row['type'];
                            $comp_name = $row['comp_name'];
                            $station = $row['station'];
                            $pertains = $row['pertains'];
                            $user_remarks = $row['user_remarks'];
                            $to_whom = $row['to_whom'];
                            $concern = $row['concern'];
                            $brief_fct = $row['brief_fct'];
                            $sec_remarks = $row['sec_remarks'];
                            $depart = $row['depart'];
                            $cisf_remarks = $row['cisf_remarks'];
                            $generalcomment = $row['generalcomment'];
                            $status = $row['status'];
                            if ($i == 0)
                            {
                                $i++;
                                echo "<tr>";
                                echo "<td><input type='text' name='id' value='$id'/></td>";
                                echo "<td><input type='text' name='ref_no' value='$ref_no'/></td>";
                                echo "<td><input type='text' name='type' value='$type'/></td>";
                                echo "<td><input type='text' name='comp_name' value='$comp_name'/></td>";
                                echo "<td><input type='text' name='station' value='$station'/></td>";
                                echo "<td><input type='text' name='pertains' value='$pertains'/></td>";
                                echo "<td><input type='text' name='user_remarks' value='$user_remarks'/></td>";
                                echo "<td><input type='text' name='to_whom' value='$to_whom'/></td>";
                                echo "<td><input type='text' name='concern' value='$concern'/></td>";
                                echo "<td><input type='text' name='brief_fct' value='$brief_fct'/></td>";
                                echo "<td><input type='text' name='sec_remarks' value='$sec_remarks'/></td>";
                                echo "<td><input type='text' name='depart' value='$depart'/></td>";
                                echo "<td><input type='text' name='cisf_remarks' value='$cisf_remarks'/></td>";
                                echo "<td><input type='text' name='generalcomment' value='$generalcomment'/></td>";
                                echo "<td><input type='text' name='status' value='$status'/></td>";
                                echo "<td><a href='delete.php?id=$id' class='button-new'>Delete</a></td>";
                                echo "</tr>";
                            }
                            echo '<br><br>';
                        }
                    ?>
                </tbody>
            </table>
            <br><br>
            <input type="submit" class="button-new" value="Save"/>
        </div>


<div class="footer">
            <div class="footer-top">
                <div class="footer-top-inner">
                    <ul>
                        <li>
                            <a href="">FAQs</a>
                        </li>
                        <li>
                            <a href="">Contact Us</a>
                        </li>
                        <li>
                            <a href="">Disclaimer</a>
                        </li>
                        <li>
                            <a href="">Terms &amp; Conditions</a>
                        </li>

                    </ul>
                </div>
            </div>
            <div class="footer-bottom fontfamily_2">
                <div class="footer-bottom-inner">
                    <div class="float-left footer-text">
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

这里是保存电话的Ecomp.php

<?php
        $db_host = 'localhost'; // Server Name
        $db_user = 'Username'; // Username
        $db_pass = 'Username'; // Password
        $db_name = 'Database'; // Database Name
        // Create connection
        $con = mysqli_connect($servername, $username, $password) or die("Unable to Connect to '$dname'");
        // Check connection
        if (!$con)
        {
            echo "Please try later.";
        }
        else
        {
            mysqli_select_db($con, $dname);
        }

            $id = $_POST['id'];
            $ref_no = $_POST['ref_no'];
            $type = $_POST['type'];
            $comp_name = $_POST['comp_name'];
            $station = $_POST['station'];
            $pertains = $_POST['pertains'];
            $user_remarks = $_POST['user_remarks'];
            $to_whom = $_POST['to_whom'];
            $concern = $_POST['concern'];
            $brief_fct = $_POST['brief_fct'];
            $sec_remarks = $_POST['sec_remarks'];
            $depart = $_POST['depart'];
            $cisf_remarks = $_POST['cisf_remarks'];
            $generalcomment = $_POST['generalcomment'];
            $status = $_POST['status'];

                mysqli_query($con," UPDATE `Complaintstable` SET `ref_no`= '$ref_no',`type`='$type',`comp_name`=`$comp_name`,`station`='$station',`pertains`='$pertains',`user_remarks`='$user_remarks',`to_whom`='$to_whom',`concern`='$concern',`brief_fct`='$brief_fct',`sec_remarks`='$sec_remarks',`depart`='$depart',`cisf_remarks`='$cisf_remarks',`generalcomment`='$generalcomment',`status`='$status' WHERE `Complaintstable`.`id` = '$id'");

                header("Location: complaintstable.php");

?>

请指导我。 这是我第一次来这里。 提前致谢。 编辑:在phpmyadmin控制台中运行时,更新SQL查询有效。

1 个答案:

答案 0 :(得分:1)

我可以看到你在反引号中包装了一个db值$comp_name,但它可能是一个字符串..

在这一行中查看$ comp_name:

mysqli_query($con," UPDATE `Complaintstable` SET `ref_no`= '$ref_no',`type`='$type',`comp_name`='$comp_name',`station`='$station',`pertains`='$pertains',`user_remarks`='$user_remarks',`to_whom`='$to_whom',`concern`='$concern',`brief_fct`='$brief_fct',`sec_remarks`='$sec_remarks',`depart`='$depart',`cisf_remarks`='$cisf_remarks',`generalcomment`='$generalcomment',`status`='$status' WHERE `Complaintstable`.`id` = '$id'");

我同意马格努斯的观点。以下是如何使用PDO 执行相同操作的方法。尽管在mysql中也可以使用预处理语句,但我总是会选择PDO。 See this reference如果您想花时间使用它。

$db = new PDO('mysql:host=localhost;dbname=someDatabase', 'someUser', 'somePass');
$stmt = $db->prepare("UPDATE `Complaintstable` SET `ref_no`= ?,`type`=  ?,`comp_name`=?,`station`= ?,`pertains`= ?,`user_remarks`=?,`to_whom`= ?,`concern`= ?,`brief_fct`= ?,`sec_remarks`= ?,`depart`= ?,`cisf_remarks`= ?,`generalcomment`= ?,`status`= ? WHERE `Complaintstable`.`id` = ?");
$stmt->execute(array($ref_no,$type,$comp_name,$station,$pertains,$user_remarks,$to_whom,$concern,$brief_fct,$sec_remarks,$depart,$cisf_remarks,$generalcomment,$status,$id));