I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'
Related
I'm trying to make a table of which it is pulling information from a database. Its suppose to list the module name and then how many assignments are in it. There are two database tables involved, one simply is an id and modulename, other is id, assignment, topic, content, etc. For some reason my table won't appear correctly, it just continues rather than starting on a new line for that table. here's a picture of what it is doing
enter image description here
here's the code(if more is needed let me know):
<div id="module_1" class="row CourseModule">
<a href="#" onclick="showAssignments(1);">
<div class="col-lg-12 columnPad">
<h1-1 class="lead">List of Modules</h1-1>
</a>
<div class="pullright">
<a id="addAssignmentbutton_1" class="btn btn-primary ThemeButton" href="#" onclick="addAssignment(1);"> Add Assignment</a>
</div>
</div>
</div>
<div id="module_Assignments_1" class="row CanvasPad" style="display:none;">
<table class="CanvasTable">
<thead>
<th>
Module
</th>
<th>
Assignments
</th>
</thead>
<?php
while($row = mysqli_fetch_assoc($result)){
$count = 0;
$query = "SELECT module_id FROM module_records";
$result2 = mysqli_query($con, $query);
if(!$result2){
echo "Can't retrieve data " . mysqli_error($con);
exit;
}
while ($modInassignment = mysqli_fetch_assoc($result2)){
if($modInassignment['module_id'] == $row['module_id']){
$count++;
}
}
?>
<td>
<?php echo $row['module_name']; ?>
</td>
<td>
<span class="badge"><?php echo $count; ?></span>
</td>
<?php } ?>
List all assignments
</table>
</div>
.js
function showAssignments(module_id){
var html_id = "#module_Assignments_" + module_id;
if($(html_id).css("display") == 'block'){
$(html_id).css("display", "none");
} else {
$(html_id).css("display", "block");
}
}
You need a table row tr and then the table data td in it
Also, not required but good for grouping without adding divs/class/id; add all these tr inside a tbody if needed. Read more why it's not required here
<table class="CanvasTable">
<thead>
<th>
Module
</th>
<th>
Assignments
</th>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result)){
$count = 0;
$query = "SELECT module_id FROM module_records";
$result2 = mysqli_query($con, $query);
if(!$result2){
echo "Can't retrieve data " . mysqli_error($con);
exit;
}
while ($modInassignment = mysqli_fetch_assoc($result2)){
if($modInassignment['module_id'] == $row['module_id']){
$count++;
}
}
?>
<tr>
<td>
<?php echo $row['module_name']; ?>
</td>
<td>
<span class="badge"><?php echo $count; ?></span>
</td>
</tr>
<?php } ?>
</tbody>
List all assignments
</table>
More info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr
To do list:
I have created the HTML and php code to insert data into the database, and read from it.
Database is looking like this:
task_id |task_desc |task_target |task_finished_date |task_status
40 |test p |2020-07-25 |2020-07-23 |completed
Task_target and task_finished_date are using type date in the database.
task_target is getting value on creation of the task, and task_finished_date is getting automatically value when we mark the job completed using now():
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
I am currently trying to program it when some task is completed AFTER the deadline to be displayed in "Failed" table, and if is closed before the deadline to be displayed in completed table.
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd < $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
As in the $finished_tasks variable i have storing the values for the completed tasks:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
Have tried to get the deadline(task_target)and target(task_finished_date) and insert them in both variables and then compare them, and based on this comparison to display them where I want, but the logic here is not right I think.
How I can separate the tasks based on the desired condition?
Entire Code Below:
<?php
// initialize errors variable
$errors = "";
// connect to database
$db = mysqli_connect("localhost", "root", "", "todo");
// insert a quote if submit button is clicked
if (isset($_POST['submit'])) {
if (empty($_POST['task_desc'])) {
$errors = "You must fill in the task";
}else{
$task_desc = $_POST['task_desc'];
$task_target = $_POST['task_target'];
$sql = "INSERT INTO tasks (task_desc, task_target) VALUES ('$task_desc', '$task_target')";
mysqli_query($db, $sql);
header('location: index.php');
}
}
// delete task
if (isset($_GET['del_task'])) {
$task_id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE task_id=".$task_id);
header('location: index.php');
}
if(isset($_GET['completed'])){
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
// select all tasks if page is visited or refreshed
$tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'Active' ");
?>
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>ToDo List Application PHP and MySQL</title>
</head>
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-12">
<div class="heading">
<h2 style="font-style: 'Hervetica';">ToDo List Application PHP and MySQL database</h2>
</div>
<form method="post" action="index.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<label for="task_target">Select Date:</label>
<input type="date" class="form-control" name="task_target">
<label for="task_desc">Describe your task:</label>
<textarea class="form-control" name="task_desc" id="body" cols="30" rows="5"></textarea>
<button type="submit" name="submit" id="add_btn" class="form-control">Add Task</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Ongoing Tasks</th>
<th>Target Date</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row['task_desc']; ?> </td>
<td> <?php echo $row['task_target']; ?> </td>
<td>
Delete
</td>
<td>
Complete
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
?>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Successful Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php } ?>
</div>
</div>
</div>
</body>
</html>
Your question is quite ambiguous, what kind of error or unexpected output are you getting?
Had to strip your code of the HTML part and the PHP part not affecting the logic, here's what I have.
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
$i++;
}
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
}
$i++;
}
}
All three while loops assign a value to the $row variable. This would cause some unexpected results as some values may be skipped.
You're referencing the SQL result indexes inappropriately.
I have managed to find a easy fix of my question.
First of all thanks to all who tried to help me and advised way of effective learning.
Really like this place.
So, on the answer:
As in the begging i have described that the Deadline of the task is defining during the creation of the task, and the closing time is getting to the database when the user click Completed button.
So i have decided to have the sort directly into the database modifying the queries like this:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date < task_target ");
$finished_failed_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date > task_target ");
In this way i have all of the completed successfully tasks in $finished_tasks variable and all of the failed in $finished_failed_tasks, after that the displaying in the page is easy.
I am not quite sure if this is practical solution, but in this way working as i desired.
Next step is to strip and refactor the code as you suggested.
Once again, thanks really much and have a nice day.
Regards, Kristiyan
I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.
I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.