Multiple delete through checkout - php

I am stuck in one of my application module. I have to set multiple delete options in my application.
I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:
<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
</head>
<body >
<?php
$con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
echo "<table border='1' style='
background-color: white;'>
<tr>
<th></th>
<th>id</th>
<th style='padding-left: 11px;'>Lead Generated Date </th>
<th>name</th>
<th>email</th>
<th>contacts</th>
<th>requirement</th>
<th style='display:none;'>Company name</th>
<th style='display:none;'>Address</th>
<th>Next Follow-Up date</th>
<th>Final_Details</th>
<th style='display:none;'>Status</th>
<th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
<th>EDIT</th>
<th>Follow-up History</th>
<th>Add Follow-up</th>
<th>Delete Record</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['contacts'] . "</td>";
echo "<td>" . $row['requirement'] . "</td>";
echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
echo "<td style='display:none;'>" . $row['address'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['final_details'] . "</td>";
echo "<td style='display:none;'>" . $row['status'] . "</td>";
echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";
$count=mysqli_num_rows($result);
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM Persons WHERE id='$del_id'";
echo $sql;
$result2 = mysqli_query($con,$sql);
}
// if successful redirect to delete_multiple.php
if($result2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
}
}
mysqli_close($con);
echo "</table>";
echo "</form>";
?>
<br><br>
[ Back To Home ]
</body>
</html>
I am stuck, the query is not working I think. I don't know what I am missing.

Try replacing
$sql = "DELETE FROM Persons WHERE id='$del_id'";
With
$sql = "DELETE FROM Persons WHERE id=$del_id";
I'm assuming the ID is a number so the single quotes aren't needed.

Related

PHP - Code is create individual tables for each record from database

