How do i delete the records from the table? - php

I am using a form to get the input of username and password to store the value into the database, once the form is submitted i have defined a table to show all the values from the users table, it have 3 fields (id, name, pass) i want to delete each record by it's id .
i am fetching the data from the users table by using the following code:
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "</tr>"; }
i want to add the delete hyperlink to delete the particular records by id.
i tried using the following code and i couldnt achieve it.
if(mysql_num_rows($result_select) > 0) {
if(isset($_POST['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_POST['id'];
$result_delete = mysql_query($query_delete);
}
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
}
I am a newbie to programming, i would appreciate if someone explain me in simple words.. thank you :)

First of all put your delete code in your page like this:
if(isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['del']); $i++)
{
// check which records to delete
if (isset($_POST['del'][$i]))
{
$query_delete = "DELETE FROM users WHERE id = " . (int) $_POST['del'][$i];
$result_delete = mysql_query($query_delete) or die(mysql_error());
}
}
echo 'Record Deleted !!' . '<br /><br />';
}
Later put your select code and modify it like this:
echo '<form action="" method="POST">';
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\"></td>";
echo "</tr>";
}
echo '<input type="submit" name="submit">';
echo '</form>';

You need to change $_POST to $_GET, so this should work.
if(isset($_GET['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_GET['id'];
$result_delete = mysql_query($query_delete);
and also put values of attributes inside double quotes, like this
echo '<td>Delete</td>';

Related

PHP Form - Update database row using one or more checkboxes

Currently stuck trying to configure a php page to update a MySQL database row "orderStatus" by checking one or more checkboxes. It should update the orderStatus of the selected row(s) to "validated" once the form is submitted. It's updating only the first row of the table likely due to $orderID = $_POST['id']; in the validate.php snippet. What I tried to do is retrieve all the orderIDs of the rows that have been checked and assign it to the variable/array $orderID, so that the orderStatus is changed only for those rows.
Here is the HTML:
<html>
<header>
<title>Validate an Order</title>
</header>
<body>
<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");
echo "<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<form action='validate.php' method='post'><input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'></form>";
mysqli_close($con);
?>
</body>
</html>
And here is the PHP for the form:
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$orderID = $_POST['id'];
if(isset($_POST['validate'])){
foreach($_POST['validate'] as $validate){
mysqli_query($con,"UPDATE orders SET orderStatus = '$validate' WHERE orderID = $orderID");
}
}
mysqli_close($con);
?>
Here's what the page looks like.
Validate an Order
Any help is appreciated!
Problem is Form is inside while loop.When you're using with you need to either put the tags completely outside the , or have the entire inside one . Any other structure breaks the syntax of the and will be ignored by the browser, or rendered incorrectly.
Please try this
<html>
<header>
<title>Validate an Order</title>
</header>
<body>
<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");
echo "<form action='validate.php' method='post'>
<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'>"
. "</form>";
mysqli_close($con);
?>
</body>
</html>
Thanks all! I wound up changing the input checkbox tag in the HTML to: <input type="checkbox" name="vid[]" id="validate" value='.$row['orderID'].' This grabs the orderIDs of those rows selected into the vid[] array.
Then in validate.php, the query sets the status to "validate" using $vid from foreach():
// if the vid array exists
if(isset($_POST['vid'])) {
// Loop through vid array "containing orderIDs" and set orderStatus to "validated" only for those orderIDs
foreach($_POST['vid'] as $vid) {
mysqli_query($con,"UPDATE orders SET orderStatus = 'validated' WHERE orderID = '$vid'");
}
}

how to use 2 fetch_assoc in one while loop?

i need list something like this.
First table is display By using the this code.
<?php
if (isset($_REQUEST['asign'])) {
include 'includes/connection.php';
$sql12= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
$query = "SELECT * FROM allinone WHERE flag=3 ORDER BY id ASC";
$rs = $cid -> query($query);
$n = $rs -> num_rows;
echo "<br /><span style='color:red;'><center>$n records found</center></span>";
echo "<div id='record'>";
echo "<table border='1' width='80%' align='center' cellpadding='0' cellspacing='0' id='unsolTable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Ticket Number</th>";
echo "<th>KOID</th>";
echo "<th>PROBLEM</th>";
echo "<th>COMMENT</th>";
echo "<th>DATE</th>";
echo "<th>TIME</th>";
echo "<th>STATUS</th>";
echo "<th>SOLVED BY</th>";
echo "<th>Assign</th>";
echo "<th>Assigned to</th>";
echo "</tr>";
while ($row = $rs -> fetch_assoc() ) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['token'] . "</td>";
echo "<td>" . $row['koid'] . "</td>";
echo "<td>" . $row['problem'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['solvedBy'] . "</td>";
echo "<td><a href='assign.php?qType=technical&name=anshul&id=" . $row['id'] . "'>anshul</a>
<br />
<a href='assign.php?qType=technical&name=kiran&id=" . $row['id'] . "'>kiran</a>
<br />
<a href='assign.php?qType=technical&name=akhilesh&id=" . $row['id'] . "'>akhilesh</a></td>";
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
echo "</tr>";
}
echo"</table>";
echo "</div>";
}
?>
On the another side is list on the assign i don't want to write it manually just get from the database how can i do this??
$sql= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
while ($row=$sql->fetch_assoc()) {
$name= $row['name'];
}
this is the another code i want to add in first code.
Nothing prevents you from executing another SQL query inside you while loop; just replace the echo...anshul...echo...kiran... part with another while loop containing the code you already provided.
You just have to make sure that you use different variables, otherwise your $row variable is overwritten by the second query. Hence, you can do something like this:
// ...
echo "<td>" . $row['solvedBy'] . "</td>";
$resultSetAssignedUsers = $cid->query("SELECT name FROM user WHERE designation = 'Technical' AND status = '1'");
while ($rowAssignedUsers = $resultSetAssignedUsers->fetch_assoc()) {
echo "<td><a href='assign.php?qType=technical&name=" . $rowAssignedUsers['name'] . "&id=" . $row['id'] . "'>" . $rowAssignedUsers['name'] . "</a>";
}
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
// ...

How to capture the values in a row

I have a dynamic table and I want to make sure when the delete button is pressed, you have to remove the corresponding values ​​in the table of the database.
You must fetch the id using the code below and an anchor tag to refer to your_script.php that process the deletion associated with the variable id that contains the id of data
$query = mysqli_query($your_connection, "SELECT * FROM your_table") or exit(mysqli_error);
while($rows =mysqli_fetch_array($query)): //fetch data from the table
$id = $rows['id']; //get id of specific data
$product = $rows['product'];
$seller = $rows['seller'];
$quant = $rows['quantity'];
$price = $rows['price'];
echo "<tr>";
echo "<td>" . $product . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $seller . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $quant . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "</tr>";
//anchor tag href to your_script.php with the variable $id attached to it then use javascript to confirm delete or not
echo "<tr>";
echo "<td>
Delete <td>";
echo </td> ";
echo "</tr>";
endwhile;

Beginner: Delete row from Table and Database PHP/HTML/MySQL

Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}

Passing array and deleting from MySQL table

I have a table in mysql and I'm printing it on screen by this code:
include ('mysql_con.php');
mysql_select_db("jaz", $con);
$res1 = mysql_query("SELECT * FROM hovno ORDER BY id DESC");
echo "<form action='delete_from_db.php' method='POST'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='6'>" . "<input type='submit' value='Delete Article'>" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . "<b>" . "Delete" . "</b>" . "</td>";
echo "<td>" . "<b>" . "ID" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Date" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Title" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Preview" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Author" . "</b>" . "</td>";
echo "</tr>";
while($a=mysql_fetch_array($res1)) {
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='checkbox[]' value='" . $a["id"] . "'>" . "</td>";
echo "<td>" . $a["id"] . "</td>";
echo "<td>" . $a["created"] . "</td>";
echo "<td>" . $a["title"] . "</td>";
echo "<td>" . $a["preview"] . "</td>";
echo "<td>" . $a["author"] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
So I'm putting id's as checkbox values into array.
In another php file I wan't to use this id's in array to delete this records with those id's. I don't know what code to use for that buy i tried this:
<?php
include ('mysql_con.php');
mysql_select_db("jaz", $con) or die(mysql_error());
$del[]=$_POST["checkbox"];
$res1 = mysql_query("SELECT * FROM hovno WHERE id='$del'") or die(mysql_error());
while($a = mysql_fetch_array($res1)){
$b = $a["id"];
mysql_query("DELETE FROM hovno WHERE id ='$b'") or die(mysql_error());
}
?>
Any idea how to actually make it work? Thanks
Although I'm not sure about PHP, if you get the list of IDs, separated by commas you can use SQL operator "in". Something like this:
$res1 = mysql_query("SELECT * FROM hovno WHERE id in (1,2,3)")
comment about variable names:
A jestli si ty promenne slusne nepojmenujes, tak se v tom uz nikdy nevyznas ;)
<?php
$del = $_POST['checkbox'];
$idsToDelete = implode($del, ', ');
$res1 = mysql_query("SELECT * FROM hovno WHERE id in ($idsToDelete)");
?>
foreach($_POST['checkbox'] as $key => $value) {
// delete query here
}

Categories