Why is my delete operation not performing? - php

In my project, I want a delete operation for a particular id when I click on the delete button, but it isn't performing. Here's my code for displaying the table:
<table class="listing" cellpadding="0" cellspacing="0">
<tr>
<th class="first"><center>Id</center></th>
<th>Category Name</th>
<th>Status</th>
<th>Edit</th>
<th class="last">Delete</th>
</tr>
<?php
include('config.php');
$sql="select * from category_tbl";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)) {
if($row['cat_status'] == 0) {
$im='<img src="../images/red.jpg" height="28" width="28">';
}
else{
$im='<img src="../images/green.jpg" height="30" width="30">';
}
if (isset($_REQUEST['false'])) {
$updt=mysql_query("update category_tbl set cat_status=1 where cat_id='".$_REQUEST['false']."'");
header('location:category.php');
}
if (isset($_REQUEST['true'])) {
$updt=mysql_query("update category_tbl set cat_status=0 where cat_id='".$_REQUEST['true']."'");
header('location:category.php');
}
?>
<tr>
<td><strong><?php echo $row['cat_id'];?></strong></td>
<td><strong><?php echo $row['cat_name'];?></strong></td>
<td><?php echo $im;?></td>
<td><img src="../images/edit.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></td>
<td><img src="../images/delete1.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></td>
</tr>
<?php
}
?>
</table>
The edit operation is working, but delete is not.
This is my code for delete:
<?php
include("config.php");
$id=$_REQUEST['id'];
$sql=mysql_query("DELETE FROM category_tbl WHERE cat_id='".$id."'");
if ($sql) {
header("location:category.php");
}
?>
After execution it stays on delete_cat.php?id=(passed id)....

i think you need to remove your foreign key from sub table or second option is you need to alter your query and set cascade like this.
ON DELETE CASCADE
for more help visit this link

Related

Change td content according to database

I have two table for assignments. TABLE1 has the data of the assignments posted by a faculty and TABLE2 has data of those students who submitted the assignments. Both table have one similar column called 'assignment name'.
My TABLE2 has a status column which is boolean data. By default i have set it to 0 and if a student submit his assignment that changes to 1.
In my HTML page (student side) I'm displaying the data from TABLE1 in a table. It shows all assignments name and I have given a submit status column there which will show if the assignment work is submitted by the logged in student. If yes it will show a check icon if not then a times icon.
This is what i have tried:
<?php
session_start();
include'connection.php';
$user=$_SESSION['details1'];
$query =mysqli_query($con,"SELECT course FROM student WHERE username='$user'");
while($row=mysqli_fetch_array($query))
{
$cr=$row['course'];
}
$query1 =mysqli_query($con,"SELECT semester FROM student WHERE username='$user'");
while($row1=mysqli_fetch_array($query1))
{
$sems=$row1['semester'];
}
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user'");
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<table class="student" id= "student">
<form action="" method="post">
<thead>
<tr class="table100-head">
<th class="column0">Sl. No.</th>
<th class="column2">Assignment Name</th>
<th class="column2">Subject</th>
<th class="column1">Date</th>
<th class="column0">View</th>
<th class="column0">Status</th>
</tr>
</thead>
<tbody>
<?php
$shcat=mysqli_query($con,"SELECT * FROM assignment INNER JOIN subject ON assignment.as_subject=subject.s_id WHERE s_course='$cr' AND a_semester='$sems'");
$cnt=1;
while($row=mysqli_fetch_array($shcat)){
$stat = 0;
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user' AND a_name=".$row['a_id']);
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<tr>
<td class="column0"><?php echo $cnt; ?></td>
<td class="column1"><?php echo $row['a_name'];?></td>
<td class="column2"><?php echo $row['s_name'];?></td>
<td class="column2"><?php echo $row['date'];?></td>
<td class="column0"><a target = "_blank" href="../faculty/assignment/<?php echo $row['image']?>"><i class="fa fa-eye"></i></a></td>
<?php
if ($stat == 0){ ?>
<td class="column0"><i class="fa fa-times" title="Not Submitted"></i></td>
<?php }
else {?>
<td class="column0"><i class="fa fa-check" title="Submitted"></i></td>
<?php }?>
</tr>
<?php $cnt=$cnt+1;
} ?>
</tbody>
</form>
</table>
From what i have tried it changes all the rows to check icon even if only one row's assignment is submitted.
Any help is much appreciated. Thank you.
Edit Its working now.
Your code to set $stat
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user'");
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
is just going to override the value of $stat leaving the last rows value in its place.
The simple way to fix this will be to move this sql into the PHP for loop and check for the specific assignment+user combination. Without your database scheme im not sure the files used but something like:
<?php
$shcat=mysqli_query($con,"SELECT * FROM assignment INNER JOIN subject ON assignment.as_subject=subject.s_id WHERE s_course='$cr' and a_semester='$sems'");
$cnt=1;
while($row=mysqli_fetch_array($shcat)){
$stat = 0;
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user' AND a_name=".$row['a_name']);
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<tr>
<td class="column0"><?php echo $cnt; ?></td>
<td class="column1"><?php echo $row['a_name'];?></td>
<td class="column2"><?php echo $row['s_name'];?></td>
<td class="column2"><?php echo $row['date'];?></td>
<td class="column0"><a target = "_blank" href="../faculty/assignment/<?php echo $row['image']?>"><i class="fa fa-eye"></i></a></td>
<?php
if ($stat == 0){ ?>
<td class="column0"><i class="fa fa-times" title="Not Submitted"></i></td>
<?php }
else {?>
<td class="column0"><i class="fa fa-check" title="Submitted"></i></td>
<?php }?>
</tr>
<?php $cnt=$cnt+1;
} ?>

