Error with bind_param() with mysql PDO connection

Asked

Viewed 79 times

0

if anyone can help me error in the script I really appreciate, the error is as follows:

Fatal error: Call to Undefined method Pdostatement::bind_param() in

the code is this:

    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)));
    }

2 answers

0

Follow the code (shopping cart) with the solution forwarded by friend Artsher Developer.

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)));
}   

0


  • 2

    Thank you friend, your reply was of great help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.