/    Sign up×
Community /Pin to ProfileBookmark

Passing array into method

I’m posting an array of User ID’s which I want to save into a database.

My array is working, here’s a var_dump

>

array(2) { [0]=> string(3) “108” [1]=> string(2) “47” }_

Here’s an error

Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /home/public_html/user.php:176 Stack trace: #0 /home/public_html/user-update.php(19): User->updateTable(Array) #1 {main} thrown in /home/public_html/user.php on line 176

[code]
$User->updateTable(array($Users));
[/code]

[code]
public function updateTable(array $POSTarray){

$Db = new Dbh;
$con = $Db->connect();

foreach ($POSTarray as $p => $myId){
$sql = “INSERT INTO `mytable` (id`, timestamp`, `userId`) VALUES (?, ?, ?)”;
$stmt = $con->prepare($sql);
$id = $this->id;
$time = time();
$stmt->bind_param(‘iii’, $id, $time, $myId);
$stmt->execute();
}

return true;
}
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 18.2021 — I think you just want to call it as...
[code=php]
$User->updateTable($Users);
[/code]

...since presumably $Users is already an array?
Copy linkTweet thisAlerts:
@kiwisauthorAug 18.2021 — @NogDog#1635704

I'd tried that. Still not better.

when I var_dump($array) I get this

array(2) { [0]=> string(3) "108" [1]=> string(2) "47" }
Copy linkTweet thisAlerts:
@kiwisauthorAug 18.2021 — The issue is acutally with my SQL

I'm getting this error

> Errormessage: Commands out of sync; you can't run this command now

[code]
foreach ($users as $u => $userId){
$sql = "INSERT INTO mytable ('id', 'timestamp', 'userId) VALUES (?, ?, ?)";
if ($stmt = $con->prepare($sql)) {
$id= $this->id;
$time = time();
$stmt->bind_param('iii', $id, $time, $userId);
} else {
die("Errormessage: ". $con->error);
}

$stmt->execute();
$stmt->free();
$stmt->close();
}

Copy linkTweet thisAlerts:
@NogDogAug 19.2021 — Ugh...MySQLi? Should that $stmt->free(); be $stmt->free_result();? From what I'm seeing, that _might_ be the problem if the query executes once in the first iteration of the loop, and then you get that error in the second iteration. But I'm not at all sure.

Can I convince you to switch from MySQLi to PDO? ;)
×

Success!

Help @kiwis 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 4.26,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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