i have an update function that updates a specific row in the table. The sql query works in PHPmyadmin and I am always returned true on the function. However, the database is not updated. I looked over the code and nothing appears wrong. What could be the problem. primary_id
is the primary id of the table and the only other column is fund_max
.
function change_fund_max ($mysqli, $project_id, $fund_max) {
if ($stmt = $mysqli->prepare("UPDATE `project_fund_max` SET `fund_max` = ? WHERE
`project_id` = ?")){
$stmt->bind_param('ii', $project_id, $fund_max);
$return = $stmt->execute();
$stmt->close();
return $return;
} else {return false;}
}
Here is the use of the function.
$fund_max = 11.55;
$project_id = 1;
$row43 = change_fund_max ($mysqli, $project_id, $fund_max);
var_dump($row43);
Execute will only return false
on a failure... like you messed up the SQL or something. What you really want to know is did the command actually update on the rows... check mysqli_stmt_affected_rows
to see if more than zero rows where changed.
Also your project_id
and fund_max
parameters are the wrong way around in your bind
;)
Result:
function change_fund_max ($mysqli, $project_id, $fund_max) {
if ($stmt = $mysqli->prepare("UPDATE `project_fund_max` SET `fund_max`=? WHERE
`project_id` = ?")){
$stmt->bind_param('di', $fund_max, $project_id);
if (!$stmt->execute()) return false;
$count=$stmt->affected_rows;
$stmt->close();
return ($count>0);
} else {
return false;
}
}
Update: For sneaky "fund_max is a decimal" comment... if fund_max is a decimal you need to bind to a double otherwise you're loosing anything right of the decimal place during the bind_param
command (my code updated)
A wild guess, but it seems the value passed in $project_id
does not exist in the database.
That would make the UPDATE
command run, not fail, but also not update any record.
another guess, project_id
and/or fund_max
may not be both integers. you may check in the manual
Also, the names of the columns shouldn't be surrounded by quotes(`), unless its present in the name itself, in the update query.
Edit:
function change_fund_max ($mysqli, $project_id, $fund_max) {
if ($stmt = $mysqli->prepare("UPDATE project_fund_max SET fund_max = ? WHERE
project_id = ?")){
$stmt->bind_param('di', $fund_max, $project_id);
$return = $stmt->execute();
$stmt->close();
return $return;
} else {return false;}
}