-1

I want to update a table but i keep getting the error: Call to a member function bind_param() on boolean

What am i doing wrong? In my db setting looks like this:

  '{"name":"Product Slider","product_name":"","product":["240","375","5555"],"limit":"5","width":"600","height":"400","loop":"1","auto":"1","pager":"1","pause":"3000","speed":"800","status":"1"}'

My attempt to update:

 $sliderSql=$con->prepare("UPDATE oc_module SET setting = '{'name':'Product Slider','product_name':'','product':[?],'limit':'5','width':'600','height':'400','loop':'1','auto':'1','pager':'1','pause':'3000','speed':'800','status':'1'}' WHERE oc_module.module_id = 95");

$param="'".$calcSlider[0]."','".$laptopSlider[0]."','".$serverSlider[0]."'";

$sliderSql->bind_param("s",$param);
user3844579
  • 475
  • 7
  • 27
  • use `var_dump($sliderSql);` to see what it's made of. – castis Mar 15 '17 at 18:59
  • Your prepare is failing because your query syntax isn't correct. Hint: look at he quotes in your JSON string. – Jay Blanchard Mar 15 '17 at 18:59
  • @castis sliderSql is a bool(false). – user3844579 Mar 15 '17 at 19:06
  • @JayBlanchard if i use this (from phpmyadmin) it still fails: setting` = \'{\"name\":\"Product Slider\",\"product_name\":\"\",\"product\":[\"240\",\"375\",\"5555\"],\"limit\":\"5\",\"width\":\"600\",\"height\":\"400\",\"loop\":\"1\",\"auto\":\"1\",\"pager\":\"1\",\"pause\":\"3000\",\"speed\":\"800\",\"status\":\"1\"}\' – user3844579 Mar 15 '17 at 19:07
  • Now check out what [mysqli::prepare](http://php.net/manual/en/mysqli.prepare.php) says under 'Return Values'. Your update statement is not properly formatted SQL. Maybe try putting that whole json payload in some escaped double quotes. – castis Mar 15 '17 at 19:07
  • Possible duplicate of [Fatal error: Call to a member function bind\_param() on boolean](http://stackoverflow.com/questions/27394710/fatal-error-call-to-a-member-function-bind-param-on-boolean) – dorukayhan Mar 15 '17 at 19:48

1 Answers1

1

The quotes in your SQL are wrong. The single quotes in your JSON are terminating the string. Quotes in JSON have to be double quotes. Since they're embedded inside a string literal, you need to escape them.

$sliderSql=$con->prepare("UPDATE oc_module SET setting = '{\"name\":\"Product Slider\",\"product_name\":\"\",\"product\":[?],\"limit\":\"5\",\"width\":\"600\",\"height\":\"400\",\"loop\":\"1\",\"auto\":\"1\",\"pager\":\"1\",\"pause\":\"3000\",\"speed\":\"800\",\"status\":\"1\"}' WHERE oc_module.module_id = 95");

But there's another problem. You can't put a ? parameter inside a string literal -- they can only be used where a value expression is allowed.

It would be easier if you used a separate variable, which you converted to JSON using json_encode().

$setting = json_encode(array(
     "name" => "Product Slider",
     "product_name" => "",
     "product" => array($calcSlider[0], $laptopSlider[0], $serverSlider[0]),
     "limit" => "5",
     "width" => "600",
     "height" => "400",
     "loop" => "1",
     "auto" => "1",
     "pager" => "1",
     "pause" => "3000",
     "speed" => "800",
     "status" => "1"));
$sliderSql = $con->prepare("UPDATE oc_module SET setting = ?");
$sliderSql->bind_param("s", $setting);
Barmar
  • 669,327
  • 51
  • 454
  • 560