注意:当我fetchAll时未定义的索引

时间:2019-07-09 16:59:16

标签: php mysql pdo

我需要从多个表中获取所有数据,而当我只做fetch()时,它只给我表中的最后一行。当我做fetchAll()时,它给我一个错误消息:

  

'注意:未定义索引:prix输入   109'行上的C:\ wamp64 \ www \ lecoq \ admin \ facture.php'

fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)

这是我的代码:

<?php 
include'header.php';
include'db/connexion.php';
session_start();
if (isset($_SESSION['login'])) {
  $login=$_SESSION['login'];
}else{
  header('location:index.php');
}

$ID=$_GET['ID'];

$sql = "SELECT * FROM customers  Where ID=$ID ";

$sql2 = "SELECT * FROM reservation_client ";

$sql3 = "SELECT * FROM facture Where id_client=$ID ";

$req = $bdd->prepare($sql);
$donnees = $req->fetch();

$req2 = $bdd->prepare($sql2);
$donnees2 = $req2->fetch();

$req3 = $bdd->prepare($sql3);
$donnees3 = $req3->fetch();

$req->execute();

$req2->execute();

$req3->execute();

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <?php include'header.php';  ?>
  </head>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <?php include'side.php';  ?>

        <!-- top navigation -->

        <!-- /top navigation -->

        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3>Facture </h3>
              </div>
            </div>

            <div class="clearfix"></div>

            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2><a href="add_facture.php?ID=<?php echo $ID ?>" class="btn btn-primary">Ajouter autre facture  </a></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
<div class="x_content">
<table id="" class="table table-striped ">
  <thead>
    <tr>
      <th>ID</th>
      <th>Nom</th>
      <th>Montant de reservation</th>
      <th>Facture</th>
      </tr>
  </thead>
  <tbody>

    <?php 
      while ($donnees = $req->fetch() AND $donnees2 = $req2->fetch() AND $donnees3 = $req3->fetchALL() ) {
        print "<tr>";
        print "<td>".$donnees['ID']."</td>";
        print "<td>".$donnees['NAME']."</td>";
        print "<td>".$donnees2['montant']." DH </td>";
        print "<td>".$donnees3['prix']."</td>";             
        print "</tr>";
      }
      $req->closeCursor();
    ?>
  </tbody>
</table>
</div>

</div>
</div>
        <!-- /page content -->

        <!-- footer content -->
      <footer>
          <div class="pull-right">
            <a href="https://colorlib.com">copyright © Tanji Info</a>
          </div>
          <div class="clearfix"></div>
       </footer>
       <!-- /footer content -->
      </div>
    </div>

1 个答案:

答案 0 :(得分:-4)

$donnees3['prix']在该特定迭代中不存在,因此您会遇到该错误。

正确的处理方式是使用isset($donnees3['prix'])并在将其打印到页面之前检查它是否存在。

例如;改变这个:

print "<td>".$donnees3['prix']."</td>";

if(isset($donnees3['prix'])){
    print "<td>".$donnees3['prix']."</td>";
}else{
    print "<td>&nbsp;</td>";
}

最好是找出为什么找不到“ prix”的原因。