Add a button with elseif $row?

I need to place a button that says "Yes", but I need that when clicking on the button take the user to a web page, here is the php code for them to review:
<table cellpadding="1" cellspacing="1" id="member">
<thead>
<tr>
<th colspan="10">Players Not Activated</th>
</tr>
<tr>
<td class="on">#</td>
<td class="on">ID</td>
<td class="on">Username</td>
<td class="on">Email</td>
<td class="on">Tribe</td>
<td class="on">Activation Code</td>
<td class="on">Act 2??</td>
<td class="on">Time</td>
<td class="on">PLAY</td>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM ".TB_PREFIX."activate";
$result = mysqli_query($GLOBALS["link"], $sql);
while($row = mysqli_fetch_assoc($result))
{
$i++;
if($row['tribe'] == 1) {$tribe = "Roman"; }
elseif($row['tribe'] == 2) {$tribe = "Teuton"; }
elseif($row['tribe'] == 3) {$tribe = "Gaul"; }
echo "
<tr>
<td>".$i."</td>
<td>".$row['id']."</td>
<td>".$row['username']."</td>
<td>".$row['email']."</td>
<td>".$tribe."</td>
<td>".$row['act']."</td>
<td>".$row['act2']."</td>
<td class=\"hab\">".date('d:m:Y H:i', $row['timestamp'])."</td>
</tr>";
}
?>
</tbody>
</table>
I need the button to appear where it says "PLAY"
and the destination url i want is this link:
<a href="activate.php?id=<?php echo $_GET['id']; ?>
I have tried 4 days ago to be able to make it work but whenever I modify the file I receive a blank error page.

I want to delete multiple images from source folder through PHP but the code for deleting the multiple images from the folder is not working

