0

I have a pretty simple query to execute. I am inserting a bunch of parts into a package, and then I save my package, get its ID, and then update all my parts to put the package ID into the appropriate field.

Here is how I build my list of IDs that need to be updated:

$IDs = [];
foreach($packageContent as $value) {
    $IDs[] = $value->ID;
}

Then, I build my query and execute it:

$statement="UPDATE tbInv 
            SET
                nPackage=:packID
            WHERE nID IN( :IDsToUpdate )";

$res = $db->prepare($statement);
$res->execute(array(":packID" => $packageID, ":IDsToUpdate" => implode(",", $IDs)));

I am putting two items in my package. ID 38 and 39. If I do a file_put_contents to trace my implode(",", IDs), I can clearly see 38,39. The problem: it only updates the row #38. Row #39 does NOT get updated.

Can somebody point me to my (probably obvious) mistake? It just doesn't make any sense to me, I'm used to MS SQL Server and a .net windows application... Web development is new to me, let alone PHP.

Thank you,

Mathieu Turcotte
  • 344
  • 2
  • 14
  • Forgot to say. This php file gets called through Ajax, which makes debugging it harder as the page is never actually shown on screen... – Mathieu Turcotte Nov 10 '16 at 19:24
  • Although the latter duplicate is not as upvoted as the 1st one, it offers a more secure solution. – Shadow Nov 10 '16 at 19:26
  • Just because the request is generally made via ajax doesn't mean you can't also make the request some other way (directly via the browser if it accepts GET requests or using one of many browser plugins that allow making POST requests). You can also view the response of ajax requests via your browsers network tab of the developer tools. – Patrick Q Nov 10 '16 at 19:40
  • it appears the problem was the implode. it add single quotes around it so the WHERE looked like: WHERE nID IN ('38,39') instead of WHERE nID IN (38,39) I worked around it using concatenations, I'm not a fan of this, but it works. Thanks for the ones who pointed me to the other similar questions, they solved the issue. – Mathieu Turcotte Nov 10 '16 at 20:53

0 Answers0