0

In my project I have:

  1. products list page
  2. single product page

Each row in the products list have a link that passes the product_ID to the single product page as follows:

<!---Products List Page--->
    ....
        <td>
        <a href="singolo_prodotto.php?product_id='.$row['id'].'">Link</a>
        </td>

In the single product page, the product_id is catched as follows:

<!---Single Product--->

<!DOCTYPE html>
<?php

$product_id = $_GET['product_id'];

if(!isset($_GET['product_id'])){$product_id='13321';}
    else {
include '../sys/conn.php';
$risultato = mysqli_query ($conn, "

(*...mysql query...*)

") or die ("Invalid query: " . mysqli_error($conn));
mysqli_close($conn);

$row = mysqli_fetch_array($risultato);
}
?>

In each field, the array value is embedded as follows:

<div class="col-lg-3">
  <form role="form">
     <div class="form-group">
       <label>SKU</label>
<input class="form-control" placeholder="SKU Code" value = "<?php echo $row['6']; ?>" >
      </div>
  </form>
</div>

If I pass from the products lists to the single product page through the above mentioned link, everything works fine. Reloading the single product page everything works fine as well. But if I push the ENTER key being positioned in one of the fields of the "single product" page it crashes giving these errors:

Notice: Undefined index: product_id in /Applications/XAMPP/xamppfiles/htdocs/gest/pages/singolo_prodotto.php on line 6

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/gest/pages/singolo_prodotto.php on line 637

Any idea on how this issue can be fixed?

Alex
  • 91
  • 2
  • 10

1 Answers1

1

The enter key submits your form, but as your form does not have an input with name product_id, the PHP code does not find it and generates the exception.

Now it depends on what you want:

  • Do you want the form submission system? Then make sure your PHP code can deal with that, and does not expect the product_id to always be defined in the $_GET array, but instead some variables (for named input elements) in the $_POST array.

  • Do you not want the form to be submitted? Then either don't use the form tag, or else add a onsubmit="return false" attribute to it.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Trincot, at this stage the form has to display only the values retrieved from the database. I should make null the action of pushing enter key. – Alex Aug 14 '17 at 19:11
  • That is then the second option I provided: add that atttribute. – trincot Aug 14 '17 at 19:12
  • Excellent, definitively it makes null enter key action. Thanks Trincot! – Alex Aug 14 '17 at 19:17