1

if anyone can help me with the problem that is happening, I am very grateful. The error would be with bind_param () using PDO connection.

Fatal error: Call to undefined method PDOStatement::bind_param() in line25

setlocale(LC_MONETARY,"en_US");
if(isset($_POST["id"])) {
foreach($_POST as $key => $value){
    $product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}   

$statement = app('db')->prepare("SELECT as_produtos.nome, as_produtos.preco FROM as_produtos WHERE id = ? LIMIT 1");
$statement->bind_param('s', $product['id']);
$statement->execute();
$statement->bind_result($product_name, $product_price);

while($statement->fetch()){ 
    $product["product_name"] = $product_name;
    $product["product_price"] = $product_price;     
    if(isset($_SESSION["products"])){ 
        if(isset($_SESSION["products"][$product['id']])) {              
            $_SESSION["products"][$product['id']]["product_qty"] = $_SESSION["products"][$product['id']]["product_qty"] + $_POST["product_qty"];                
        } else {
            $_SESSION["products"][$product['id']] = $product;
        }           
    } else {
        $_SESSION["products"][$product['id']] = $product;
    }   
}   
$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}

if(isset($_GET["remove_code"]) && isset($_SESSION["products"])) {
$product_code  = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING);
if(isset($_SESSION["products"][$product_code])) {
    unset($_SESSION["products"][$product_code]);
}   
$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}

if(isset($_GET["update_quantity"]) && isset($_SESSION["products"])) {   
if(isset($_GET["quantity"]) && $_GET["quantity"]>0) {       
    $_SESSION["products"][$_GET["update_quantity"]]["product_qty"] = $_GET["quantity"]; 
}
$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}
3
  • 1
    check this post if helps you stackoverflow.com/questions/18702772/… Commented Aug 11, 2019 at 21:43
  • bind_param() is from mysqli, not PDO. bindParam() is what you want. But check the manual, because it doesn't work quite the same way Commented Aug 11, 2019 at 22:18
  • Thanks for the info, it helped a lot. Commented Aug 13, 2019 at 3:43

2 Answers 2

2

I guess you meant to call bindParam.
Also there's no function called bind_result (Check: What is the equivalent of bind_result on PDO).
Furthermore, you trying to bind param (':s'), but there's '?' in your request string.

$statement = app('db')->prepare("SELECT as_produtos.nome, as_produtos.preco FROM as_produtos WHERE id = :s LIMIT 1");  
$statement->bindParam(':s', $product['id']);

Or use bindValue.

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

1 Comment

Thanks for the help friend, I solved the problem with your explanation and adjusting the lines below, thank you very much.
0

Correct code (Shopping Cart) ok:

setlocale(LC_MONETARY,"pt_BR");

# add products in cart 
if(isset($_POST["id"])) {
foreach($_POST as $key => $value) :
$product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
endforeach; 

 $statement = app('db')->prepare("SELECT as_produtos.nome, as_produtos.preco FROM 
 as_produtos WHERE id = :id LIMIT 1");
 $statement->bindParam(':id', $product['id'], PDO::PARAM_INT);
 $statement->execute();

 foreach($statement as $dados_produto) :

$product["product_name"] = $dados_produto["nome"];
$product["product_price"] = $dados_produto["preco"];

if(isset($product["product_name"])){ 

    if(isset($_SESSION["products"][$product['id']])) {              
        $_SESSION["products"][$product['id']]["product_qty"] = $_SESSION["products"][$product['id']]["product_qty"] + $_POST["product_qty"];

    } else {
        $_SESSION["products"][$product['id']] = $product;
    }   

} else {
    $_SESSION["products"][$product['id']] = $product;
}   

endforeach;

$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}

# Remove products from cart
if(isset($_GET["remove_code"]) && isset($_SESSION["products"])) {
$product_code  = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING);
if(isset($_SESSION["products"][$product_code])) {
unset($_SESSION["products"][$product_code]);
}   
$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}

# Update cart product quantity
if(isset($_GET["update_quantity"]) && isset($_SESSION["products"])) {   
if(isset($_GET["quantity"]) && $_GET["quantity"]>0) {       
$_SESSION["products"][$_GET["update_quantity"]]["product_qty"] = 
$_GET["quantity"];  
}
$total_product = count($_SESSION["products"]);
die(json_encode(array('products'=>$total_product)));
}   

Comments

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.