Php sqlsrv search POST and GET Method multiple parameters - php

First of all, i want to create a page that show a record of last one week from today date. Then, when user searching, it will show the specific record. The problem now is the pagination that i made clash through the last one week query, so the searching data cannot load its query bcs i set it up as POST method.
$name = null;
$id = null;
if(isset($_POST["search"]))
{
$name = $_POST["name"];
$id = $_POST["id"];
}
if(isset($_GET["search"]))
{
$name = $_GET["name"];
$id = $_GET["id"];
}
$stmt = " SELECT c.* FROM (SELECT ROW_NUMBER() OVER(ORDER BY CustomerID) AS RowID,* FROM customer WHERE Name LIKE '%".$name."%' and ID LIKE '%".$id."%'
WHERE c.RowID > $row_start AND c.RowID <= $row_end";
This is if else statement
if (isset($_POST['search'])) {
if ($num_rows == 0) {
echo "No data ";
echo "<br/ >";
} else {
echo "<table style='border: 1px solid black;><tr>"
. "<th><font color='white'>Student <br> ID</font></th>"
. "<th><font color='white'>Name</font></th>"
. " <th><font color='white'>Date</font></th>"
. "</tr>";
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
echo "<tbody style='border: 1px solid black; border-collapse: collapse; background-color: white'><tr>"
. "<td align='center' for='Student_ID'>" . "<a href='http://localhost:808/jjjl/newdashboard/Studensdf.php?Student_ID=" . $row['Student_ID'] . "'>" . $row['Student_ID'] . "</a>" . "</td>"
. "<td align='left'>" . $row['Name'] . "</td>"
. "<td align='center' for='Dates'>" . $row['Dates']->format('d/m/Y') . "</td>";
}
echo "</tbody";
echo "<br / >";
}
} else {
?>
<div class="col-md-12">
<?php
/*for last one week*/
if ($num_rows1 == 0) {
echo "No data for a week ago";
echo "<br/ >";
} else {
echo "<table style='border: 1px solid black;><tr>"
. "<th><font color='white'>Student <br> ID</font></th>"
. "<th><font color='white'>Name</font></th>"
. " <th><font color='white'>Date</font></th>". "</tr>";
while ($row = sqlsrv_fetch_array($query2, SQLSRV_FETCH_ASSOC)) {
echo "<tbody style='border: 1px solid black; border-collapse: collapse; background-color: white'><tr>"
. "<td align='center' for='Student_ID'>" . "<a href='http://localhost:8080/hhol/nhhhboard/Stukhhtail.php?Student_ID=" . $row['Student_ID'] . "'>" . $row['Student_ID'] . "</a>" . "</td>"
. "<td align='left'>" . $row['Name'] . "</td>"
. "<td align='center' for='Dates'>" . $row['Dates']->format('d/m/Y') . "</td>";
}
echo "</tbody";
echo "<br / >";
}
}
And the form
<form name="frmSearch" class="form-horizontal" action="<?php echo $script; ?>" method="post">
<label class="col-md-4">Student ID</label>: <input type="text" name="studentid" id="txtKeyword" value="<?php echo $studentid; ?>"><br>
<label class="col-md-4">Name</label>: <input type="text" name="name" id="txtKeyword1" value="<?php echo $name; ?>">
<input type="submit" name="search" value="Search"></th>
and pagination
if ($prev_page) {
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$prev_page'><< Back</a> ";
}
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&'>$i</a> ]";
} else {
echo "<b> $i </b>";
}
}
if ($page != $num_pages) {
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$next_page'>Next>></a> ";
}
sqlsrv_close($conn);

Related

Form submits multiple times with one button click, running the related action="" script multiple times

