简体   繁体   English

PHP-代码突然无法更新mysql行

[英]PHP - Code suddenly not updating mysql row

This code had been working for a while, but suddenly it doesn't without anything changing. 这段代码已经工作了一段时间,但是突然之间,它没有做任何更改。 The first part of the code is checking the user's credentials are correct. 代码的第一部分是检查用户的凭据是否正确。 If they are, the data entered in the form is then submitted to the mysql and an email notification is sent. 如果是,则将表单中输入的数据提交给mysql并发送电子邮件通知。 Here is the code: 这是代码:

 <?php
    if(isset($_POST['insert'])){
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $databaseName = "change_management";
    $connect = mysqli_connect($hostname, $username, $password, $databaseName);

    $user = $_POST['user'];
    $passcode = $_POST['passcode'];

    $sql = "SELECT * FROM admin WHERE username = '" . $user . "' and passcode ='".md5($passcode)."'";
    $result = mysqli_query($connect,$sql);
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    $count = mysqli_num_rows($result);
    if($count ==0) {
    echo "
<font color=red><strong>Authentication failed - Invalid credentials</font></strong><br><br>";
    }
else {
    $change_id = $_POST['change_id'];
    $approval_disposition = $_POST['approval_disposition'];
    $approval_reason = $_POST['approval_reason'];
    $approval_impact = $_POST['approval_impact'];
    $user = $_POST['user'];
    $approval_date = date('Y/m/d');
    $approval_expiry_date = date('Y/m/d', strtotime('+2 weeks'));

    $query = "UPDATE change_request_tbl SET approval_disposition='$approval_disposition', approval_reason='$approval_reason', approval_impact='$approval_impact', approval_name='$user', approval_date='$approval_date', approval_expiry_date='$approval_expiry_date' WHERE change_id='$change_id'";
    $query = "SELECT `change_requestor` FROM `change_request_tbl` WHERE `change_id` = $change_id";
    $result1 = mysqli_query($connect,$query);
    while ($row = mysqli_fetch_array($result1))

    $to = "******, ****, ****, *****, $row[change_requestor]";
    $subject = "New Change Acceptance Board (CAB) Approval";
    $message = " 
    <html>
    <head>
    <style>
    body {font-family: Calibri;}
    body {font-style: italic;}
    .table {
    background-color: white;
    }
    </style>
    <title>Change Acceptance Board Approval</title>
    </head>
    <body>
    <p>A new Change Acceptance Board (CAB) Approval has been submitted:</p>
    <table border=4 bordercolor=black class=table>
    <tr>
    <th>Change ID:</th>
    <th>Disposition:</th>
    <th>Reason for Rejection or Exceptions:</th>
    <th>Impact (By CAB):</th>
    <th>Approved/Rejected by:</th>
    <th>Date Approved:</th>
    <th>Approval Expiry Date*:</th>
    </tr>
    <tr>
    <td>$change_id</td>
    <td>$approval_disposition</td>
    <td>$approval_reason</td>
    <td>$approval_impact</td>
    <td>$user</td>
    <td>$approval_date</td>
    <td>$approval_expiry_date</td>
    </tr>
    </table>
    <p>*This change must be completed by the Expiry Date. If this is not achieved a second CAB Request must be raised referencing this Change ID.</p>
    </body>
    </html>";
    $headers = "MIME-Version:1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .='From: <******>' . "\r\n";

    mail($to,$subject,$message,$headers);
    echo "
Your CAB Approval Form has been submitted. You will receive an email confirmation shortly.<br><br>";
    }
    }
    ?>

I am not getting any errors but I am getting the emails with the correct information. 我没有收到任何错误,但收到的电子邮件中包含正确的信息。 This issue lies solely with the update query. 此问题仅与更新查询有关。 Any ideas why it has suddenly stopped working? 有什么想法为什么它突然停止工作?

您两次声明了$query ,因此使用以下SELECT覆盖了UPDATE Query。

$query = "UPDATE change_request_tbl SET approval_disposition='$approval_disposition', approval_reason='$approval_reason', approval_impact='$approval_impact', approval_name='$user', approval_date='$approval_date', approval_expiry_date='$approval_expiry_date' WHERE change_id='$change_id'";
$result = mysqli_query($connect,$query);

$query = "SELECT `change_requestor` FROM `change_request_tbl` WHERE `change_id` = $change_id";

$result1 = mysqli_query($connect,$query);

Sql queries valiable are overridden in the problem 问题中的sql查询变量被覆盖

I believe your problem lies here: 我相信您的问题出在这里:

$query = "UPDATE change_request_tbl SET approval_disposition='$approval_disposition', approval_reason='$approval_reason', approval_impact='$approval_impact', approval_name='$user', approval_date='$approval_date', approval_expiry_date='$approval_expiry_date' WHERE change_id='$change_id'";

$query = "SELECT `change_requestor` FROM `change_request_tbl` WHERE `change_id` = $change_id";

$result1 = mysqli_query($connect,$query);

The UPDATE $query is overwritten by the subsequent SELECT $query, so it just selects the data but does not update it beforehand. UPDATE $ query被后续的SELECT $ query覆盖,因此它只是选择数据而不会事先更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM