DELETE ROW where ID = $id not deleting the row requested - php

I'm trying to implement a feature which allows me to delete students from a students list. (which is a table)
Though when I click delete it does not delete the student in that current row HOWEVER.. if i change my query to DELETE FROM students WHERE id = 1 for example.. it works. So, I'm believing the problem lies with not being able to grab the id from the current row however, I'm not sure where I am going wrong with this.
//StudentsList.php
<?php include ('../resources/styling.html'); ?>
<?php include ('adminNavbar.php'); ?>
<!--HTML styling of the table that outputs the database entries, bootstrap styling-->
<div class="container content-area">
<div class="row">
<div class="col-md-12">
<div class="panel">
<br>
<div class="text-center border border-light p-5">
<p class="h4 mb-4">All Student Accounts</p>
</div>
<br>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Surname</th>
<th scope="col">Programme</th>
<th scope="col">Student ID</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
</div>
</div>
</div>
</div>
<?php
include ('../resources/config.php');
$sql = "SELECT id,firstname, surname, programme, studentid FROM students";
$result = $db->query($sql);
// while loop to output data of each row for each student in database
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["id"]. '</td>
<td>' . $row["firstname"] .'</td>
<td> '.$row["surname"] .'</td>
<td> '.$row["programme"] .'</td>
<td> '.$row["studentid"] .'</td>
<td ><a class="btn btn-danger" href="MLdelete.php?id=".$row["id"].""</a> Delete</td>
</tr>';
}
$db->close();
?>
</tbody>
</table>
<div class="text-center border border-light p-5">
Add New Student Account
</div>
//MLdelete.php
<?php
require ( '../resources/config.php' );
if(isset($_GET["id"])){
$id = mysqli_real_escape_string($db, $_GET["id"]);
// sql to delete a record
$query = "DELETE FROM students WHERE id ='{$id}'";
$del=mysqli_query($db,$query);
if ($del) {
header("Location: StudentsList.php");
} else {
echo "Error deleting record: " . $db->error;
}
}
$db->close();
?>
Basically, when I click the DELETE button for a row, nothing happens.

You did not properly quote your <a> link.
It should be
<td ><a class="btn btn-danger" href="MLdelete.php?id=' . $row["id"] . '"</a> Delete</td>
Also use prepared statements as per my comment. This will prevent SQL injection attacks.
Check out prepared statements explanation and examples via the PHP manual

Related

how to show two mysql tables values at one while loop using php

