Jump to content

binding param to prepared statement


funbinod

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.

Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...