如何解决注意:未定义的索引:ID中的ID未定义的索引:3行中的detailsmodal.php中的ID

时间:2019-05-27 10:10:03

标签: php mysql

我的PHP代码有问题

  

“注意:未定义的索引”

由于我是初学者,所以我并没有很好地解决问题所在,所以请帮助我。

<?php 
    require_once 'core/init.php';
    $id = $_POST['id'];
    $id =(int)$id; 

    $sql ="SELECT * FROM products WHERE id = '$id'";
    $result = $db->query($sql);
    $product= mysqli_fetch_assoc($result);

    $brand_id = $product['brand'];
    $sql = "SELECT brand FROM brand WHERE id = '$brand_id'";
    $brand_query = $db->query($sql);
    $brand = mysqli_fetch_assoc($brand_query); 
?>

// index.php

<?php 
      require_once 'core/init.php';
      include 'includes/head.php';
      include 'includes/navigation.php';
      include 'includes/headerfull.php';
      include 'includes/leftbar.php';
      include 'includes/footer.php';      
      include 'includes/detailsmodal.php';

      $sql = "SELECT * FROM products WHERE featured = 1";
      $featured = $db->query($sql);
?>
        <!--main content-->
        <div class="col-md-8">          
        <div class="row">
        <h2 class="text-center">Feature products</h2>
        <?php while($product = mysqli_fetch_assoc($featured)) : ?>
        <div class="col-md-3 text-center"> 
            <h4><?= $product['title']; ?> </h4>
            <img src="<?= $product['image']; ?>" alt="<?= $product['title']; ?>" class="img-thumb" />
            <p class="list-price text-danger">List price:<s>$<?= $product['list_price']; ?></s></p>
            <p class="price">Our price :$<?= $product['price']; ?></p>
        <button type="button" class="btn btn-sm btn-success" onclick="detailsmodal(<?= $product['id']; ?>)">Details</button>
        </div>

        <?php endwhile ;?>
        </div>
    <footer class="text-center" id="footer">&copy; copyright 2019</footer>

     </div>     

    <?php 
        include 'includes/rightsidebar.php';
        include 'includes/footer.php';

    ?>
  

未定义索引:ID中   第3行上的C:\ xampp \ htdocs \ tutorial \ includes \ detailsmodal.php

1 个答案:

答案 0 :(得分:1)

在detailsmodal.php文件索引$ _POST ['id']中未定义

首先检查是否设置了作业....

if(isset($_POST['id'])){
   code...
}else{
   code...
}

$id = isset($_POST['id']) ? $_POST['id'] : null;