I have created two tables, table1 and table2
table1 includes (id, codes and titles).
table2 includes id, table1_id, column1, column2, column3
I want Insert value on table1(codes , titles)
and show in table using PHP
and insert table2(column1, column2, column3) and show it
under table1 values.
This my codes
<?php
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr >
<td style="border: none"><?php echo $row->codes; ?></td>
<td style="border: none"><?php echo $row->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-
describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);
$stmt->execute();
$res=$stmt->get_result();
while($row=$res->fetch_object() )
{
?>
<tr>
<td><?php echo $row->column1 ?></td>
<td> <?php echo $row->column3 ?></td>
<td> <?php echo $row->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-
target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
I expected to shows me like this
but when I run that code it shows me , one value
You made two mistakes:
Used same variable name $row for iteration in outer and inner loop
Didn't specified table1_id in where condition in second query
Try below code, I am, sure It will work
<?php
include('config.php');
$ret = " SELECT * from table1
";
$stmt = $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute(); //ok
$res = $stmt->get_result();
$cnt = 1;
while ($row1 = $res->fetch_object()) {
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr>
<td style="border: none"><?php echo $row1->codes; ?></td>
<td style="border: none"><?php echo $row1->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria- describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret = " SELECT * from table2 where table1_id = ".$row1->id;
$stmt = $mysqli->prepare($ret);
$stmt->execute();
$res = $stmt->get_result();
while ($row2 = $res->fetch_object()) {
?>
<tr>
<td><?php echo $row2->column1 ?></td>
<td> <?php echo $row2->column3 ?></td>
<td> <?php echo $row2->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>

Print html table in pdf , many pdf to be generated

I have a page that is generated using data from a database into a table format.
Each result of a student is displayed on a page so that when printed on a printer, each result is printed on a page. I need now to generate a pdf for each student. How can I add code to generate the pdf, using the name of the student?
Thanks in advance.
<body>
<?php
$grade=7;
require 'GetStudentNames.php';
while ($row1 = $result1->fetch_assoc()) { ?>
<P style="page-break-before: always">
<table margin-top="70px" width="100%">
<tr>
<td rowspan="2"><img src="logo.PNG" alt="logo"></td>
<td style="text-align:center" rowspan="1">
<h2>xyz School</h2>
</td>
<tr>
<td rowspan="1" style="text-align: center; ">
<h3 style="text-decoration: underline ;text-align:center">Student Report Sheet - First Term 2021</h3>
</td>
</tr>
</table>
<?php echo '<br><table width="100%"><th width="50%">Student Name: '.#$row1["name"].' '.#$row1["firstname"].'</th>';
echo '</td>';
require 'db.php';
$sql = "select subject from results where SN = '".#$row1["sn"]."' order by listorder";
$result = $conn->query($sql);
$count=1;
while ($row = $result->fetch_assoc()) {
echo '<tr class="oneSubj"><td class="oneSubj">'. $count.'. '.#$row["subject"].'</td>
</tr>';
$count++;
}?>
</table>
<br>
<table style="width: 100%">
<th style="width:25%">Absences..................</th>
<br>
<h5 style="text-decoration: underline ;text-align:center">Special Mention</h5>
</table>
<?php }?>
</body>

Show results from mysql query with php 5

<table class="table table-bordered table-hover table-responsive bgwhite clgray">
<thead>
<tr>
<th class="text-center text-capitalize" width="20">
No
</th>
<th class="text-center text-capitalize">
idbarang
</th>
<th class="text-center text-capitalize">
Kategori
</th>
<th class="text-center text-capitalize">
Merk
</th>
<th class="text-center text-capitalize">
Foto
</th>
<th class="text-center text-capitalize">
Stok
</th>
</tr>
</thead>
<?php
$queryStok = "SELECT SUM(stok) AS jumlahbarang FROM tabelstok WHERE idjenisusaha=1 GROUP BY idbarang";
$resultStok = mysqli_query($link,$queryStok);
$queryStok2 = "SELECT * FROM tabelbarang,tabelmerk WHERE tabelbarang.idmerk=tabelmerk.idmerk";
$resultStok2= mysqli_query($link,$queryStok2);
while ($datastok=mysqli_fetch_assoc($resultStok,$resultStok2)) {
?>
<tbody>
<tr>
<td class="text-center text-capitalize">
<?php echo $datastok['merk'] ?>
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
<?php echo $datastok['jumlahbarang'];?>
</td>
</tbody>
<?php } ?>
</table>
How can I show the result of 2 tables with 2 queries, in A SINGLE loop.
If you want to show the result of two queries without writting two while loops, you should use mysqli_multi_query() function to execute multiple queries at once. Each statement should have a semicollon at the end (the last statement should not contain the semicollon). You can add as many queries as you want.
$query = "YOUR FIRST STATEMENT;";
$query .= "YOUR SECOND STATEMENT";
$result = mysqli_multi_query($link, $query);
do {
$result = mysqli_store_result($link);
while ($row = mysqli_fetch_row($result)) {
// DO YOUR STUFF HERE WITH $row
}
mysqli_free_result($result);
} while (mysqli_next_result($link));
If you want to merge the queries into one, then if there is a column which connects/associates both tables, then you should use JOIN when you build the query. Example:
SELECT customers.id, customers.name FROM customers
INNER JOIN details ON customers.id=details.user_id;
This merges the elements from details table with elements from customers table, linked by a common value (the customers ids).
As you didn't show the structure of the database, and tables I can't show an example based on your code, just a general one.

PHP - Checkbox in table not returning any value

When I click the delete button, the values of those checkboxes that are checked in the table are not shown. It is suppose to delete records on the database based on the id but i'm stuck in getting the value of the checkboxes. Please help correct my code. I'm using bootstrap for your information.
PHP
$sql = "SELECT id, title FROM box";
$query = mysqli_query($db, $sql);
if(isset($_POST['btnDelete'])) {
$checkbox = isset($_POST['chkDelete']) ? $_POST['chkDelete'] : array();
for($i=0;$i<count($checkbox);$i++)
{
$message = $checkbox[$i];
}
}
HTML
<div class="content">
<div id="table">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th></th>
<th>#</th>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_array($query))
{
$html = '<tr>
<td data-title="">
<input name="chkDelete[]" type="checkbox" value="' . $result['id'] . '">
</td>
<td data-title="#">' . $result['id'] . '</td>
<td data-title="Title">' . $result['title'] . '</td>
<td data-title="Action">
Edit
</td>
</tr>';
echo $html;
}
?>
</tbody>
</table>
</div>
<form action="" method="POST">
<input type="submit" name="btnDelete" value="Delete" />
</form>
<div class="message"><?php echo $message;?></div>
</div> <!--//END content-->
PHP (Write Delete Query inside for loop to delete all checked checkbox.)
<?php
if(isset($_POST['btnDelete']))
{
$checkbox = isset($_POST['chkDelete']) ? $_POST['chkDelete'] : array();
$id = 0;
for($i=0;$i<count($checkbox);$i++)
{
$id = $checkbox[$i];
$deleteQuery = "DELETE FROM box WHERE id='$id'";
$DeleteQueryExec = mysqli_query($db, $deleteQuery);
}
}
?>
HTML (Put entire table inside <form></form>)
<div class="content">
<form action="" method="POST">
<div id="table">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th></th>
<th>#</th>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT id, title FROM box";
$query = mysqli_query($db, $sql);
while($result = mysqli_fetch_array($query))
{
$html = '<tr>
<td data-title="">
<input name="chkDelete[]" type="checkbox" value="' . $result['id'] . '">
</td>
<td data-title="#">' . $result['id'] . '</td>
<td data-title="Title">' . $result['title'] . '</td>
<td data-title="Action">
Edit
</td>
</tr>';
echo $html;
}?>
</tbody>
</table>
</div>
<input type="submit" name="btnDelete" value="Delete" />
<div class="message"><?php echo $message;?></div>
</form>
</div> <!--//END content-->

First Index Number Pop Up Last in Table

I'm using SB Admin Bootstrap.
My table didn't functioning as it should be (should be user can search the data in table and data arranged well), but :
This is my php code:
<div class="box-content">
<?php
if ($result) {
?>
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Username</th>
<th class="text-left">Password</th>
<th class="text-left">Full Name</th>
<th class="text-left">Task</th>
</tr>
</thead>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '
<tbody>
<tr>
<td class="center">' . $row['userid'] . '</td>
<td class="center">' . $row['username'] . '</td>
<td class="center">' . $row['password'] . '</td>
<td class="center">' . $row['fullname'] . '</td>
<td class="center">
<a class="btn btn-success" href="#">
<i class="halflings-icon white zoom-in"></i>
</a>
<a class="btn btn-info" href="#">
<i class="halflings-icon white edit"></i>
</a>
<a class="btn btn-danger" href="#">
<i class="halflings-icon white trash"></i>
</a>
</td>
</tr>
</tbody>';
}}
?>
</table>
</div>
Answer moved from comment
You are repeating <tbody> element, so script are just taking first occurrence of this tag and executing some functions, leaving every further <tbody> intact.
Move out of loop, it must be unique in table.

Categories