Here's what I'm trying to do:
I'm working on an inventory management system for my work, and the idea is that when we need to order something it's status is set to 'order' in the mySQL database which contains all of our inventory items. We have an 'Order Queue' page that displays all of the things that need ordered. The twist is that we need two different tables for each supplier: one for Purchase Orders and one for Request for Quotes (e.g. buying raw material isn't always a constant price, so a PO and RFQ need a different format). Then for each supplier table there is a submit button that says "Push supplier name PO/RFQ".
Pressing this button takes you to a second page which creates the PO and sends the email to the supplier. I know this isn't the issue here because regardless of what is in this script it will run three times when the button is pressed.
Here's some code:
$suppTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')");
This creates the array storing all the supplier info, and is all correct.
<div class="trackheader" style="border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;">
<h3 style="color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;">Display:</h3>
</div>
<div class="trackcont3" style="border-radius:0px 0px 5px 5px; border: 2px solid #CC3333; margin-bottom:20px;">
<form action="" method="POST" id="rec">
<?php
while($row1 = mysqli_fetch_assoc($suppTable)){
echo "<input type='checkbox' id='" . $row1['name'] . "' name='" . $row1['name'] . "' value='" . $row1['name'] . "' onchange='this.form.submit()'";
if(isset($_POST['' . $row1['name'] . ''])){
echo "checked='checked'";
}
echo ">";
echo "<label for='" . $row1['name'] . "'>" . $row1['name'] . "</label>";
}
?>
</form>
</div>
Please ignore the inline CSS - this will get added to the stylesheet once this actually works...
This is a pretty straightforward snippet: it creates display toggles for each supplier in the database so that they don't all show up at once.
Now for the fun stuff:
<?php
$loopTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')"); //this creates another array with each supplier tied to the user account
while($row2 = mysqli_fetch_assoc($loopTable)){ //this while loop creates two tables for each supplier: one with items that need to be ordered in a PO and another table for RFQ
if(isset($_POST['' . $row2['name'] . ''])){ //controls display from the above form
$display['' . $row2['name'] . ''] = "block";
} else {
$display['' . $row2['name'] . ''] = "none";
}
echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
<h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] . " PO</h3>
</div>";
echo "<div class='trackcont2'><table style='width:100%; !important'><tbody style='width:100%; !important'>
<tr style='width:100% !important;'>
<th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Price</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Total</th>
</tr>";
$i = 0;
$listTablePO = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'PO')"); //this creates an array with all database items from the appropriate supplier which need ordered (and go in the PO table)
$total = 0; //tallies a running total of the price
while($row3 = mysqli_fetch_assoc($listTablePO)){ //this while loop creates the table of all parts that need ordered and their necessary info
if($i % 2 == 0){
echo "<tr class='odd' style='text-align: center;'>";
} else {
echo "<tr class='even' style='text-align: center;'>";
}
echo "<td>" . $row3['part_name'] . "</td>";
echo "<td>" . $row3['description'] . "</td>";
if($row3['order_link'] == null){
echo "<td>" . $row3['supplier_part_no'] . "</td>";
} else {
echo "<td><a href='" . $row3['order_link'] . "' target='_blank'>" . $row3['supplier_part_no'] . "</a></td>";
}
if($row['order_override'] == 0){
echo "<td>" . $row3['order_qty'] . "</td>";
$qty = $row3['order_qty'];
} else {
echo "<td>" . $row3['order_override'] . "</td>";
$qty = $row3['order_override'];
}
echo "<td>$" . sprintf('%.2lf', $row3['price']) . "</td>";
$total = ($total + ($qty * $row3['price']));
echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
echo "</tr>";
$i++;
}
echo "</tbody></table></div>
<form action='/po-mailing/' method='POST' name='" . $row2['name'] ."PO'>
<input type='hidden' value='" . $row2['name'] . "' name='PO' id='PO'></input>
<button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " PO</button>
</form>
</div>"; //the above snippet handles creating the appropriate form and submit button. All this needs to do is route to the appropriate page (/po-mailing/) and pass the correct supplier name.
//From there I can get all the necessary info from the SQL database and don't need to pass any other info through the form.
//I suspect this is where my issue lies, but all I need is an individual Push PO button for each supplier that directs to /po-mailing/ and passes the supplier info to the page. The problem here is it is doing it three times...
The next code is basically the same as above but for the RFQ table. Skip this part
// the same code is essentially repeated for RFQ for each supplier with minor format changes (it is also irrelevant as I have only been testing with the PO side)
echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
<h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] . " RFQ</h3>
</div>";
echo "<div class='trackcont2'><table style='width:100% !important;'><tbody style='width:100%; !important'>
<tr style='width:100% !important;'>
<th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Last Price</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Estimate</th>
</tr>";
$i = 0;
$listTableRFQ = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'RFQ')");
while($row5 = mysqli_fetch_assoc($listTableRFQ)){
if($i % 2 == 0){
echo "<tr class='odd' style='text-align: center;'>";
} else {
echo "<tr class='even' style='text-align: center;'>";
}
echo "<td>" . $row5['part_name'] . "</td>";
echo "<td>" . $row5['description'] . "</td>";
if($row5['order_link'] == null){
echo "<td>" . $row5['supplier_part_no'] . "</td>";
} else {
echo "<td><a href='" . $row5['order_link'] . "' target='_blank'>" . $row5['supplier_part_no'] . "</a></td>";
}
if($row['order_override'] == 0){
echo "<td>" . $row5['order_qty'] . "</td>";
$qty = $row5['order_qty'];
} else {
echo "<td>" . $row5['order_override'] . "</td>";
$qty = $row5['order_override'];
}
echo "<td>$" . sprintf('%.2lf', $row5['price']) . "</td>";
$total = ($total + ($qty * $row5['last_price']));
echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
echo "</tr>";
$i++;
}
echo "</tbody></table></div>
<form action='/rfq-mailing/' method='POST' id='" . $row2['name'] ."RFQ'>
<input type='hidden' value='" . $row2['name'] . "' name='" . $row2['name'] . "' id='" . $row2['name'] . "'></input>
<button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " RFQ</button>
</form>
</div>";
}
?>
Hopefully someone can spot where I'm screwing up.. FWIW I'm also using Wordpress as a backend for the system (just an easy way to handle accounts, create pages, etc)
I found the solution:
My wordpress theme appears to load the head of each page multiple times, so the issue wasn't with the code itself. On each load it was submitting the info.
Encasing necessary info in if(isset($_POST['submitbuttonexample'])){} fixed the issue.

