0
$stmt2 = $db->prepare("INSERT INTO friend(`uId`,`friendId`) VALUES (?,?), VALUES(?,?)");
$stmt2->bind_param('ssss', $userId,$friendId,$friendId,$userId);

I expect it would insert 2 rows with the result

uId friendId
1    2
2    1

but it returned

Call to a member function bind_param() on a non-object

1

2 Answers 2

1

INSERT takes only one VALUES clause, even if the VALUES clause includes multiple tuples.

INSERT INTO friend(`uId`,`friendId`) VALUES (?,?), (?,?)

But more importantly, you should always check the return value of prepare() because it returns false if there's an error in your query. Of course you cannot call a the bind_param() method on a false value, because false is not an object with methods at all.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried your format but how is the bind param look like? this $stmt2->bind_param('ssss', $userId,$friendId,$friendId,$userId); doesn't work
Did you check for errors as I suggested? What error was output? See an example here: php.net/manual/en/mysqli.error.php
yes I tried your answer but it return Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement
0

Try this

$a = '';
$b = '';

$stmt2 = $db->prepare("INSERT INTO friend(`uId`,`friendId`) VALUES (?,?)");
$stmt2->bind_param('ss', $a, $b);

$a = $userId;
$b = $friendId;
$stmt2->execute();

$a = $friendId;
$b = $userId;
$stmt2->execute();

1 Comment

@user3522457 you're using mysqli or pdo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.