0

I have been staring at a problem for far to long now.

I have a PHP file that is preparing an insert query, binding the params, and executing the query (Simple).

QUERY

$stmt = $db->prepare("SELECT insert_user(?, ?, ?, ?, ?, ?)"); 

For some reason the $stmt object returned by execute returns -1 for the affected rows. If I alter the code to do just an insert query with the values I wanted to bind, hard coded instead, the query works just fine.

HARD CODED QUERY

$db->query("SELECT insert_user('Test', 'Account', '[email protected]', 'testAccount9', '1980-01-01', 1)"); 

Something is going wrong in the bind_param section. I have errors turned on and am checking for mysqli errors too, but both are returning no errors.

PHP FILE

... $postdata = file_get_contents("php://input"); if (isset($postdata) && !empty($postdata)) { $request = json_decode($postdata); } if ( validate_string($request->fname) && validate_string($request->lname) && validate_integer(intval($request->gender))) { $stmt = $db->prepare("SELECT insert_user(?, ?, ?, ?, ?, ?)"); if ($stmt) { $stmt->bind_param("ssssis", $first_name, $last_name, $email, $password, $gender, $dob); $first_name = $request->fname; $last_name = request->lname; $email = $request->email; $password = password_hash($request->password, PASSWORD_BCRYPT); $gender = intval($request->gender); $dob = $request->dob; $stmt->execute(); if ($stmt->affected_rows > 0) { echo toJson('success'); } else { echo toJson('fail'); } } else { echo toJson("Prepare failed: (" . $db->errno . ") " . $db->error); } } else { echo toJson('fail - passed data not valid'); } ... 

I feel the error must be simple at this point, but I have tried at least 23,432 different things to no success.

4

1 Answer 1

2

My guess the problem is here "ssssis" . The fifth should be string, the last integer. Maybe this should work?

Sign up to request clarification or add additional context in comments.

5 Comments

If you look at the value put into the 5th bind (gender) the value is set using $gender = intval($request->gender);
@Shir Gans I am passing a integer value for gender in my DB. Date is a string converted to a DATE by the insert_user function.
Right, but OP's HARD CODED QUERY, they use gender 1 as the final argument.
OMG, that is it! What a waste of time for you guys sorry!
The insert_function in my DB takes DOB as the 5th param and gender as the last.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.