0

I am trying to update information in a table using MySQLI and PHP. I know that the data is getting to the update section because I have used print_r() and var_dump() in many spots to verify something is there, but nothing gets changed in the table. Here is what I have for my update:

$conn->query("update inventory set cost = $cost, csmt = $csmt, 
    hold = {$data[$n]['hold']}, commission_level = " . $cLvl .",
    date_process = '$today' where sku = {$data[$n]['sku']}");

If I just add a cost then the table will update, but if I change the csmt or hold fields, nothing gets updated.

If I hardcode the values in the a MySQL query, it works fine. So it appears that there is some problem with the PHP code.

Anyone see where I am going wrong with this?

$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']}");
2
  • That all depends on what the values (types) of those variables are, and what mysql expects them to be.
    – Matt
    Aug 3, 2012 at 17:50
  • @Matt all values passed to mysql are integers (ie 10.00 for cost, 1 for csmt and 0 for hold) and mysql is looking for double(cost) tinyint(csmt) and int(hold)
    – Jim
    Aug 3, 2012 at 17:55

3 Answers 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();

you'll need to change the s (in bindParam) to the according data type of each variable

0

After much trial and error here is the corrected code that works great.

    $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

}

I just like to give the final answer in case someone else has the same issue.

-1

Depending on the type of data you're trying to update, (INT, STRING, VARCHAR or other), some data may need to be formatted before it can be sent to the database.

If $csmt refers to TEXT or VARCHAR from a user OR an item OR something, then the text may include quotes or other characters which need to be formatted properly before sending to the database. IF part of the command being sent to the database is invalid, the database will not accept the data.

It would be nice to know exactly what kind of data-types your variable represent, but assuming your $csmt is a TEST of some sort you will need to use

This will code the string value ->

$value = mysql_real_escape_string( $csmt ); // Formats the string for database storage

and $value should be used instead of $csmt

This will decode the string value ->

$todisplay = htmlspecialchars( $csmt, ENT_QUOTES ); // $csmt from database
// $todisplay = the value to display on your page.
1
  • 1
    you onlu need to do this for mysql_query which are being depreciated. He is using mysqli, there mysql_escape_string is irrelevant
    – mlishn
    Aug 3, 2012 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.