Page 1 of 1

Fatal error: Call to a member function bind_param() on strin

Posted: Sun Jul 19, 2015 1:03 pm
by anjshr7
Fatal error: Call to a member function bind_param() on string in D:\xamp\htdocs\login-check1\cart_update.php on line 17......
Below is phpcode please help me to figure it out....its urgent.....

***** PLEASE USE PHP CODE TAG *****

Code: Select all

<?php
session_start();
include_once("config.php");

//add product to session or create new one
if(isset($_POST["type"]) && $_POST["type"]=='add' && $_POST["product_qty"]>0)
{
	foreach($_POST as $key => $value){ //add all post vars to new_product array
		$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
    }
	//remove unecessary vars
	unset($new_product['type']);
	unset($new_product['return_url']); 
	$product_code = $_POST['product_code'];
 	//we need to get product name and price from database.
    $statement = "SELECT product_name, price FROM products WHERE product_code='$product_code' LIMIT 1";
    $statement->bind_param('s', $new_product['product_code']);
    $res = mysql_query($statement);
    $statement->bind_result($product_name, $price);
	
	while($statement = mysql_fetch_object($res)){
		
		//fetch product name, price from db and add to new_product array
        $new_product["product_name"] = $product_name; 
        $new_product["product_price"] = $price;
        
        if(isset($_SESSION["cart_products"])){  //if session var already exist
            if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check item exist in products array
            {
                unset($_SESSION["cart_products"][$new_product['product_code']]); //unset old array item
            }           
        }
        $_SESSION["cart_products"][$new_product['product_code']] = $new_product; //update or create product session with new item  
    } 
}


//update or remove items 
if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
{
	//update item quantity in product session
	if(isset($_POST["product_qty"]) && is_array($_POST["product_qty"])){
		foreach($_POST["product_qty"] as $key => $value){
			if(is_numeric($value)){
				$_SESSION["cart_products"][$key]["product_qty"] = $value;
			}
		}
	}
	//remove an item from product session
	if(isset($_POST["remove_code"]) && is_array($_POST["remove_code"])){
		foreach($_POST["remove_code"] as $key){
			unset($_SESSION["cart_products"][$key]);
		}	
	}
}

//back to return url
$return_url = (isset($_POST["return_url"]))?urldecode($_POST["return_url"]):''; //return url
header('Location:'.$return_url);

?>

Re: Fatal error: Call to a member function bind_param() on s

Posted: Sun Jul 19, 2015 1:32 pm
by Christopher

Code: Select all

    $statement = "SELECT product_name, price FROM products WHERE product_code='$product_code' LIMIT 1";
    $statement->bind_param('s', $new_product['product_code']);
The error message is exactly correct, you are attempting to call the method bind_param() on a string. What you want to do is create a mysqli object and then call the prepare() method to create a mysqli statement. Then you can bind parameters and execute the statement.

See: http://php.net/manual/en/mysqli-stmt.execute.php

Re: Fatal error: Call to a member function bind_param() on s

Posted: Sun Jul 19, 2015 8:58 pm
by anjshr7
Can you tell me in mysql_query ..... i am using mysql_query not mysqli ..... Can i use the code of mysqli in mysql

Re: Fatal error: Call to a member function bind_param() on s

Posted: Mon Jul 20, 2015 12:01 am
by Christopher
You cannot mix mysql and mysqli. Use mysqli because the mysql extension is no longer supported. See the link to the documentation above. The documentation has example code you can copy and use.