Datatables console error jQuery.Deferred exception: Cannot set property '_DT_CellIndex' - php

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>";
}
?>

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>

Related

PHP MySQL only displays Blank Table

Good Day! Im having a problem displaying the data from table. I have already inputted 6 data and it will show how many base from my $i++ but it does not show the userno, fullname and udate. i would really appreciate it Please and thank you! Table Display Here is my code:
table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
<?php }; ?>
</tbody>
</table>
You forgot to echo your results.
Change
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
To
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
Or
<td><?= $uresult ['userno'];?></td>
<td><?= $uresult ['fullname'];?></td>
<td><?= $uresult ['udate'];?></td>
$sql = "SELECT userno, fullname, udate FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> userno: ". $row["userno"]. " - Full Name: ". $row["fullname"]. " - udate: " . $row["udate"] . "<br>";
}
} else {
echo "0 results";
}
//Don't Forget To:
$conn->close();
This'll be your PHP, adjust it accordingly to your table, hope this helps!
First of all, you have missed end</tr>, second do you really have to use variable i and increment it? where you can echo all ID? and why there's semi-colon after your closing bracket.Lastly, you should have put echo in variables uresult Hope this could help you
<table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
?>
<tr>
<td><?php echo $uresult ['id'];?></td>
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
and also you can write it, in this way:
and echo variable `i` if you want to see the count of it.
<?php
$uquery= "SELECT * from users";
$viewquery = mysqli_query($con,$uquery);
while($uresult = mysqli_fetch_assoc($viewquery))
{
?>

Delete query not working to delete stock

i am implementing clothing shopping website in which admin manages stock for example add, update, delete stock. i am having problem with my delete query. stock is displayed in table and every row has its own delete button. when i click on delete button it always takes last row id and delete that and not that row which i want. Query is taking always last row id.
CODE:
<form action="" method="post" enctype="multipart/form-data" name="deleting" >
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$id=$_POST['ID']; //problem is here: it always takes last row id
$link=mysqli_connect("localhost","root","") or die("Cannot Connect to the database!");
mysqli_select_db("login",$link) or die ("Cannot select the database!");
$query="DELETE FROM add_stock WHERE id='".$id."'";
$result=mysqli_query($query,$link) or die(mysqli_error($link));
if($result)
{
echo '<script>confirm("Are you sure want to delete this record?")</script>';
echo '<script>alert("Record ".$id." removed successfully!")</script>';
}
else
{
die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");
}
}
?>
I would rather suggest not to use <form> just for deleting purpose. You can use <a> tag to redirect it to other page for deleting purpose.
Here is my code.
index.php
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query($sql);
while ($result = mysqli_fetch_array($rs_result)) {?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td>
<a href="deleteStock.php?id=<?php echo $result['id'];?>">
<input type="button" value="Delete" >
</a>
</td>
</tr>
<?php }?>
</table>
<?php
if(isset($_GET['delete'])){
if($_GET['delete'] == "success"){
echo '<script>alert("Record removed successfully!")</script>';
}
if($_GET['delete'] == "fail"){
echo '<script>alert("An unexpected error occured while <b>deleting</b> the record, Please try again!")</script>';
}
}
?>
<script>
$(document).on('click', "#myTable a", function(e) {
if(confirm("Are you sure want to delete this record?")) {
return true;
} else {
return false;
}
});
</script>
deleteStock.php
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$link = mysqli_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysqli_select_db("login", $link) or die("Cannot select the database!");
$query = "DELETE FROM add_stock WHERE id = $id";
$result = mysqli_query($query, $link) or die(mysqli_error($link));
if($result){
header("location:index.php?delete=success");
} else {
header("location:index.php?delete=fail");
}
}?>
[Important: And, still you have not created separate file for DB Connection, which I have already mentioned in my answer of your question modal popup keep populating only first item data on all item buttons 1 Week before. It implies, you don't learn from your mistake or you don't need any suggestions.]
Your script taking last row in post always because you have single form for all your data and your last value will be overwrite all previous data. Intead of it remove form from out side of table add add it to td where you have specify hidden field and delete button. Like below:
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><form action="" method="post" name="deleting" ><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></form></td>
</tr>
<?php
}
?>
</table>
Also Remove enctype="multipart/form-data" from form its needed only if you have to upload file

Column name field showing multiple time in php?

I am searching records and records are displaying from database, but column name is repeating.Please check below images.
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
</tbody>
</table>
<?php
}
} else {
echo "0 results";
}
What i am getting
What i need
after added html code on header getting output
Put your table code outside of the loop like below :-
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
<?php } } else { echo "0 results";}?>
</tbody>
</table>
You are getting header part after every fetching because you are using header part also in loop. Place header part above loop and keep only
below code in loop.
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>

While loop not working in PHP

While loop not working not showing anything.
My code is working when I use without creating a function on the same page but when I create function on functions.php and call it, it does not work.
<?php
include "include/db.php";
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
while ($row_search = mysqli_fetch_array($result_search)) {
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
}
}
?>
and
<?php
if(isset($_GET['search'])){
global $connection;
global $result_search;
search();
?>
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php echo #$name_search; ?></td>
<td><?php echo #$f_search; ?></td>
<td><?php echo #$school_search; ?></td>
<td><?php echo #$roll_search; ?></td>
<td><?php echo #$email_search; ?></td>
<td><?php echo #$class_search; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can see nothing happen:
Did you try to connect to the mysql before the execution of the query. Maybe, you have put the connect code in the other file, however couldn't see it here so just wondering.
your code should be like below, hope it will work to you.
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
return $row_search = mysqli_fetch_array($result_search);
}
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
$search_results = search();
$i=1;
foreach($search_results as $row_search){
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $name_search; ?></td>
<td><?php echo $f_search; ?></td>
<td><?php echo $school_search; ?></td>
<td><?php echo $roll_search; ?></td>
<td><?php echo $email_search; ?></td>
<td><?php echo $class_search; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

Categories