PHP How to Delete Multiple Records using Checkbox - php

I have a little problem regarding with deleting multiple records in my table. I used checkbox in order to delete them but it doesn't work. I don't know what would be the exact CODE for it.
Here is my PHP code
<?php
echo "<form action='#'>
<fieldset>
<input type='text' name='search' value='' id='searchtalents' placeholder='Search Keywords..' size='40'/>
</fieldset>
</form>";
echo "<form name='tmsform' method='POST' action=''>";
$sql = "SELECT * FROM talentinfo WHERE 1 LIMIT 10";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
echo"<div id='talentinfo'><table id='mr_database' name='myform' style='border:1px solid #fff;' cellspacing=0 cellpading='2' class='pretty'>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th>Mr Tracking Number</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Contact Number</th>
<th>School</th>
<th>Course</th>
<th>Year Graduated</th>
<th>Position Considered</th>
<th>Referred Location</th>
<th>Unit</th>
</tr>
</thead>";
$counter = 40000;
while ($row = mysql_fetch_assoc($result)) {
$filled = (($counter % 2) == 1) ? "style='background-color:#BCD9E1;'" : "" ;
$id = $row['talents_id'];
echo "<tbody><tr {$filled} class='tmsdel'>";
echo "<td><a href ='#' rel='#edit_talents{$counter}'><center><img src='img/edit.gif' width='25' height='21' title='Edit'></center></a></td>";
echo "<td><a href ='#' id=".$row['talents_id'].'&idelete=talents'." class='delete'><center><img src='img/delete.png' width='25' height='21' title='Delete'></center></a></td>";
echo "<td><input type='checkbox' name='checkbox[]' id='check".$row['talents_id']."' value='".$row['talents_id'].'&idelete=talents'."'/></td>";
echo "<td><a href='#' rel='#tracing_number{$counter}' style='text-decoration:none; font-weight:bold; color:#444;'>" . $row ['mr_tracking_number'] . "</a></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact_number'] . "</td>";
echo "<td>" . $row['school'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['year_graduated'] . "</td>";
echo "<td>" . $row['position_considered'] . "</td>";
echo "<td>" . $row['referred_location'] . "</td>";
echo "<td>" . $row['unit'] . "</td>";
echo "</tr></tbody>";
?>
And here is my Javascript
$(function(){
$(document).ready(function(){
});
$("#delete-all").click(function(){
$("#mr_database").remove();
$.ajax({
url: "modules/delete-all.php",
type: "get",
async: false,
data: "check"
});
});
});

Please follow steps:
First you need to apply any class name to your all checkboxes.
Call function on button click for delete then
var allVals = '';
$("#delete-all").click(function(){
$('.checkboxclass :checked').each(function() {
allVals = allVals + $(this).val() + ',';
});
}
Then you need to pass allVals variables in ajax and post to .php file
Like: data: 'ids=' + allVals in $.ajax
In last you can get this variable in php file and do delete process on it.
Like: $ids = explode(',', $_REQUEST['ids'); and use ids in mysql query
Hope this helps you.

Rather simple answer since what i am seeing here is a script to view the table and delete the html in the browser but not the data on the server.
You need to write the modules/delete-all.php script mentioned here:
url: "modules/delete-all.php"
which should contain the " delete * FROM talentinfo " SQL Query.
This however will seamlessly delete all table contents. So you might want to do a prompt:
$("#delete-all").click(function(){
Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)");
if (Check == true) {
$("#mr_database").remove();
$.ajax({
url: "modules/delete-all.php",
type: "get",
async: false,
data: "check"
});
}
});
The delete_all.php should delete all, so just do:
<?php
$sql = "delete * FROM talentinfo ";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
exit;
} else {
print "ok";
}
?>

Related

pass a jquery variable to php code

this is my php code
require 'functions/connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee ORDER BY id asc");
echo "
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
and this is my jquery code
<script>
$('.sort').click(function(){
var value = $(this).attr('data-val');
var field_name = $(this).attr('data-field');
$.post( "getEmployee.php", {value:value, field_name:field_name}, function( data ) {
$('.responstable').html(data);
});
if(value=="asc"){
$x="asc";
}
else{
$x="desc";
}
});
</script>
i would like to pass the variable x from the jquery code to the php so i can order my database asc or desc like this:
result = mysqli_query($conn,"SELECT * FROM employee ORDER BY id $x");
In your jquery code
$.post( "getEmployee.php", { sort: "asc" } );
In your php code using
$_POST['sort']

Having issue showing image returning from database with $.post() jquery and php

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";

MySQL query working in workbench, but does not work via PHP interface

I have a MySQL query that I know to be working in Workbench/Sequel Pro that appears to return empty in my PHP code:
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
if($db_field = mysql_fetch_assoc($result)) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field = mysql_fetch_assoc($result)) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
The result of this code is the first table row (Exam ID, Module ID etc.) is displayed but the table content is not.
I cannot work out why this is happening. Other than the SQL query, the code is identical to code used elsewhere (where it is working fine).
You are doing mysql_fetch_assoc twice. The first time it reads the record and prints the table header. The second time there are no more records so it doesn't print a row.
Change
if($db_field = mysql_fetch_assoc($result)) {
to
if(mysql_num_rows($result) > 0) {
Try this. You are using mysql_fetch_assoc($result); two times, First time it get result and second time there is no result.
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result)
if($db_field != "" ) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}

Multiple delete through checkout

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.

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