<?php include("header.php"); ?>
<?php
if (#$_POST['delete']=="Delete"){
$count=count($_POST['delbx']);
for($i=0;$i<$count;$i++){
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resulty = mysqli_query($conn, $delete) or die(mysql_error());
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$path="admin/".$rowdy['a_image'];
echo $path;
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
} ?>
<div class="table-responsive">
<table class="table">
<caption>All Users</caption>
<?php
$sql = "SELECT a_id, a_name, a_phone, a_password, a_role, a_mail, a_image FROM admin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {?>
<thead>
<tr>
<th><form action="view_user.php" method="post"><input name="delete" type="submit" id="delete" value="Delete"></th><th>S. No.</th> <th>Name</th> <th>Phone No.</th> <th>Mail Id</th> <th>Role</th> <th>Password</th> <th>Image</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()) { ?>
<tbody>
<tr>
<th scope="row">
<?php echo $row["a_id"]; ?>
</th>
<td align="center" bgcolor="#FFFFFF">
<input name="delbx[]" type="checkbox" id="delbx[]" value="<?php echo $row["a_id"]; ?>" />
</td>
<td>
<?php echo $row["a_name"]; ?>
</td>
<td>
<?php echo $row["a_phone"]; ?>
</td>
<td>
<?php echo $row["a_mail"]; ?>
</td>
<td>
<?php echo $row["a_role"]; ?>
</td>
<td>
<?php echo $row["a_password"]; ?>
</td>
<td>
<img src="admin/<?php echo $row["a_image"]; ?>" width="60" height="40">
</td>
<th>
Edit
</th>
</tr>
</tbody>
<?php
}
} else {
echo "0 results";
}?>
</table>
</form>
</div>
<?php include("footer.php"); ?>
The code I mention is not deleting the multiple images from the source folder but deleting the multiple data from database whereas I am trying to delete images from the source folder along with data please help thanks in advance
One of the problem is you are deleting the row and trying to select image column from the deleted row.. dont use user supplied variables directly in your query
your code should be
for($i=0;$i<$count;$i++){
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
if(mysqli_query($conn, $delete)){
$path="admin/".$rowdy['a_image'];
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
}

Php help to remove remove td line

Hello i just create my support system and i get ticket detalis with echo but it show all td line as picture i need to show only the first line
my code
<table cellpadding="0" cellspacing="0" border="0" class="data">
<thead>
<tr>
<th width="115"><p2>Ticket ID</p2></th>
<th width="90"><p2>Status</p2></th>
<th width="160"><p2>Ticket Topic</p2></th>
<th width="160"><p2>Client</p2></th>
</tr>
</thead>
<tr> <?php
echo "<td><a href='?page=ticket&id=".$row[id]."'</a>#".$row[id]."</td>";
echo "<td>".$row[status]."</td>";
echo "<td>".$row[naslov]."</td>";
$userr = mysql_query('SELECT * FROM client WHERE clientid='.$row["user_id"].'');
$useri = mysql_fetch_array($userr);
echo "<td><a href='clientsummary.php?id=".$row[user_id]."'</a>".$useri['firstname']." ".$useri['lastname']."</td>";
?>
</tr>
you need to move your table header outside the loop. without seeing the mechanics of the loop, I'm guessing it will need to look similar to this:
<table cellpadding="0" cellspacing="0" border="0" class="data">
<thead>
<tr>
<th width="115"><p2>Ticket ID</p2></th>
<th width="90"><p2>Status</p2></th>
<th width="160"><p2>Ticket Topic</p2></th>
<th width="160"><p2>Client</p2></th>
</tr>
</thead>
<tbody>
<?php include 'missing.query.on.other.script.php';
while ($row = mysql_fetch_array($whatever_resource)){
?>
<tr> <?php
echo "<td><a href='?page=ticket&id=".$row[id]."'</a>#".$row[id]."</td>";
echo "<td>".$row[status]."</td>";
echo "<td>".$row[naslov]."</td>";
$userr = mysql_query('SELECT * FROM client WHERE clientid='.$row["user_id"].'');
$useri = mysql_fetch_array($userr);
echo "<td><a href='clientsummary.php?id=".$row[user_id]."'</a>".$useri['firstname']." ".$useri['lastname']."</td>";
?>
</tr>
<?php } // end while ?>
</tbody>
</table>

If IDs from two different tables are equal, display name from another table

I'm writing a code for my little admin panel, and since I'm not that advanced of a coder, I'm experiencing some troubles with getting a name using two different tables.
Here's my code so far:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
include 'db_connect.php';
$sql = "SELECT * FROM $tbl_name WHERE is_dead='0'";
$result=mysql_query($sql);
?>
<title>Title</title>
<center><img src="header.png"></center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<? include 'menu.php';?>
</tr>
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Unique ID</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Last Online</strong></td>
<td align="center"><strong>Options</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['id']; ?></td>
<td><? if ($rows['unique_id'] == 7815684) { echo '<font color="blue"><b><u>7815684</u></b></font>'; }
elseif ($rows['unique_id'] == 2312964) { echo '<font color="blue"><b><u>2312964</u></b></font>'; }
else { echo $rows['unique_id']; } ?></td>
<td><? echo $rows['model']; ?></td>
<td align='center'><font color="green"><b><? echo $rows['last_updated']; ?></b></font></td>
<td align="center">update
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
So what I'm trying to do is to get user name, using two tables $tbl_name and $prtalbe using their unique_id. So, if unique_id from $tbl_name equals unique_id from $prtable, I want to show user's name from $prtalbe.
I've been trying another sql query:
$sql = "SELECT * FROM $tbl_name, $prtable WHERE $tbl_name.unique_id = $prtable.unique_id;
$result=mysql_query($sql);
Then doing while loop to get it working
while($rows=mysql_fetch_array($result)){
$rows['name'];
}
and it did work actually, but it didn't want to put it right into my code, since ID from $prtable and $tbl_name are different.
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN you are fetching all rows from each table and combining them given the ON condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp

Categories