Jump to content
funbinod

binding param to prepared statement

Recommended Posts

hello all!

i'm trying something for binding parameters to a prepared statement like below.

$tbl = 'stock';
$cols = 'icode=?, item=?, catid=?, description=?, unitid=?, pprice=?, sprice=?, oqty=?, oprice=?, oamt=?, bcode=?';
// $vals = "'101', 'ASUS H61M-E', 'abcde', '2', '1', '5300.00', '6500.00', '11', '5300', '55528', '101'";
$vals = "101, ASUS H61M-E, abcde, 2, 1, 5300.00, 6500.00, 11, 5300, 55528, 101";
$uq = $mysqli->prepare("UPDATE $tbl SET $cols WHERE $whcol='$whval'");
if (!$uq) {
	$msg = "Error1: $mysqli->error!";
} else {
	$uq->bind_param('sssssssssss', $vals);  // this doesn't work.
//	$uq->bind_param('sssssssssss', 101, ASUS H61M-E, abcde, 2, 1, 5300.00, 6500.00, 11, 5300, 55528, 101); // but this works well.
	if (!$uq->execute()) {
		$msg = "Error3: $uq->error!"; ///// this says "No data supplied for parameters in prepared statement."
	} else {
		$msg = "UPDATED successfully!";
	}
}

here i'm trying to put all the update values into a string and then bind it to the statement. but this says "No data supplied for parameters in prepared statement". but when i use individual values, it works fine. please suggest me something.

 

thanks in advance.

Share this post


Link to post
Share on other sites

mysqli_stmt::bind_param binds each variable to a matching parameter. You cannot match all of them to one. It doesn't work like that.

(Neither can you attempt to use a string like an array)

What you can do however, is bind an array using variable length argument lists. Use ... to indicate an argument list as below.

<?php
$vals = [101, "ASUS H61M-E", "abcde", 2, 1, 5300.00, 6500.00, 11, 5300, 55528, 101];
$uq = $mysqli->prepare("UPDATE $tbl SET $cols WHERE $whcol='$whval'");
if (!$uq) {
	$msg = "Error1: $mysqli->error!";
} else {
	$uq->bind_param('issiiddiiii', ...$vals);

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...