更新的数据在MYSQL上恢复为原始值

时间:2017-03-07 01:30:14

标签: php mysql

我有一个奇怪的问题,当我运行更新功能时,它已成功执行&我在mysql数据库检查它已更新。但是,几分钟后,mysql数据库的值恢复为原始值(更新前)。

因为我可以看到我的代码似乎没问题,因为我可以更新mysql数据库中的值但不知何故可能与$ _SESSION有关,这是我不熟悉的。以下是我的代码: -

    <?php session_start();
    $staff_no=$_SESSION['staff_no'];
    $user_name=$_SESSION['user_name'];
    $pass=$_SESSION['pass'];
    $level=$_SESSION['level'];
    include("../connect.php");
    include ("../session.php");
    $today=date('Y-m-d');

    $VendorID=$_GET['vendorid'];

    if($submit=isset($_POST['submit']))
            {
            $VendorID=isset($_POST['VendorID']) ? $_POST['VendorID'] : '';
            $CompanyName=isset($_POST['CompanyName']) ? $_POST['CompanyName'] : '';
            $Address=isset($_POST['Address']) ? $_POST['Address'] : '';
            $TelephoneNo=isset($_POST['TelephoneNo']) ? $_POST['TelephoneNo'] : '';
            $FacsimileNo=isset($_POST['FacsimileNo']) ? $_POST['FacsimileNo'] : '';
            $URL=isset($_POST['URL']) ? $_POST['URL'] : '';
            $RegisterDate=isset($_POST['RegisterDate']) ? $_POST['RegisterDate'] : '';
            $status=isset($_POST['status']) ? $_POST['status'] : '';
            $region=isset($_POST['region']) ? $_POST['region'] : '';

            $sql2 = "UPDATE vendor SET VendorID='$VendorID',CompanyName='$CompanyName',Address='$Address',TelephoneNo='$TelephoneNo',
                    FacsimileNo='$FacsimileNo',URL='$URL',RegisterDate='$RegisterDate',status='$status',region='$region'
                    WHERE VendorID='$VendorID'";

            if(@mysqli_query($con, $sql2))
            {
                echo "<script type='text/javascript'>\n";
                echo "alert('Successfully edit data');\n";
                echo "</script>";
                echo "<script>window.navigate('vendordetails_add.php?vendorid=$VendorID')</script>";    
            }
            else
                echo 'the data failed to edit'.mysqli_error();

        }//if isset submit

          $sql =mysqli_query($con, "SELECT * FROM vendor WHERE VendorID='$VendorID'");
          $row = mysqli_fetch_array($sql);

          $id=$row['id'];
          $VendorID=$row['VendorID'];
          $CompanyName=$row['CompanyName'];
          $Address=$row['Address'];
          $TelephoneNo=$row['TelephoneNo'];
          $FacsimileNo=$row['FacsimileNo'];
          $URL=$row['URL'];
          $RegisterDate=$row['RegisterDate'];
          $status=$row['status'];
          $region=$row['region'];         
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form name="vendor_edit" enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="VendorID" value="<?php echo $VendorID; ?>" />
<input type="hidden" name="status" value="<?php echo $status; ?>"  />
<table width="784" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td height="80" valign="top">    
            <table width="100%" border="0" cellpadding="0" cellspacing="0" background="../image/center.jpg">                              
                <tr>
                    <td height="20" valign="top">&nbsp;</td>
                    <td width="81" valign="top" class="txt">Company</td>
                    <td colspan="2" valign="top"><input name="CompanyName" type="text" class="text" id="CompanyName" size="50"  value="<?php echo $CompanyName; ?>"/></td>
                </tr>      
                <tr>
                    <td height="60" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Address</td>
                    <td colspan="2" valign="top"><textarea name="Address" cols="40" rows="4" class="txt" id="Address"><?php echo $Address; ?></textarea></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Telephone No</td>
                    <td colspan="2" valign="top"><input name="TelephoneNo" type="text" class="text" id="TelephoneNo" size="15" value="<?php echo $TelephoneNo; ?>"/></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt"> Fax No</td>
                    <td colspan="2" valign="top"><input name="FacsimileNo" type="text" class="text" id="FacsimileNo" size="15" value="<?php echo $FacsimileNo; ?>"/></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Website</td>
                    <td colspan="2" valign="top"><input name="URL" type="text" class="text" id="URL" size="30" value="<?php echo $URL; ?>"/></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Region</td>
                    <td colspan="2" valign="top">
                        <select name=region id="region">
                            <option value="Malaysia"<?php if ($region=='Malaysia'){ ?> selected="yes" <?php } ?> >Malaysia </option>
                            <option value="International"<?php if ($region=='International'){?> selected="yes" <?php } ?> >International</option>
                        </select></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Status</td>
                    <td colspan="2" valign="top">
                        <select name=status id="status">
                            <option value="Active"<?php if ($status=='Active'){ ?> selected="yes" <?php } ?> >Active </option>
                            <option value="KIV"<?php if ($status=='KIV'){?> selected="yes" <?php } ?> >KIV</option>
                            <option value="Reconsider"<?php if ($status=='Reconsider'){?> selected="yes" <?php } ?> >Reconsider</option>
                        </select></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top" class="txt">Register Date</td>
                    <td colspan="2" valign="top"><input name="RegisterDate" type="text" class="text" id="RegisterDate" value="<?php echo $RegisterDate; ?>" size="15"/></td>
                </tr>
                <tr>
                    <td height="21" valign="top">&nbsp;</td>
                    <td valign="top">&nbsp;</td>
                    <td colspan="2" width="613" valign="top">
                        <input type="submit" name="submit" class="text" value="Update" />
                        <input type="button" name="back" value="Back" class="text" onClick="window.history.back(); return false;" /></td>
                </tr> 
              </td>
         </table>  