How to make search based on pickup date and return date?

In the car page,i already input all the data into mysql database(car table).So i want to ease the user to find the car that available on pickup date and return date.So,I made search pickup date and return date in search page.But the error is "could not be execute".
This is using mysql and php.
car.php
</body>
<table align='center' width='40%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<form action="search.php" method="post" enctype="multipart/form-data">
<td><input type="date" name="Pickup_date" placeholder="Pickup_date" style='height:38px' required /></td>
<td><input type="date" name="Return_date" placeholder="Return_date" style='height:38px' required /></td>
<td><input type="submit" name="submitbook" value="Search" style='background-color: blue; border: none; color: white; padding: 10px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px;'/></td>
</div>
</form>
</table>
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo '<input type="hidden" name="pickup_date"
value="'.$pickup_date.'">';
echo '<input type="hidden" name="return_date"
value="'.$return_date.'">';
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
search.php
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search=$_GET['s'];
$query="SELECT * FROM car WHERE date BETWEEN 'pickup_date' AND
'return_date'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100' width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td>" . $row['pickup_date'] . "</td>";
echo "<td>" . $row['return_date'] . "</td>";
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
i expect the output is based on pickup_date and return_date.But the output is "ERROR:Could not able to execute".
You Can check with this , When you have two tables you can use inner join query to fetch the data
SELECT columns
FROM table WHERE pickup_date < 'your_pickup_date' AND return_date >= 'your_pickup_date' ORDER
BY car or date;
Without knowing your schema, it's difficult to provide a detailed solution, but an example of a plausible query might be as follows:
"
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM car c
WHERE pickup_date < :return_date
AND return_date >= :pickup_date
ORDER
BY pickup_date
, car_id;
"

How to make an dropdown menu to sort data from an database

