我有以下 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。
因此,考虑到输出正确,问题就转向更新部分。-它可能会将 amount_before_tax
和 tax
列更新为 0.00
或跳过作为 mysql 中定义的默认值。
我的表的结构如下:
- id - int(11) AI
- 金额 - float (10,2)
- 税前金额 - float (10,2)
- 税 - float (10,2)
请帮忙!
最佳答案
您不要在数字字段周围放置单引号:
试试这个:
$query2 = "UPDATE rcpts_exp SET amount_before_tax = ".$a_b_t.", tax = ".$t." WHERE amount = ".$row['amount'];
关于PHP 更新现有记录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20214829/