</form>
</body>
</html>

另一个涉及$ status的页面是vendor_add.php: -

<?php 
session_start();
$staff_no=$_SESSION['staff_no'];
$user_name=$_SESSION['user_name'];//call session back
$pass=$_SESSION['pass'];
$level=$_SESSION['level'];
include("../connect.php");
include ("../session.php");
$today=date('Y/m/d');

if($submit=isset($_POST['submit']))
{   
    $sql=mysqli_query($con, "SELECT VendorID FROM vendor ORDER BY  VendorID DESC LIMIT 1");
    $row=mysqli_fetch_array($sql);

    $temp=$row['VendorID'];
    $VendorID=$temp + 1;

    $today=date('Y-m-d');

    $CompanyName=isset($_POST['CompanyName']) ? $_POST['CompanyName'] : '';
    $Address=isset($_POST['Address']) ? $_POST['Address'] : '';
    $TelephoneNo=isset($_POST['TelephoneNo']) ? $_POST['TelephoneNo'] : '';
    $FacsimileNo=isset($_POST['FacsimileNo']) ? $_POST['FacsimileNo'] : '';
    $URL=isset($_POST['URL']) ? $_POST['URL'] : '';
    $RegisterDate=isset($_POST['RegisterDate']) ? $_POST['RegisterDate'] : '';
    $region=isset($_POST['region']) ? $_POST['region'] : '';

    $sql2 = "INSERT INTO vendor(VendorID,CompanyName,Address,TelephoneNo,FacsimileNo,URL,RegisterDate,region,status)VALUES
            ('$VendorID','$CompanyName','$Address','$TelephoneNo','$FacsimileNo','$URL','$RegisterDate','$region','active')";

    if(mysqli_query($con, $sql2))
        {
            echo "<script type='text/javascript'>\n";
            echo "alert('Successfully add data');\n";
            echo "</script>";
            echo "<script>window.navigate('vendordetails_add.php?vendorid=$VendorID')</script>";    
        }
        else
            echo 'the data could not be added'.mysqli_error();

}//if isset submit
mysqli_close($con);     
?>

还有我的connect.php: -

<?php

//connection to database
//must include this file in every file to undeny data access


$dbuser        = "root";  
$dbserver    = "localhost";  
$dbpass        = "mypass123";  
$dbname        = "vendor";  


$con= new mysqli($dbserver, $dbuser, $dbpass) or die ("UNABLE TO CONNECT TO DATABASE"); 
mysqli_select_db($con, $dbname) or die ("UNABLE TO SELECT DATABASE"); 

?>

2 个答案:

答案 0 :(得分:0)

抱歉,麻烦。我发现了头痛的罪魁祸首。该剧本由前工作人员开发。我发现index.php中包含autorun.php脚本。

autorun.php会自动将供应商.class_name { word-wrap: break-word; } 置于&#34; Active&#34;,&#34; KIV&#34; &安培; &#34;重新考虑&#34;基于脚本中指定的时间范围。这就是为什么每次我更新status时它都会恢复为&#34; Active&#34;几分钟后。

无论如何,谢谢你的帮助.. !!

答案 1 :(得分:-1)

如果您使用支持事务的InnoDB表,那么如果您没有启动显式事务,则其中一个表上的每个操作都会启动一个隐式事务。

https://dev.mysql.com/doc/refman/5.7/en/commit.html

只要您在同一会话中(关键字&#34;连接池&#34;以及&#34;连接超时&#34;),您的更改对您而言是可见的,但对其他任何人都不可见。当您的连接最终超时时,事务将回滚而不是提交。这是真的,因为你永远不会在这里关闭你的会议。

要使更改成为永久更改,您有3个选项:

  • 操作后施放commit
  • 设置隐式提交并正确关闭会话
  • 使用例如MyISAM表(尽管它们没有使用外键约束)

一般来说,你应该:

  • 正确关闭您的结果集(mysqli_free_result)
  • 确保在脚本终止时正确关闭连接
  • 确保所有交易详情都已正确设置,否则您将遇到锁定问题

这应该可以解决您的问题,在整个PHP脚本部分的末尾(?>之前),执行以下操作:

mysqli_free_result($con, $sql);
mysqli_query($con, "COMMIT");
mysqli_close($con);