doq1969
2017-08-14 18:58 阅读 43
已采纳

如果按下回车键,则Product_ID变量赋值不起作用[复制]

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?

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

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.

    点赞 评论 复制链接分享

相关推荐