我在这里做错了什么


What am i doing wrong here?

我已经盯着这个页面看了一个多小时了。我的更新功能似乎没有更新。当我通过sql尝试它时,它似乎还可以。我在这个页面的底部有一个表单,它更新了表中的一个字段。有人能发现错误吗?

   <?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);

// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
    require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();

 if(isset($_GET['Reference'])){
 $todays_date = date("Y-m-d H:i:s");
 $content .= " <h3> Details for Bundle : $reference </h3> ";
 $bundle = $BundleProgress->GetBundle($_GET['Reference']);   
 $reference = $_GET['Reference'];
 // Now show the details
    foreach($bundle as $x){
        $content .= "
       <table>
                                     <tr>
                    <th> Location </th> 
                    <td>" . $x['Description'] . "</td> 
                    </tr>

                    <tr>
                    <th> Works Order Number </th> 
                    <td>" . $x['WorksOrder'] . "</td> 
                    </tr>

                    <tr>
                    <th> Bundle Number </th> 
                            <td>" . $x['Number'] . "</td>
                    </tr>

                     <tr>
                    <th>Qty Issued</th>
                    <td>" . $x['Qty'] . "</td>
                    </tr>

                    <tr>
                    <th>Bundle Reference </th> 
                    <td>" . $x['Reference'] . "</td>
                    </tr>
                    <tr>
                    <th>Style description</th> 
                                            <td>" . $x['Stock'] . "</td>
                    </tr>
                    <tr>
                            <th>Due Date</th>
                    <td>" . $x['DueDate'] . "</td>
                    </tr>

                    <tr>
                    <th>Date In </th>
                    <td>" . $x['DateIN'] . "</td>
                    </tr>
                    <tr>
                    <th>Date Out</th>
                    <td>" . $x['DateOUT'] . "</td>
                    </tr>
                    <tr>
                    <th>Last Code</th>
                    <td>" . $x['Last'] . "</td>
                    </tr>
                </table>
                <br> "; 
    }
                 $content .= " </table>
                <form action='viewBundle.php?step=2' method='post'>
                <p>Reason: <input type='text' name='reason' /><br       
                                    /><p>
                <p><input type='hidden' name='bundlereference'   
                                     id='Username' value='" . $x['Reference'] . "' />
                <input type='submit' name ='add'/></form>
                </table>  ";   
                if($_GET['step'] == 2) {

        $BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
                 $content .= " <a href='index.php?location=" .   
              $x['Description'] . "'> updated</a> "; 
                }
       }

     else {
    $content .= "<h3>Something has gone wrong</h3>
    <br>
    <a href='index.php?location=" . $x['Description'] . "'> Return to Previous            
  Page </a> 
    ";
}
  $template->SetTag("content", $content);
  echo $template->Display();
  ?>

功能

    public function UpdateReason($reason, $bundlereference) {
                    $sql = "UPDATE `ArchiveBundle`
                                    SET `Issue` = " . $reason . "
                                    WHERE `BundleReference` = " . $bundlereference .    
                ";";
                    mysql_select_db(DB_DATABASE_NAME, $this->conn);
                    return mysql_query($sql, $this->conn);
            }

更改:

if($_GET['step'] == 2)

至:

if((int)$_GET['step'] === 2)

和:

public function UpdateReason($reason, $bundlereference) {
    $sql = "UPDATE `ArchiveBundle`
        SET `Issue` = " . $reason . "
        WHERE `BundleReference` = " . $bundlereference .    
        ";";
    mysql_select_db(DB_DATABASE_NAME, $this->conn);
    return mysql_query($sql, $this->conn);
}

至:

public function UpdateReason($reason, $bundlereference) {
    mysql_select_db(DB_DATABASE_NAME, $this->conn);
    $_reason = mysql_real_escape_string($reason,$this->conn);
    $_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
    $sql = "UPDATE `ArchiveBundle`
            SET `Issue` = '" . $_reason . "'
            WHERE `BundleReference` = '" . $_bundlereference . "'";
    return mysql_query($sql, $this->conn);
}

试试看。代码尚未经过测试,但这是一个很好的起点。

要尝试调试这里发生的事情,请执行以下操作:

public function UpdateReason($reason, $bundlereference) {
    error_reporting(E_ALL ^ E_NOTICE);
    $db_selected = mysql_select_db(DB_DATABASE_NAME, $this->conn);
    if (!$db_selected) {
        die("Can't use db : " . mysql_error());
    }
    $_reason = mysql_real_escape_string($reason,$this->conn);
    $_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
    $sql = "UPDATE `ArchiveBundle`
            SET `Issue` = '" . $_reason . "'
            WHERE `BundleReference` = '" . $_bundlereference . "'";
    mysql_query($sql, $this->conn);
    die(mysql_error());
}

此外,在提交表单时,您似乎没有传递Reference参数,因此在发布表单时if(isset($_GET['Reference'])将失败。我更改了下面的表和表单代码,使其更具可读性,在表单提交时传递Reference参数,并在获取数据集之前更新数据库记录,以便您可以看到返回的表中更新的记录。

// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);

// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();

if(isset($_GET['Reference'])){
    if($_GET['step'] == 2) {
        $BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
    }
    $todays_date = date("Y-m-d H:i:s");
    $content .= " <h3> Details for Bundle : $reference </h3> ";
    $bundle = $BundleProgress->GetBundle($_GET['Reference']);   
    $reference = $_GET['Reference'];
    // Now show the details
    foreach($bundle as $x){
        $content .= "
                    <table>
                        <tr><th> Location </th><td>" . $x['Description'] . "</td></tr>
                        <tr><th> Works Order Number </th><td>" . $x['WorksOrder'] . "</td></tr>
                        <tr><th> Bundle Number </th><td>" . $x['Number'] . "</td></tr>
                        <tr><th>Qty Issued</th><td>" . $x['Qty'] . "</td></tr>
                        <tr><th>Bundle Reference </th><td>" . $x['Reference'] . "</td></tr>
                        <tr><th>Style description</th><td>" . $x['Stock'] . "</td></tr>
                        <tr><th>Due Date</th><td>" . $x['DueDate'] . "</td></tr>
                        <tr><th>Date In </th><td>" . $x['DateIN'] . "</td></tr>
                        <tr><th>Date Out</th><td>" . $x['DateOUT'] . "</td></tr>
                        <tr><th>Last Code</th><td>" . $x['Last'] . "</td></tr>
                    </table>
                    <br>";
    }
    $content .= "<table>
                    <form action='viewBundle.php?Reference=" . $_GET['Reference'] . "&step=2' method='post'>
                        <p>Reason: <input type='text' name='reason' /></p><br/>
                        <p><input type='hidden' name='bundlereference' id='Username' value='" . $x['Reference'] . "' /></p>
                        <input type='submit' name ='add'/>
                    </form>
                </table>";   
} else {
    $content .= "<h3>Something has gone wrong</h3>
                <br>
                <a href='index.php?location=" . $x['Description'] . "'> Return to Previous Page </a> 
                ";
}
$template->SetTag("content", $content);
echo $template->Display();

我可以建议您先检查mysql_query返回的内容来分解它吗。可能是这个特定变量的定义不正确。还要记住在查询中的值中添加引号。