I'm trying to create one table that will loop through all the records from my database but it seems to be create individual tables for each record instead.
$query = 'select * from images';
$result = mysqli_query($connection,$query);
$row_count = mysqli_num_rows($result);
for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';
foreach($row as $next) {
{
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
echo '</section>';
}
Ideally, you would loop around the tr
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
foreach($row as $next) {
{
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
// all the other columns
echo "</tr>";
}
echo "</table>";

Update the php after deleting in database

This is The main Page where the user_list.php gets $txt to query "where Like".
<?php
include_once('dbConfig.php');
$result = mysqli_query($conn,"SELECT * FROM compid");
<title>Computer Record</title>
</head>
<body><thead>
<tr>
<th id='time'> </th>
<th>Computer ID</th>
<th id='time'>ComputerName</th>
<th id='title'>Status</th>
<th id='cost'>IP</th>
<th id='size'>OS</th>
<th id='author'>RAM</th>
<th id='author'>CPU</th>
<th id='author'>Machine Model</th>
<th id='author'>Serial Number</th>
<th id='author' >Remarks</th>
<th id='author'>Application</th>
<th id='author'>User</th>
<th id='author'>Delete</th>
<th id='author'>Edit</th>
</tr></thead>
";
while($row = mysqli_fetch_array($result))
{
echo"<tbody>";
echo "<td><input type='checkbox' name='Days[]' value=". $row['cid'] ."> </td>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['cname'] . "</td>";
echo "<td>" . $row['stat'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['os'] . "</td>";
echo "<td>" . $row['ram'] . "</td>";
echo "<td>" . $row['cpu'] . "</td>";
echo "<td>" . $row['mmodel'] . "</td>";
echo "<td>" . $row['snumber'] . "</td>";
echo " <td>" . $row['remarks'] . "</td>";
echo "<td ><a href='App_List.php?cname=" . $row['cname'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/view.png' style='border-style: none'></a></td>";
echo "<td><a href='User_List.php?cname=" . $row['cname'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/user_group.png' style='border-style: none'></a></td>";
echo "<td><a href='del_Main.php?ID=" . $row['ID'] . "' onclick='return checkDelete()'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/erase.png' style='border-style: none'></a></td>";
echo "<td><a href='Edit_Main.php?ID=" . $row['ID'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/edit.png' style='border-style: none'></a></td>";
}
echo "</table>";?>
?>
This is User_List.php
if(isset($_GET['cname'])) {
$txt= $_GET['cname'];
$result = mysqli_query($con,"SELECT * FROM user Where cname like '$txt'");
echo"<body bgcolor='#696969'>
<input type='submit' value='Export' method='post'>
</head>";
echo "<table border='1'>
<tr>
<th>Users</th>
<th> </th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$one = $row['cname'];
echo "<tr>";
echo "<td>" . $row['user'] . "</td>";
echo "<td><a href='User_List.php?ID=" . $row['ID'] . "' onclick='return checkDelete()'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/erase.png' style='border-style: none'></a></td>"; echo "</tr>";
echo "</tr>";
}
echo "</table>";
This is my delete button in table how can i update the page this is the delete.php:
if(isset($_GET['ID'])) {
$result = mysqli_query($conn,"DELETE FROM `SGSCOMP`.`user` WHERE `user`.`ID` = ".$_GET["ID"]);
}
How to get back to the User List
You can do a header redirect directly after the update-query in your delete.php.
header("location:User_List.php")
This will redirect the user back to User_List.php.
header() is used to send a raw HTTP header. See the ยป HTTP/1.1 specification for more information on HTTP headers.
To go back to User_List.php, you can simply use header(). This will redirect users after running mysqli_query.
You should check if the row was deleted successfully by using mysqli_affected_rows(). This will return the number of rows affected, ie if 1 row was deleted, it will return 1.
if(isset($_GET['ID'])) {
$result = mysqli_query($conn,"DELETE FROM `SGSCOMP`.`user` WHERE `user`.`ID` = ".$_GET["ID"]);
if (mysqli_affected_rows($conn) > 0) {
header("Location:User_List.php");
exit;
} else {
// row was not deleted successfully
}
}
More information on header() at http://www.w3schools.com/php/func_http_header.asp.
For User_Main.php, it should be:
You cannot put <?php tags inside of <?php tags.
<?php
include_once('dbConfig.php');
$result = mysqli_query($conn,"SELECT * FROM compid");
?>
<title>Computer Record</title>
</head>
<body><thead>
<tr>
<th id='time'> </th>
<th>Computer ID</th>
<th id='time'>ComputerName</th>
<th id='title'>Status</th>
<th id='cost'>IP</th>
<th id='size'>OS</th>
<th id='author'>RAM</th>
<th id='author'>CPU</th>
<th id='author'>Machine Model</th>
<th id='author'>Serial Number</th>
<th id='author' >Remarks</th>
<th id='author'>Application</th>
<th id='author'>User</th>
<th id='author'>Delete</th>
<th id='author'>Edit</th>
</tr></thead>
<?php
while($row = mysqli_fetch_array($result))
{
echo"<tbody>";
echo "<td><input type='checkbox' name='Days[]' value=". $row['cid'] ."> </td>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['cname'] . "</td>";
echo "<td>" . $row['stat'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['os'] . "</td>";
echo "<td>" . $row['ram'] . "</td>";
echo "<td>" . $row['cpu'] . "</td>";
echo "<td>" . $row['mmodel'] . "</td>";
echo "<td>" . $row['snumber'] . "</td>";
echo " <td>" . $row['remarks'] . "</td>";
echo "<td ><a href='App_List.php?cname=" . $row['cname'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/view.png' style='border-style: none'></a></td>";
echo "<td><a href='User_List.php?cname=" . $row['cname'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/user_group.png' style='border-style: none'></a></td>";
echo "<td><a href='del_Main.php?ID=" . $row['ID'] . "' onclick='return checkDelete()'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/erase.png' style='border-style: none'></a></td>";
echo "<td><a href='Edit_Main.php?ID=" . $row['ID'] . "'><img src='http://10.9.57.129:8888/SGSComputer5.3/FINAL/images/edit.png' style='border-style: none'></a></td>";
}
echo "</table>";
?>
For User_List.php
You didn't close if(isset($_GET['cname'])) {, with another curly bracket.
For delete.php, it should be:
There's no need to concat the string as it's reductant.
if(isset($_GET['ID'])) {
$result = mysqli_query($conn, "DELETE FROM `SGSCOMPuser` WHERE `userID` = '$_GET["ID"]');
}

Get table value html

I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}

Output not displaying all rows

Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";

Approve Form submit

I have trouble with creating an approval form as am still php beginner,
the idea is
user submit a form am setting a default value"0" in the approved row at the table..
so behind the scenes the admin shows all members from this table where approved="0"
and this is the code
<code>
<?php
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</code>
I just want to add checkbox for every table user and when checked thier status changed to 1 in approved column
thanks all
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
<th>Update</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['ID']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
Now comes process.php
if(isset($_POST['approve'])){
if(isset($_POST['check'])){
foreach ($_POST['check'] as $value){
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die (mysql_error());
}
}
}
though you are using mysql_* functions here, i recommend you to use PDO
EDIT:
As per your request, this is the update.
Change this code in your admin panel script:
echo "<input type='submit' name='approve' value='approve'/>";
Delete the above line and add this instead:
echo "<input class='action' type='button' name='approve' value='approve' />";
echo "<input class='action' type='button' name='edit' value='edit' />";
echo "<input class='action' type='button' name='delete' value='delete' />";
echo "<input type='hidden' name='action' value='' id='action' />"; //Action (edit, approve or delete) will be set here which will be passed as POST variable on form submission
Now you will need some javascript to do some tricks.
Add the following code preferably head section in your admin panel script
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.action').click(function(){
var action = $(this).attr('name');
$('#action').val(action);
$(this).closest('form').submit();
})
})
</script>
Now comes the modification in process.php file
if (isset($_POST['approve'])) {
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $value) {
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die(mysql_error());
}
}
} elseif(isset($_POST['edit'])){
//do the edit things here
} elseif(isset($_POST['delete'])){
foreach ($_POST['check'] as $value){
$sql = "DELETE FROM post WHERE ID=$value";//modify it
mysql_query($sql) or die(mysql_error());
}
}
NOTE
You may not want to mutiple checkbox for edit. You just need to tweak the javascript code above a little and it'l send the ID as a post variable on form submission from which you can retreive the details for one entry and then edit functions will come. I'l leave it to you. try it, post your trial code here and i'l give you a solution if it doesn't work.

Categories