0

Below is my code. I get this error

Fatal error: Uncaught Error: Call to a member function bindParam() on bool

<?php 
    require("connectProduct.php");

Get these $_POST from another PHP. Please let me know how can I change?

 if (isset($_POST)){
        $brand_name   = trim($_POST['brandname']);
        $product_type = trim($_POST['type']);
        $product_name = trim($_POST['Prodname']);
        $color        = trim($_POST['color']);
        $image        = trim($_POST['image']);
        $id           = (int) $_POST['id'];
        $quantity     = (int) ($_POST['qty']);
        $price        = (float) $_POST['price'];


    try {
            $sql = 'INSERT INTO items_table(id, brand_name, product_type,name,price,color,image,quantity) 
            VALUES(:id, :brand_name, :product_type, :product_name, :price, :color, :image, :quantity)';
            $stmt = $conn->prepare($sql);

"Uncaught Error: Call to a member function bindParam() on bool" in below line

        $stmt->bindParam(":id", $id);
        $stmt->bindParam(":brand_name", $brandname);
        $stmt->bindParam(":product_type", $prodtype);
        $stmt->bindParam(":product_name", $prodname);
        $stmt->bindParam(":price", $price);
        $stmt->bindParam(":color", $color);
        $stmt->bindParam(":image", $image);
        $stmt->bindParam(":quantity", $qty);
        $stmt->execute();

if ($stmt->rowCount()) {
            header("Location: create.php?status=created");
            exit();
        }
        header("Location: create.php?status=fail_create");
        exit();
    } catch (Exception $e) {
        echo "Error " . $e->getMessage();
        exit();
    }
} else {
    header("Location: create.php?status=fail_create");
    exit();
}
?>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Potato
  • 1
  • 1
  • @Dharman yes im using PHP 7.4 – Potato Aug 07 '21 at 14:15
  • On PHP 7.4 you need to enable PDO error reporting manually. – Dharman Aug 07 '21 at 14:17
  • @Dharman what code should i add in – Potato Aug 07 '21 at 14:18
  • Read the answer I gave you carefully. It's all there. – Dharman Aug 07 '21 at 14:18
  • @Dharman i get this error after adding this code $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); The error: Fatal error: Uncaught Error: Call to undefined method mysqli::setAttribute() – Potato Aug 07 '21 at 14:27
  • Prepare returned false so there was an error in the sql or with the connection. – Josh J Aug 07 '21 at 14:28
  • Are you using mysqli or PDO then? Your code is PDO code, but your error states that your `$conn` is an instance of mysqli. Did you read the PHP manual to learn how to open PDO connection properly? – Dharman Aug 07 '21 at 14:28
  • Im using mysqli . How can i change from PDO to mysqli ? – Potato Aug 07 '21 at 14:30
  • @JoshJ i have already check there is no error.. – Potato Aug 07 '21 at 14:30
  • Why on earth would you want to change the code from PDO to mysqli? Stick with PDO if you have already written PDO code. It's much easier and better than mysqli – Dharman Aug 07 '21 at 14:31
  • There is definitely an error in SQL if your connection is mysqli. It doesn't support the same features as PDO does. So all of the named placeholders are SQL syntax errors – Dharman Aug 07 '21 at 14:31
  • @Dharman Im still new with PHP.. Do you have any solution to solve my error please. I really need help. I just want to solve this error and complete my assignment. – Potato Aug 07 '21 at 14:33
  • @Dharman so i change my $sql = 'INSERT INTO items_table(id, brand_name, .... ? – Potato Aug 07 '21 at 14:34
  • @Dharman but there is no time for me.. Would you able to help me solve this error? i send you my files – Potato Aug 07 '21 at 14:37
  • No. I am not here to help you write your project. Stack Overflow is not a free personal help desk. I have given you materials where you can learn PDO. I can't help you any more. You can't take shortcuts when learning programming. You need to take some time to learn how to do it properly. – Dharman Aug 07 '21 at 14:39

0 Answers0