MySQL UPDATE语句不通过Web表单更新数据库数据

时间:2014-04-23 09:42:57

标签: php html mysql forms

场景:我有一个网络表单,用户可以手动输入数据(见下文)。用户将提交表单,数据将自动添加到数据库中

date, order_ref, first_name, last_name, postcode, country, quantity, scott_packing, packing_price, courier_price, dispatch_type, job_status

在另一个页面上,用户将只能查看当前正在处理的所有作业(此数据来自数据库)并添加跟踪编号并编辑packing_price,courier_price and job_status并提交新数据

http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-23at104045_zps2a628d50.png

问题当用户点击“全部提交”按钮时,应该将用户重定向到感谢页面,该页面只是通知用户他们的输入已经成功但是此刻,用户仅被定向到包含导航菜单的空白页面。我检查了数据库以查看数据是否已更新但没有任何更改。如何让我的更新语句起作用,以便用户可以更新现有的作业?

这是显示所有作业的页面代码:

        <?
session_start();

if(!session_is_registered(myusername))
{
    header("location:../index.php");
}

include("../template/header.php");
include("../controllers/cn.php");

$sql = "SELECT * FROM Jobs";
$qry = mysql_query($sql);

echo "<div class='content'>";
echo "<form class='form_edit' method='post' action='updatejob.php'>";
echo "<table id='job_list' cellpadding='0' cellspacing='0'>
<tr>
<th>Job No</th>
<th>Date</th>
<th>Qty</th>
<th>Postcode</th>
<th>Country</th>
<th>Packed by Scott</th>
<th>Packing Price</th>
<th>Courier Price</th>
<th>Tracking No</th>
<th>Dispatch Type</th>
<th>Job Status</th>
</tr>";


while($row = mysql_fetch_array($qry))
{
    echo "<tr>";
    echo "<td width='80' style='text-align: center;'>" . $row['order_ref'] . "</td>";
    echo "<td width='100' style='text-align: center;'>" . $row['date'] . "</td>";
    echo "<td width='100' style='text-align: center;'>" . $row['quantity'] . "</td>";
    echo "<td width='100' style='text-align: center;'>" . $row['postcode'] . "</td>";
    echo "<td width='100' style='text-align: center;'>" . $row['country'] . "</td>";
    echo "<td width='100' style='text-align: center;'><input type='CHECKBOX' id='scott_packing' name='scott_packing' value='". $row['scott_packing'] . "'></td>";
    echo "<td width='100' style='text-align: center;'><input type='text' id='packing_price' name='packing_price' value='".  $row['packing_price'] . "'/></td>";
    echo "<td width='100' style='text-align: center;'><input type='text' id='courier_price' name='courier_price' value='". $row['courier_price']."'/></td>";
    echo "<td width='100' style='text-align: center;'><input type='text' id='tracking_number' name='tracking_number' value='". $row['tracking_number'] . "'/></td>";
    echo "<td width='100' style='text-align: center;'>" . $row['dispatch_type'] . "</td>";
    echo "<td width='100' style='text-align: center;'><select name='job_status' id='job_status'>
        <option value='". $row['job_status'] ."'>". $row['job_status']. " <option value='dispatched'>Dispatched</td>";   

    //echo "<td width='100' style='text-align: center;'><a href='editjob.php'>edit</td>";

    echo "</tr>";
}

echo "</table>";
echo "<input type='submit' name='submit' value='submit all'/>";
echo "</form>";

mysql_close();


?>

以下是应该更新数据的代码:

    <?
session_start();

if(!session_is_registered(myusername)){
header("location:../index.php");

}

include("../template/header.php");
include("../controllers/cn.php");

if (isset($_POST['submit']))
{
    $order_ref = $_POST['order_ref'];
    $packing_price = $_POST['packing_price'];
    $courier_price = $_POST['courier_price'];
    $tracking_number = $_POST['tracking_number'];
    $job_status = $_POST['job_status'];

    $sql_qry = "UPDATE Jobs SET '$packing_price, $courier_price, $tracking_number, $job_status' WHERE order_ref = '$order_ref'";

    $query = mysql_query($sql_query);

      if(!$query)
    {
        die('Could not update data' .mysql_error());
    } else
    {
        header("location: updatesuccess.php");
        exit;
    }

    mysql_close();

}




?>

2 个答案:

答案 0 :(得分:0)

您的更新查询错误。更新的查询应该像

UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

在这种情况下,它将类似于

UPDATE Jobs SET packing_price='$packing_price',courier_price='$courier_price',tracking_number='$tracking_number',job_status='job_status' WHERE order_ref = '$order_ref'";

并且您错过了输入字段的name属性。如果您未指定,则无法访问$_POST['packing_price'] packing_price,其中name是输入字段的method="post"

同时将echo "<form class='form_edit' action='updatejob.php' method='post'>"; 添加到表单中,如

{{1}}

答案 1 :(得分:0)

尝试使用此查询

$sql_qry = "UPDATE Jobs SET column1 = '$packing_price', column2 ='$courier_price', .... WHERE order_ref = '$order_ref'";

就像Roland Jansen所说,你缺少输入标签上的名字属性