简体   繁体   English

我在这里做错了什么?

[英]What am i doing wrong here?

I've been staring at this page for over an hour. 我盯着这个页面已经一个多小时了。 My update function just doesnt not seem to update. 我的更新功能似乎并没有更新。 When i tried it through sql it seems ok. 当我通过sql尝试时,看起来还可以。 I have a form at the bottom of this page which updates a field in a table. 我在此页面底部有一个表单,用于更新表中的字段。 Can anyone spot the mistakes? 谁能发现错误?

   <?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();

  ?>

Function 功能

    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);
            }

change: 更改:

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

to: 至:

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

and: 和:

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);
}

to: 至:

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);
}

Try that. 试试看 Code hasn't been tested but it's a good place to start. 代码尚未经过测试,但这是一个不错的起点。

To try and debug what's going on here do the following: 要尝试调试这里发生的事情,请执行以下操作:

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());
}

Also, it looks like on your form submission you're not passing in the Reference parameter so the if(isset($_GET['Reference'])) will fail when you post the form. 另外,看起来在提交表单时,您没有传递Reference参数,因此在发布表单时if(isset($ _ GET ['Reference']))将失败。 I've change the table and form code below to make it more readable, pass in the Reference param on form submission, and also to update the db record BEFORE fetching the dataset so you'll see the updated records in the table returned. 我已经更改了下面的表和表单代码,以使其更具可读性,在提交表单时传递Reference参数,并在获取数据集之前更新db记录,以便您在返回的表中看到更新的记录。

// 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();

Can I advise you break this down by first checking what mysql_query returns. 我可以建议您先检查一下mysql_query返回什么来分解此内容。 It maybe that this particular variable is defined incorrectly. 可能是该特定变量定义不正确。 Also remember add quotes to the values in your query. 还请记住在查询中的值上添加引号。

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

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