1

No idea why this won't work. I do a SELECT on the same database following the exact same procedure (ie prepare,bind,execute). But the update for some reason will not update in the the DB.

Here is the code :

$stmtUpdate = $mysqli->prepare("UPDATE license_keys.$injection_key_text_type 
SET given_date = CURRENT_DATE(), contact_email = ?, given_bool = ?, contact_id = ?    
WHERE key_text_id = ?;";
$stmtUpdate->bind_param("siss",$recipient_email,1,$contact_id,$key_text_id);
$stmtUpdate->execute();
$stmtUpdate->close();

Things I've tried

  • Wrapping the first line in an if statement
  • Switched given_date = CURRENT_DATE to given_date = ? then just added an arbitrary string to my bind params ie ($stmtUpdate->bind_param("ssiss","11-8-2014",$recipient_email,1,$contact_id,$key_text_id);
  • tried it with and without the ";" at the end of the statement because it works with the procedural version but not the OO way.

Also Note if I run it as procedural it works just fine, but I need it to work using prepared statements. Here is the procedural code:

$mysqli->query("UPDATE license_keys.$injection_key_text_type 
SET given_date = CURRENT_DATE(), contact_email = '$recipient_email', given_bool = 1, contact_id = '$contact_id' 
WHERE key_text_id = '$key_text_id';");
EternalHour
  • 8,308
  • 6
  • 38
  • 57

1 Answers1

1

Looks to me based on the variable names that you need several of those placeholders to be integers rather than a string if they are in fact INT.

$stmtUpdate->bind_param("siii",$recipient_email,1,$contact_id,$key_text_id);

For debugging purposes, I would recommend you do this. $mysqli will be your db connection. Hopefully it gives you an idea of what's wrong.

if (!($stmtUpdate = $mysqli->prepare("UPDATE license_keys.$injection_key_text_type 
SET given_date = CURRENT_DATE(), contact_email = ?, given_bool = ?, contact_id = ?    
WHERE key_text_id = ?")) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    exit;
}

if (!$stmtUpdate->bind_param("sisi",$recipient_email,1,$contact_id,$key_text_id)) {
    echo "Binding parameters failed: (" . $stmtUpdate->errno . ") " . $stmtUpdate->error;
    exit;
}

if (!$stmtUpdate->execute();) {
    echo "Execute failed: (" . $stmtUpdate->errno . ") " . $stmtUpdate->error;
    exit;
}
EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • Very good suggestion, I did not even think of that. So that did give me a better idea what may be causing it now, because $key_text_id is an int so I made that change, but oddly enough $contact_id is being stored as an varchar?? I'm thinking it might have something to do with the `given_date = CURRENT_DATE()` function now. Because my test page isn't even spitting out the html I originally put at the top of my script. Thank you very much for this suggestion – Brandon 10x Nov 08 '14 at 18:17
  • Is the `given_date` column type `DATE`, `DATETIME`, or `TIMESTAMP`? – EternalHour Nov 08 '14 at 18:22
  • `given_date` is type `date` in database – Brandon 10x Nov 08 '14 at 18:28
  • Ok, you're good there. Have you tried `WHERE key_text_id = ?";`? – EternalHour Nov 08 '14 at 18:34
  • Yeah I gave that a try with no luck. Question, on this line here `$stmtUpdate->bind_param("ssisi","11-8-14",$recipient_email,1,$contact_id,$key_text_id);` does that 1 need to be in quotes? I tried it but it didn't make a difference, I'm just wondering if that is some standard maybe? – Brandon 10x Nov 08 '14 at 18:49
  • Since it's an integer, it doesn't need quotes. – EternalHour Nov 08 '14 at 18:50
  • Gotcha. Thanks. Hmmm... this is still quite the stumper not sure why this won't work. Thanks for all your help EternalHour – Brandon 10x Nov 08 '14 at 18:53