php - Mysqli update throwing Call to a member function bind_param() error -
hi have 70/80 field form need insert table instead of manually creating 1 huge insert statement firstly have created table in db names of inputs in form here code use create/alter table
function createtable($array, $membermysqli) { foreach ($array $key => $value) { //echo "<p>key: ".$key." => value: ".$value . "</p>"; $query = "alter table questionnaire add ".$key." text"; if($stmt = $membermysqli->prepare($query)) { $success = $stmt->execute(); } } echo "<h1>array count: ". count($array) ."</h1>" ; }
this works fine , altered table how wanted it. insert form values basic 1 field insert store id of row , have loop post variables updating row. here code that:
$stmt = $membermysqli->prepare("insert questionnaire(userid) values (?)"); $stmt->bind_param('s', $_post['userid']); $stmt->execute(); $rowid = $stmt->insert_id; $stmt->close(); $membermysqli->autocommit(false); function updatecolumn($membermysqli, $query, $uid, $value) { if ($value) { $stmt = $membermysqli->prepare($query); //throws bind param error here $stmt->bind_param("ss", $value, $uid); $stmt->execute(); } } function loopinputs($array, $membermysqli, $rowid) { foreach ($array $key => $formvalue) { var_dump($key); updatecolumn($membermysqli, "update questionnaire set $key = ? id = ?", $rowid, $formvalue); } } loopinputs($_post, $membermysqli, $rowid); $membermysqli->commit(); $membermysqli->close();
this throws bind param error , have no idea why. great.
o, let's try canonical answer.
call member function
(or expects parameter 1 mysqli_result, boolean given
procedural style) not error symptom, other problem.
error message means no object created should.
so - there problem creating $stmt
object.
it's problem query. so, need track error down.
mysqli won't tell what's going on unless asked explicitly. so, have check result of every mysqli function interacting server , if result false - check $mysqli->error
.
it important convert mysqli error message php error, let go according site-wide error reporting settings.
if using mysqli_query() on application code without encapsulating helper class, trigger_error()
way raise php error, tell file , line number error occurred
so, prepare()
, execute() , query()
calls have written way:
$stmt = $mysqli->prepare($query) or trigger_error($mysqli->error."[$query]");
or in procedural style
$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");
in scripts
, since notified of reason, why object weren't created. (if you're curious of or
syntax, i've explained here) note query included in error message, let inspect visually , test in environment.
however, if you're encapsulating query class, file , line trigger error quite useless point call itself, not application code caused problem. so, when running mysqli commands encapsulated, way have used:
$result = $mysqli->query($sql); if (!$result) { throw new exception($mysqli->error." [$query]"); }
as exception provide stack trace, lead the place erroneous query called.
note have able see php errors in general. on live site have peek error logs, so, settings have be
error_reporting(e_all); ini_set('display_errors',0); ini_set('log_errors',1);
while on local development server it's right make errors on screen:
error_reporting(e_all); ini_set('display_errors',1);
and of course should never ever use error suppression operator (@) in front of statements.
Comments
Post a Comment