0

我正在尝试使用 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']}");

如果我只是添加成本,那么表格将更新,但如果我更改csmthold字段,则不会更新任何内容。

如果我对 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']}");
4

3 回答 3

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 中)更改为每个变量的相应数据类型

于 2012-08-03T17:54:34.013 回答
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

}

我只是想给出最终答案,以防其他人有同样的问题。

于 2012-08-06T16:22:55.997 回答
-1

根据您尝试更新的数据类型(INT、STRING、VARCHAR 或其他),可能需要对某些数据进行格式化,然后才能将其发送到数据库。

如果 $csmt 引用来自用户的 TEXT 或 VARCHAR 或项目或其他内容,则文本可能包含引号或其他字符,这些字符在发送到数据库之前需要正确格式化。如果发送到数据库的部分命令无效,数据库将不接受数据。

很高兴确切知道您的变量代表哪种数据类型,但假设您的 $csmt 是某种类型的测试,您将需要使用

这将对字符串值进行编码 ->

$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.
于 2012-08-03T18:04:21.077 回答