Passing array and deleting from MySQL table - php

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
}

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>";
// ...

Sum columns using MySQL/PHP

My question is that why there are blank values for HW1/HW2/HW3 columns when I ran the code in the browser?.
Studentid and Sum columns displayed the code correctly. Any ideal how to fix this?
<?php
$result = mysqli_query($con,"SELECT studentid,SUM(hw1+hw2+hw3)
FROM grade
GROUP BY studentid");
echo "<table border='1'>
<tr>
<th>StudentID</th>
<th>HW1</th>
<th>HW2</th>
<th>HW3</th>
<th>SUM</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['studentid'] . "</td>";
echo "<td>" . $row['hw1'] . "</td>";
echo "<td>" . $row['hw2'] . "</td>";
echo "<td>" . $row['hw3'] . "</td>";
echo "<td>" . $row['SUM(hw1+hw2+hw3)'] . "</td>";
;}
echo "</table>";
mysqli_close($con);
?>
just change your query to this
"SELECT studentid,hw1, hw2, hw3, SUM(hw1+hw2+hw3) as hw
FROM grade
GROUP BY studentid"
Because you did not select the columns, try:
$result = mysqli_query($con, 'SELECT `studentid`, `hw1`, `hw2`, `hw3`, SUM(`hw1`+`hw2`+`hw3`) as `sum` FROM `grade` GROUP BY `studentid`');
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['studentid'] . "</td>";
echo "<td>" . $row['hw1'] . "</td>";
echo "<td>" . $row['hw2'] . "</td>";
echo "<td>" . $row['hw3'] . "</td>";
echo "<td>" . $row['sum'] . "</td>";
}
I'm not sure, since your question is poorly written. But I think you just need to do this to get the values:
SELECT studentid,hw1,hw2,hw3,SUM(hw1+hw2+hw3)

I need to assign a unqiue class to a PHP MYSQL table's TR

How can I assign a class id that is unique for all the <tr>
The id's need to stay stay even when I refresh as I will be using them to style the table.
Here is the code I am using:
while($row = mysql_fetch_array($result2))
{
echo "<tr id='centered' class='"; echo "'";
echo "<td>" . $row['Year_9'] . "</td>";
echo "<td>" . $row['Year_8'] . "</td>";
echo "<td>" . $row['Year_7'] . "</td>";
echo "<td>" . $row['Year_6'] . "</td>";
echo "<td>" . $row['Year_5'] . "</td>";
echo "<td>" . $row['Year_4'] . "</td>";
echo "<td>" . $row['Year_3'] . "</td>";
echo "<td>" . $row['Year_2'] . "</td>";
echo "<td>" . $row['Year_1'] . "</td>";
echo "<td>" . $row['Year_0'] . "</td>";
Perhaps use a counter with a multi-valued class?
$i = 1;
while($row = mysql_fetch_array($result2))
{
$secondClass = 'abc' + $i;
echo "<tr id='centered' class='firstClass $secondClass'>";
...
$i++;
}

How to calculate multiplication value inside the while loop in PHP?

I have one while loop which displays data from the database. Now I want multiply two values in one row and display the result in the same row, the same way multiply the values and display the result in every row. I am not getting the result. Can anyone help me? I am new to PHP.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
$ss=$row['amount'];
echo '<td >'.'<input type="checkbox" name="status" value="" >'.'</td>';
echo '<td >'.'<input type="text" name="qty">'.'</td>';
echo "<td>" . $rr1 . "</td>";
echo "</tr>";
}
From Where you are getting $rr1??
You can have
echo "<td>" . $row['item'] * $row['amount'] . "</td>";
Hope this is what you want...
you can try
printf('amount is %d',$row['item'] * $row['amount']);
reference
Try below :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['item'] * $row['amount']). "</td>";
echo "</tr>";
}
Change the query as
$result= mysql_query("Select id, item, amount, (item * amount) total
from table_name");
then in while loop
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td >" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['total'] "</td>";
echo "</tr>";
}

Categories