PHP失败更新现有记录

时间:2013-11-26 10:43:39

标签: php mysql sql-update row

我有以下PHP代码:

//Selecting amount from rcpts_exp table
$query = "SELECT amount from rcpts_exp";
$result = mysql_query($query);
if(!$result){
    echo "FAILED";
}
$num = mysql_num_rows($result);

//fetching each row
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
//Simple Mathematics 
$a_b_t = $row['amount'] / 1.16;
$t = $a_b_t * 0.16;

//--------------------------------For Testing Output
//echo $row['amount']."<br />";
//echo "ABT = ". $a_b_t = number_format($a_b_t,2)."<br />";
//echo "tax = ".$t  = number_format($t,2)."<br />";
//-------------------------------End Testing

//Update the values into rcpts_exp for each amount
$query2 = "UPDATE rcpts_exp SET amount_before_tax = '".$a_b_t."', tax = '".$t."' WHERE amount = '".$row['amount']."'";
$result2 = mysql_query($query2);
if(!$result2){
echo "Update Failed";
mysqli_error();
}

}

“测试”部分完美地输出所需的结果,它将$row['amount']除以1.16,然后将下一个乘以0.16。 因此,考虑到输出正确,问题将转向UPDATE部分.-它可能会将amount_before_taxtax列更新为0.00,或者跳过默认值,如mysql中所定义。 / p>

我的表结构如下:

  • id - int(11)AI
  • 金额 - 浮动(10,2)
  • amount_before_tax - float(10,2)
  • tax - float(10,2)

请帮助!

1 个答案:

答案 0 :(得分:1)

您不要在数字字段周围放置单引号:

试试这个:

$query2 = "UPDATE rcpts_exp SET amount_before_tax = ".$a_b_t.", tax = ".$t." WHERE amount = ".$row['amount'];