/    Sign up×
Community /Pin to ProfileBookmark

Prepared Statement Loop Insert issue

Good morning again,

This piece of code has stumped for me the last 4 hours. The first issue I solved on this post and was user error.

The second bit seems more technical which I’m not sure how to fix. As per my other thread. I’m passing in an array to my object and now within a method I want to foreach loop through that array and insert each one into a database.

There may be better ways of achieving this and I’m open to those. I’ve been writing PHP for only a few months so please excuse me.

Here is my code

[code]
$students= $this->studentList;
foreach ($students as $key => $value) {
$stmt3 = $con->stmt_init();
$stmt3->prepare(“INSERT INTO `studentList` (`objectVal0`, `objectVal`, `objectVal2`) VALUES (?, ?, ?)”);
$student = $students[$key];
$stmt3->bind_param(‘iii’, $var1, $var2, $student);
$stmt3->execute();
}
[/code]

When I dump my original object I get this

“`
‘studentList’ =>
array (
0 => ’10’,
1 => ’65’,
),
“`

**My error is :** PHP Fatal error: Uncaught Error: Cannot pass parameter 3 by reference in

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 17.2021 — Which line is failing? (The rest of the error message should tell you, likely after the colon with the file name, e.g. /path/to/some/file.php:123, where 123 would be the line number.) My guess -- and only a guess -- at this point is it's complaining about $var2 in the call to bind_param(), as that looks like the only 3rd parameter in that code.

In any case, based on what you've posted, I see no reason to use $key, or for that matter assigning some the a new variable called $students. You should be able to do:
[code=php]
foreach($this->studentList as $student) {
$stmt3 = $con->stmt_init();
$stmt3->prepare("INSERT INTO studentList (objectVal0, objectVal, objectVal2) VALUES (?, ?, ?)");
$stmt3->bind_param('iii', $var1, $var2, $student);
$stmt3->execute();
}
[/code]

But that would not appear to have anything to do with that error message, as far as I can see.
×

Success!

Help @singaporeDude spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...