PHP更新集如果否则

时间:2017-12-04 02:13:29

标签: php if-statement set

如果给定记录中的sinkQty字段不等于现有值,则希望当前日期为NOW(),否则保持现有日期。 更新失败。任何智慧的话都会对这个项目有所帮助!

$tbl_name="sink";

$count=mysql_num_rows($result);

$sinkid    = $_POST['sinkID'];
$qty       = $_POST['sinkQty'];
$qtyonorder= $_POST['sinkQtyOnOrder'];
$desiredqty= $_POST['sinkDesiredQty']; 
$posted    = $_POST['sinkPosted'];

for($i=0;$i<$count;$i++)
{
    $sql1=("UPDATE $tbl_name 
            SET sinkPosted = IF ( sinkQty != $qty[$i] ,NOW(), $posted[$i]),
                sinkQty=$qty[$i],
                sinkDesiredQty=$desiredqty[$i],
                sinkQtyOnOrder=$qtyonorder[$i]                         
            WHERE sinkID=$sinkid[$i]");

    $result1=mysql_query($sql1);
}

1 个答案:

答案 0 :(得分:0)

 $sql1=("UPDATE $tbl_name 
        SET sinkPosted = IF ( sinkQty != $qty[$i] ,NOW(), $posted[$i]),
            sinkQty=$qty[$i],
            sinkDesiredQty=$desiredqty[$i],
            sinkQtyOnOrder=$qtyonorder[$i]                         
        WHERE sinkID=$sinkid[$i]");
// Try
$query = "UPDATE $tbl_name 
        SET sinkPosted = IF ( sinkQty != $qty[$i] ,NOW(), $posted[$i]),
            sinkQty=$qty[$i],
            sinkDesiredQty=$desiredqty[$i],
            sinkQtyOnOrder=$qtyonorder[$i]                         
        WHERE sinkID=$sinkid[$i]";
echo "<pre>$query</pre>";
$result1 = mysql_query($query) or die(mysql_error());
// And please, remove SQL Injection from $query.