2017-08-14 18:58 阅读 43


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--->
        <a href="singolo_prodotto.php?product_id='.$row['id'].'">Link</a>

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

<!---Single Product--->

<!DOCTYPE html>

$product_id = $_GET['product_id'];

    else {
include '../sys/conn.php';
$risultato = mysqli_query ($conn, "

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

") or die ("Invalid query: " . mysqli_error($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">
<input class="form-control" placeholder="SKU Code" value = "<?php echo $row['6']; ?>" >

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doubi7496 doubi7496 2017-08-14 19:07

    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.

    点赞 评论 复制链接分享