我正在尝试使用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']}");
答案 0 :(得分:1)
$conn->query("update inventory set cost = ?, csmt = ?, hold = ?, commission_level = ?, date_process = ? where sku = ?");
$conn->bindParam("ssssss", $cost, $csmt, $data[$n]['hold'], $clvl, $today, $data[$n]['sku']);
$conn->execute();
您需要将s
(在bindParam中)更改为每个变量的相应数据类型
答案 1 :(得分:0)
经过多次试验和错误后,修正后的代码效果很好。
$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']> 1 || $data[$n]['csmt'] == 1) {
//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]);
}
}
// update inventory table
$conn->query("update inventory set cost = $cost, csmt = {$data[$n]['csmt']}, hold = {$data[$n]['hold']}, commission_level = " . $cLvl .",
date_process = '$today' where sku = {$data[$n]['sku']}");
$holdList[$n] = array('buyer' => $data[$n]['buyer'],
'process date' => $today,
);
$n++;
} else {
$n++; }// end of if statement
}
我只想给出最终答案,以防其他人遇到同样的问题。
答案 2 :(得分:-1)
根据您尝试更新的数据类型(INT,STRING,VARCHAR或其他),某些数据可能需要先格式化才能发送到数据库。
如果$ csmt引用来自用户的TEXT或VARCHAR或项目或某事物,则文本可能包含引号或其他字符,这些字符需要在发送到数据库之前正确格式化。如果发送到数据库的命令的一部分无效,则数据库将不接受该数据。
很高兴知道你的变量代表什么类型的数据类型,但假设你的$ csmt是某种类型的TEST,你将需要使用
这将编码字符串值 - >
$value = mysql_real_escape_string( $csmt ); // Formats the string for database storage
应使用和$ value代替$ csmt
这将解码字符串值 - >
$todisplay = htmlspecialchars( $csmt, ENT_QUOTES ); // $csmt from database
// $todisplay = the value to display on your page.