0

我正在使用一个提交按钮来更新数据库中的数据并发送电子邮件。但是,数据并未保存到数据库中。代码如下所示:

<?php
    if(isset($_POST['order_status']))
    {
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
    die('Could not connect: ' . mysql_error());
    }

    $orderno =$_POST['order_no'][$i];
    $status =$_POST['order_status'][$i];
    print_r($_POST['order_no']);
    $sql = 'UPDATE Orders SET status="' . $_POST['order_status'] . '" WHERE   Orderno="' .    $_POST['order_no'] . '"';

    mysql_select_db('PurchaseOrders');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
    die('Could not update data: ' . mysql_error());
    }
    echo "Updated data successfully\n";
    mysql_close($conn);
    }
?>
<?php
    if(isset($_POST['order_selected']))
    {
    $keys = array_keys($_POST['order_selected']);

    foreach($keys as $key)
    {
        $to = $_POST['order_requested_by'][$key];
        $subject = "Order Form Approval";
        $message = "Your purchase order number: " . " " . $_POST['order_no'][$key] ."    ". "is" ." ". $_POST['order_status'][$key] ;
        $headers = "From: noreply@somewhere.com";
        $sent = mail($to, $subject, $message, $headers) ;
        if($sent)
        {header( 'Location: http://localhost/test/../' ) ; }
        else
        {print "We encountered an error sending your mail"; }
    }
}
?>

在我的表单中,输入字段如下所示:

 echo '<td><input type="text" readonly=true name="order_supplier['.$i.']" value="' .    $row['Supplier'] . '"/></td>';
    echo '<td><input type="text" readonly=true name="order_total_price['.$i.']" value="' . $row['totalprice'] . '"/></td>';
    echo '<td><input type="text" readonly=true name="order_requested_by['.$i.']" value="' . $row['requestedby'] . '"/></td>';
    echo '<td>';
    echo '<select name="order_status['.$i.']" id="id" onchange="showUser(this.value)" >';
    echo '<option value = "Pending"   name="order_status['.$i.']" class = "pending"' . ($row['status'] == 'Pending' ? ' selected=selected' : '') . '>Pending</option>';
echo '<option value = "Approved"  name="order_status['.$i.']" class = "approved"' .     ($row['status'] == 'Approved' ? ' selected=selected' : '') . '>Approved</option>';
    echo '<option value = "Disapproved" name="order_status['.$i.']" class     ="disapproved"' . ($row['status'] == 'Disapproved' ? ' selected=selected' : '') . '>Disapproved</option>';
    echo '</select>';

我真的不知道为什么会这样。当我单击提交时,会出现一条消息,表明数据已成功输入数据库。但是没有输入数据。非常感谢

4

3 回答 3

1

好吧,我很惊讶它没有抛出错误..

echo '<select name="order_status['.$i.']" id="id" onchange="showUser(this.value)" >

$_POST['order_status']是一个数组..

所以,我建议你回显 $sql,我想你会明白为什么它没有更新你想的记录。

我猜它应该读

$sql = 'UPDATE Orders SET status="' .$status  '" WHERE   Orderno="' .$orderno ' . '"'; 
于 2012-09-20T14:41:33.083 回答
0

您的 Sql 语句的“/”用法不正确。

值必须用 ' 而非 " 包围。

如果我们在您的查询中去掉您的变量,您有...

UPDATE Orders SET status="order_status" WHERE   Orderno="order_no"

实际上它应该是:

UPDATE Orders SET status='order_status' WHERE   Orderno='order_no'

将您的查询更改为:

$sql = "UPDATE Orders SET status='" . $_POST['order_status'] . "' WHERE   Orderno='" .    $_POST['order_no'] . "'";
于 2012-09-20T14:58:17.857 回答
0

如果 $_POST['order_no'] 是一个数组,则查询格式错误,这适用于所有作为数组的 postdata。那就是如果你在你的 html 中有这样的东西:

<input type="text" name="order_no[]" />
<input type="text" name="order_no[]" />
<input type="text" name="order_no[]" />

然后 $_POST['order_no'] 将有多个值和你的 sql 字符串:

$sql = 'UPDATE Orders SET status="' . $_POST['order_status'] . '" WHERE   Orderno="' . $_POST['order_no'] . '"';

不会是正确的sql。

于 2012-09-20T15:05:04.487 回答