Home>

I get an error while creating an EC site in PHP.
Data is displayed on the product list screen, but when I move to the product details screen, the error "Trying to access array offset on value of type bool in" is displayed. I would be grateful if anyone could help.

Error screen
] (A9010f6ae377494c03b36c0ecd78800d.png)

Product list screen

<? php
session_start ();
session_regenerate_id (true);
if (! isset ($_SESSION ['member_login'])) {
    print'Welcome guest';
    print' Member login<br />';
    print'<br />';
} else {
    print'Welcome'. $_SESSION ['member_name'].'Sama';
    print' Logout<br />';
    print'<br />';
}
?>
<! DOCTYPE html>
<html>
<head>
  <meta charset = "UTF-8">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
  <title>Product list</title>
</head>
<body>
  <? php
    try {
        require_once ('../ common/dbconect.php');
        $sql ='SELECT id, product_name, price, image FROM mst_product WHERE 1';
        $stmt = $dbh->prepare ($sql);
        $stmt->execute ();
        $dbh = null;
    } catch (Exception $e) {
        print $e->getMessage ();
    }
    print'<p>Product list</P>';
    while ($rec = $stmt->fetch (PDO :: FETCH_ASSOC)):
    ?>

     <? php
       $id = $rec ['id'];
       $pro_name = $rec ['product_name'];
       $price = $rec ['price'];
       $image_name = $rec ['image'];
       if (empty ($image_name)) {
           $disp_image ='';
       } else {
           $disp_image ='';
       }
       ?>
            '"><? Php echo $disp_image;?>
          <? php print $pro_name;?>-<? php print $price;?>Yen

  <? php endwhile;?>

  <br />
     Look at the cart 

</body>
</html>

Product details screen

<? php
session_start ();
session_regenerate_id (true);
if (! isset ($_SESSION ['member_login'])) {
    print'Welcome guest';
    print' Member login<br />';
    print'<br />';
} else {
    print'Welcome'. $_SESSION ['name'].'sama';
    print' Logout<br />';
    print'<br />';
}
?>
<! DOCTYPE html>
<html>
<head>
  <meta charset = "UTF-8">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
  <title>Product information</title>
</head>
<body>
  <? php
    ini_set ('display_errors', 1);
    error_reporting (E_ALL);
    require_once ('../ common/dbconect.php');
    try {
        $id = $_GET ['proid'];
        $dbh->setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
        $sql ='SELECT * FROM mst_product WHERE id =?';
        $stmt = $dbh->prepare ($sql);
        $data [] = $id;
        $stmt->execute ($data);
        $rec = $stmt->fetch (PDO :: FETCH_ASSOC);
        $pro_name = $rec ['product_name'];
        $price = $rec ['price'];
        $image_name = $rec ['image'];
        $dbh = null;
        if (empty ($image_name)) {
            $disp_image ='';
        } else {
            $disp_image ='';
        }
        print' Add to cart<br /><br />';
    } catch (Exception $e) {
        print $e->getMessage ();
        exit ();
    }
    ?>
     Refer to product information 
  <? php print'Product code:'. $Id;?>
  <br />
  <br />
  <? php print'Product name:'. $Pro_name;?>
  <br />
  <br />
  <? php print'Price:'. $Price.'Yen';?>
  <br />
  <br />
  <? php print $disp_image;?>
  <form>
      <input type = "button" onclick = "history.back ()" value = "back">
  </form>
</body>
</html>
php
  • Answer # 1

    In the official PHP documentation

    $sth->execute (array ($calories, $colour));


    Because it is

    $data [] = $id;


    To

    $data = array ($id);


    I think it will work if you rewrite it.

  • Answer # 2

    google translate
    I'm trying to access an array offset of Boolean values

    You said, do you have any idea?