user3159187
user3159187

Reputation: 39

Not working mysql query with prepared statements

I'm trying to understand where I am wrong with this query..

$query = "INSERT INTO turni (data, utenteId, turno_ceduto, tipo_turno_ceduto,
turno_cercato, 
note, date) VALUES (?, ?, ?, ?, ?, ?, NOW())";

$stmt = mysqli_prepare($dbc,$query);
mysqli_stmt_bind_param($stmt,'sissss', $data, $utenteId, $turno_ceduto,
$tipo_turno_ceduto, $turno_cercato, $note);

mysqli_stmt_execute($stmt);
$count = mysqli_stmt_affected_rows($stmt);

Why is $count -1??

Upvotes: 0

Views: 47

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562240

You should always check for errors after each prepare() and execute(), and report the errors.

Here's an example:

if (($stmt = mysqli_prepare($dbc,$query)) === false) {
    trigger_error(mysqli_error($dbc), E_USER_ERROR);
}
mysqli_stmt_bind_param($stmt,'sissss', $data, $utenteId, $turno_ceduto,
    $tipo_turno_ceduto, $turno_cercato, $note);

if (mysqli_stmt_execute($stmt) === false) {
    trigger_error(mysqli_stmt_error($stmt), E_USER_ERROR);
}
$count = mysqli_stmt_affected_rows($stmt);

Upvotes: 0

sectus
sectus

Reputation: 15454

Question

Why is $count -1??

Answer

-1 indicates that the query has returned an error

mysqli_stmt_affected_rows

You shoud check errors with mysqli_error, it's returns a string description of the last error.

You get the error description from the last mysqli-function, not from the last mysql-error.

Upvotes: 1

Related Questions