我正在尝试使用 MySQLI 和 PHP 更新表中的信息。我知道数据正在进入更新部分,因为我已经在许多地方使用print_r()
并var_dump()
验证了某些东西是否存在,但表中没有任何变化。这是我的更新:
$conn->query("update inventory set cost = $cost, csmt = $csmt,
hold = {$data[$n]['hold']}, commission_level = " . $cLvl .",
date_process = '$today' where sku = {$data[$n]['sku']}");
如果我只是添加成本,那么表格将更新,但如果我更改csmt
或hold
字段,则不会更新任何内容。
如果我对 MySQL 查询中的值进行硬编码,它可以正常工作。因此,PHP代码似乎存在一些问题。
任何人都知道我在哪里出错了吗?
$today = date('Y-m-d');
$data = json_decode($_POST['data'], true);
foreach ($data as $value) {
// check to see if it was taken off hold
if ($data[$n]['cost']> 0 || $data[$n]['csmt'] !== 0) {
//update commission information
//gather data
$result = $conn->query("SELECT book_code FROM book_type WHERE book_type_id = {$data[$n]['booktype']}");
if ($result) { $bookType = $result->fetch_assoc(); }
$result = $conn->query("SELECT book_type_3 FROM feature WHERE feature_id = {$data[$n]['featureid']}");
if ($result) { $bookType3 = $result->fetch_assoc(); }
$result = $conn->query("SELECT condition_id FROM defect WHERE defect_id = {$data[$n]['defect']}");
if ($result) { $cId = $result->fetch_assoc(); }
$result = $conn->query("select source_id from inventory where sku = {$data[$n]['sku']}");
if ($result) { $buyer = $result->fetch_assoc(); }
$result = $conn->query("SELECT class_code, notes, price, usedbuying_price FROM follette_title WHERE isbn13 = {$data[$n]['isbn']}");
if ($result) {
$follett = $result->fetch_assoc();
$follett_price = $follett['usedbuying_price'];
}
if ($data[$n]['cost'] == 0 || $bookType3['book_type_3'] == "DMG")
{
$cLvl = null;
}
elseif ($bookType['book_code'] == "IE" || $bookType['book_code'] == "AIE")
{
$cLvl = 3;
}
elseif ($cId['condition_id'] == 1 || $cId['condition_id'] == 2 || $cId['condition_id'] == 3 || $cId['condition_id'] == 4 || $follett['class_code'] == "D" || strpos($follett['notes'],"(T)") == true)
{
$cLvl = 2;
}
elseif ($cId['condition_id'] == 5)
{
$cLvl = 1;
}
if ($data[$n]['csmt'] == 0) {
//get buyer commission rate
if (!empty($cLvl))
{
$result = $conn->query("SELECT lvl" . $cLvl . " FROM buyers WHERE buyer_id = {$buyer['source_id']}");
//echo"SELECT lvl" . $cLvl . " FROM buyers WHERE buyer_id = {$buyer['source_id']}"; die;
if ($result) { $comRate = $result->fetch_assoc(); } //print_r($comRate); die;
}
//calculate final cost
if (!empty($comRate['lvl'.$cLvl]))
{
$cost = ($data[$n]['cost']) + (($data[$n]['cost']) * $comRate['lvl'.$cLvl]);
}
}
$csmt = $data[$n]['csmt'];
$conn->query("update inventory set cost = $cost, csmt = $csmt,
hold = {$data[$n]['hold']}, commission_level = " . $cLvl .",
date_process = '$today' where sku = {$data[$n]['sku']}");