0

I have set the php as below aims at accessing mysql database and then get the column of id(set as pid). However, the page can access to the database, but it failed to access the table.

<?php
    $con = mysqli_connect('localhost','root','password','adeline');
    if ($con) {
      echo 'Connection Successfully';
    }else{
      echo 'Connection Failed';
    }

    if (isset($_GET['pid'])) {
      // Prepare statement and execute, prevents SQL injection
      $stmt = $con->prepare('SELECT * FROM product WHERE pid = ?');
      $stmt->execute([$_GET['pid']]);
      // Fetch the product from the database and return the result as an Array
      $product = $stmt->fetch_assoc();
      // Check if the product exists (array is not empty)
      if (!$product) {
          // Simple error to display if the id for the product doesn't exists (array is empty)
          exit('Product does not exist!');
      }
    } else {
        // Simple error to display if the id wasn't specified
        exit('Product does not exist isset!');
    }

?>

it shows "Connection SuccessfullyProduct Does Not Exist Isset!" when I run the page. I did add the product table in phpmyadmin and pid exists in the column. I don't understand why it cannot access to product table to get the pid I want. This is my product table in my database.

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
newcomer
  • 1
  • 2
  • This is showing that the value isn't set in `$_GET` – Nigel Ren Sep 25 '22 at 12:51
  • Sorry, what does it mean? How to set the value? – newcomer Sep 25 '22 at 13:03
  • https://stackoverflow.com/questions/5415224/how-to-set-get-variable – Nigel Ren Sep 25 '22 at 13:06
  • But I thought I already set the value by phpmyadmin column pid. Do I need to set the value? I mean the value should be same as the pid value after accessing my database product table. If my understanding is incorrect, pls help to advise. Thx! – newcomer Sep 25 '22 at 13:19

0 Answers0