-1

my datatables didn't work and i get console error Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined but when i delete the query inside tbody its working fine.

<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>ID</th>
                    <th>Nama</th>
                    <th>Golongan</th>
                    <th>Nilai Output</th>
                    <th>Penilaian Atasan</th>
                    <th>Nilai Learning</th>
                    <th>Nilai Kedisiplinan</th>
                    <th>Nilai 5R</th>
                    <th>Hasil</th>
                    <th>Tanggal</th>
                  </tr>
                </thead>
                  <!-- query -->
                  <?php
                  $no = 0;
                  $query = "SELECT * FROM user 
                            where id= $_SESSION[id]";

                  $query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal 
                            FROM tkaryawan 
                            JOIN user 
                            ON user.id=tkaryawan.id 
                            where user.id= $_SESSION[id] 
                            ORDER BY tanggal DESC";

                  $result = mysqli_query($koneksi, $query);
                  
                  if(!$result){
                    die ("Query Error: ".mysqli_errno($koneksi).
                    " - ".mysqli_error($koneksi));
                  }
                  while($data = mysqli_fetch_assoc($result))
                  {
                    $no++;
                    ?>
                  <tbody>
                    <tr>
                      <td><?php echo $no;?></td>
                      <td><?php echo $data['id'];?></td>
                      <td class="text-capitalize"><?php echo $data['nama'];?></td>
                      <td><?php echo $data['golongan'];?></td>
                      <td><?php echo $data['nilai_output'];?></td>
                      <td><?php echo $data['nilai_atasan'];?></td>
                      <td><?php echo $data['nilai_learning'];?></td>
                      <td><?php echo $data['nilai_kedisiplinan'];?></td>
                      <td><?php echo $data['nilai_5r'];?></td>
                      <td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
                      <td class="text-secondary"><?php echo $data['tanggal'];?></td>
                    <tr>
                  </tbody>
                  <?php
                  }
                  // free the memory 
                  mysqli_free_result($result);
                  // close conection
                  mysqli_close($koneksi);
                  ?>
              </table>

appreciate any help you can provide.

EDITED

          <?php

          $stmt = $mysqli->prepare("SELECT id,nama FROM user WHERE user.id= $_SESSION[id]");
          $stmt->execute();
          $res = $stmt->get_result();
          $row = $res->fetch_assoc();

          if(!$row){
            mysqli_query($link, $sql);
          }
          else
          {
          echo "<h3 class='text-uppercase'>HASIL KINERJA </br> <p class='font-weight-bold text-danger'> $row[nama] </p> </h3>";
          }
          ?>
Lee wayy
  • 73
  • 1
  • 11
  • your first query ```$query = "SELECT * FROM user where id= $_SESSION[id]";``` is being overwritten by the second one. Is this intentional? Also I'm guessing ```$koneksi``` is the connection variable? – ewokx Sep 10 '20 at 08:13
  • aww thanks for pointing that, that is unintentional. and yes it was a connection variable – Lee wayy Sep 10 '20 at 08:18
  • might this help? https://stackoverflow.com/questions/37080190/datatable-breaks-nested-repeater-and-bootstrap – ewokx Sep 10 '20 at 08:22
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 14 '20 at 13:31
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Sep 14 '20 at 13:31
  • @dharman can you check my new edited post? is that how to use prepare statement properly? thanks – Lee wayy Sep 15 '20 at 01:37
  • No, you must use parameter binding https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Sep 15 '20 at 08:14
  • thankss, it help me a lot – Lee wayy Sep 15 '20 at 08:46
  • @Dharman did i really need to bind the data if i just only want to view the data? – Lee wayy Sep 16 '20 at 05:56
  • Yes. You always need to bind the data to avoid errors. – Dharman Sep 16 '20 at 08:06

1 Answers1

0

You put your while loop above tbody which will loop the tbody as well. You need to move the while and its closing tag on <tr>. Also try to change the closing tr tag on tbody to be </tr>.

<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>ID</th>
                    <th>Nama</th>
                    <th>Golongan</th>
                    <th>Nilai Output</th>
                    <th>Penilaian Atasan</th>
                    <th>Nilai Learning</th>
                    <th>Nilai Kedisiplinan</th>
                    <th>Nilai 5R</th>
                    <th>Hasil</th>
                    <th>Tanggal</th>
                  </tr>
                </thead>
                  <!-- query -->
                  <?php
                  $no = 0;
                  $query = "SELECT * FROM user 
                            where id= $_SESSION[id]";

                  $query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal 
                            FROM tkaryawan 
                            JOIN user 
                            ON user.id=tkaryawan.id 
                            where user.id= $_SESSION[id] 
                            ORDER BY tanggal DESC";

                  $result = mysqli_query($koneksi, $query);
                  
                  if(!$result){
                    die ("Query Error: ".mysqli_errno($koneksi).
                    " - ".mysqli_error($koneksi));
                  }
                    ?>
                  <tbody>
                    <?php
                     while($data = mysqli_fetch_assoc($result))
                     {
                         $no++;
                    ?>
                    <tr>
                      <td><?php echo $no;?></td>
                      <td><?php echo $data['id'];?></td>
                      <td class="text-capitalize"><?php echo $data['nama'];?></td>
                      <td><?php echo $data['golongan'];?></td>
                      <td><?php echo $data['nilai_output'];?></td>
                      <td><?php echo $data['nilai_atasan'];?></td>
                      <td><?php echo $data['nilai_learning'];?></td>
                      <td><?php echo $data['nilai_kedisiplinan'];?></td>
                      <td><?php echo $data['nilai_5r'];?></td>
                      <td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
                      <td class="text-secondary"><?php echo $data['tanggal'];?></td>
                    </tr>
                    <?php } ?>
                  </tbody>
                  <?php
                  // free the memory 
                  mysqli_free_result($result);
                  // close conection
                  mysqli_close($koneksi);
                  ?>
              </table>
DennisFrea
  • 1,112
  • 8
  • 10
  • how about the js file? – DennisFrea Sep 10 '20 at 08:24
  • i only using js from cdn that datatables provide – Lee wayy Sep 10 '20 at 08:33
  • I see the closing tr tag is not correct on your tbody section, it should be same like the code I put on answer. Is it working if you fix that? – DennisFrea Sep 10 '20 at 08:43
  • oops my bad, i fixed it and it works but looks so ugly – Lee wayy Sep 10 '20 at 08:53
  • you also need to include the css to make it look better I guess https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css – DennisFrea Sep 10 '20 at 08:58
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 14 '20 at 13:31
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Sep 14 '20 at 13:31