I am working on an application to use on an website. The application let you add job offers to a database to post it on the website and on social media. I have done a lot already but now I am trying to make an sort menu. In my database I have 1 row that is called Status. The Status is an enum with the data Open and Closed so I would like to have a sort option. I would like to sort on Open, Closed and all.
I already have a table and I would like to see the changes on that existed table if that is possible but I have no idea how to do this. Can somebody help me with this? I am using HTML, PHP and as database PHPMyAdmin. Here is my code:
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
width: 120px;
}
</style>
</head>
<body>
<h2>Jobs Overview (Admin)</h2>
<form action="/action_page.php">
<select id="sorting">
<option value="All" >All</option>
<option value="Open">Open</option>
<option value="Closed">Closed</option>
</select>
<br><br>
<?php
include 'Connection.php';
echo "<table>";
echo "<tr>";
echo "<th>" . "jid" . "</th>";
echo "<th>" . "role" . "</th>";
echo "<th>" . "type" . "</th>";
echo "<th>" . "availability" . "</th>";
echo "<th>" . "location" . "</th>";
echo "<th>" . "status" . "</th>";
echo "<th>" . "Verwijder?" . "</th>";
echo "</tr>";
$select = "SELECT * FROM test";
$result = mysql_query($select);
if($result) {
} else {
echo "Error! <br>";
echo mysql_error();
}
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
echo "<td>" . $data["status"] . "</td>";
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
?>
<br><br>
Vacatures Toevoegen
</body>
</html>
Thanks in advance
You can add HTML class to open and closed jobs.
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
if ($data["status"] == 0) {
echo "<td class='open'>" . $data["status"] . "</td>";
} else {
echo "<td class='closed'>" . $data["status"] . "</td>";
}
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
After, this you have to write a jquery script to hide the jobs .on change.
$('#sorting').on('change', function(){
var status = $('#sorting:selected').val();
if (status == "Open") {
$('.closed').hide();
} else if (status == "Closed") {
$('.open').hide();
} else if (status == "All"){
$('.closed').show();
$('.open').show();
}
});​
I hope this solves your problem.
You can make a Ajax post request on select change to get the records and put those records on success in your table.
More about how to do this with Jquery / Ajax here
More about Jquery on change here

PHP, trying to make row ID's work

So I basically been coding this quick stat shower for my game server, but I want it to ID numbers.
<html>
<head>
<meta charset="utf-8">
<title>ExileMod Stats</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$con=mysqli_connect("******","******","******","******"); //server address, username, password, dbname
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check order ascending or descending
if (isset($_GET["order"])) {
$sort = $_GET["order"];
if ($_GET["order"] == "asc"){
$sort = "desc";
$order = "asc";
}
else{
$sort = "asc";
$order = "desc";
}
}
//check filter
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = "name";
}
$list=array('name', 'money', 'score', 'kills', 'deaths', 'uniform', 'vest', 'last_updated_at');
if (in_array($name,$list))
{
//variable ok
}
else
{
$name = "name";
}
$query = mysqli_query($con,"SELECT * FROM account a INNER JOIN player p ON a.uid = p.account_uid ORDER BY a.$name $order");
?><!--//echo "<table border='1'>-->
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table class="table">
<tr>
<?php echo "<th>Player Name</th>";?>
<?php echo "<th>Money</th>";?>
<?php echo "<th>Score</th>";?>
<?php echo "<th>Kills</th>";?>
<?php echo "<th>Deaths</th>";?>
<?php echo "<th>Uniform</th>";?>
<?php echo "<th>Vest</th>";?>
<?php echo "<th>Last Updated</th>";?>
</tr>
<!--//";-->
<?php
while($row = mysqli_fetch_array($query))
{
?><tr class="danger"><?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
© HeroesOfGaming 2016 - 2017
</div>
</body>
</html>
So what I am trying to do is put numbers so it counts 1 2 3 4 5 6 7 to however many players are selected from the database? Hopefully this makes sense.
You can do like this
<?php
$i = 0; // <--- Added this
while($row = mysqli_fetch_array($query))
{
$i++; // <--- Added this
?><tr class="danger"><?php
echo "<td>". $i . "</td>"; // <--- Added this
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

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.

Categories