updating values in a table by using a button - php

So I have a list of items and for each item, there is a remove and update button, the remove will remove the row from the database, and the update button will change the quantity.
But the problem is, I can only update or remove the last item in the list. and I have tried many things but with no solution.
Any suggestions?
The sql query and the form:
$totaloftotal=0;
try{
require('connection.php');
$sql="select i.item_name, c.qty, i.item_price, c.iid, i.item_photo, i.item_qty, c.cart_id FROM items i, cart c WHERE i.item_id=c.iid and uid=23 ";
$rs=$db->query($sql);
if($rs->rowCount()==0){
echo"CART EMPTY!!";
} else{
?>
<thead>
<tr>
<th>Update</th>
<th>Remove</th>
<th>Image</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($row = $rs->fetch()){
$total=0;
$total+=$row[2];
?>
<form method="post" action="updatecart.php">
<td><button class="btn" type="submit" name="update" >Update</button></td>
<td><button class="btn" type="submit" name="remove" >Remove</button></td>
<?php echo"$row[1]"?>
<input type="hidden" name="itemid" value=<?php echo"$row[3]"?>>
<input type="hidden" name="cartid" value=<?php echo"$row[6]"?>>
<td>
<a href="product_detail.html">
<img alt="" src="photos/<?php echo"$row[4]"?> " width= 100 height=100>
</a>
</td>
<td><?php echo"$row[0]"?></td>
<td>
<input type="number" id="quantity" name="quantity" min=1 max=<?php echo"$row[5]"?> value=<?php echo"$row[1]"?>></td> td>
<?php echo"$row[2]"?><</td>
<?php $total = $total * $row[1]?>
<td><?php echo"$total"?></td>
</tr>
<?php $totaloftotal+=$total;?>
<?php
}
//echo"$totaloftotal";
}
} catch ( PDOException $e ){
die($e->getMessage() );
}
?>
and here is the updatecart.php which is supposed to make the changes:
<?php
print_r($_POST);
extract($_POST);
if(isset($update)){
try{
require('connection.php');
$qty= $_POST["quantity"];
$itemid= $_POST["itemid"];
$cartid= $_POST["cartid"];
$qty= intval($qty);
$sql2= "update cart set qty=$qty where iid=$itemid and uid=23";
$x = $db->exec($sql2);
$db=null;
header("location:cart.php");
}catch (PDOException $e){
die( $e->getMessage() );
}
}
else{
try{
require('connection.php');
$sql2= "delete from cart where iid=$itemid";
$x = $db->exec($sql2);
$db=null;
header("location:cart.php");
}catch ( PDOException $e ){
die($e->getMessage());
}
}
?>

try use while : and endwhile
...
<tbody>
<tr>
<?php
while($row = $rs->fetch()):
?>
<?php
$total=0;
$total+=$row[2];
?>
<form method="post" action="updatecart.php">
<td> <button class="btn" type="submit" name="update" >Update</button></td>
<td> <button class="btn" type="submit" name="remove" >Remove</button></td>
<?php echo"$row[1]"?>
<input type="hidden" name="itemid" value="<?php echo $row[3]?>">
<input type="hidden" name="cartid" value="<?php echo $row[6]?>">
<td><img alt="" src="photos/<?php echo"$row[4]"?> " width= 100 height=100></td> <td><?php echo"$row[0]"?>
</td>
<td><input type="number" id="quantity" name="quantity" min=1 max=<?php echo"$row[5]"?> value=<?php echo"$row[1]"?>></td> td><?php echo"$row[2]"?><</td>
<?php $total = $total * $row[1]?> <td><?php echo"$total"?></td>
</tr>
<?php $totaloftotal+=$total;?>
<?php endwhile ?>
//echo"$totaloftotal";
.....

Related

PHP Array and foreach Combination calculation

In a quiz app, I am taking user answer using a form. I am retrieving correct answer from database table. I want to compare the correct answer with the user's answer and count how many answer was right and how many answer was wrong.
Here is my form:
<form id="question" class="" action="quiz_ans.php" method="post">
<table id="quiz-question" align="center" class="row-border compact order-column stripe">
<input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
<thead>
<?php
if($QuizQuestions) {
$i=1;
foreach($QuizQuestions as $row):
?>
<tr>
<th><?php echo $i; ?>. <?php echo $row->Question; ?>
<br>
<?php if(isset($row->Screenshot)) { ?>
<img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
<?php } ?>
</th>
</tr>
</thead>
<tbody>
<?php if(isset($row->Option1)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option2)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option3)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option4)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
</tr>
<?php } ?>
<tr>
<td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
</tr>
<?php if(isset($row->Id)) { ?>
<tr>
<td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
</tr>
<?php } ?>
</tbody>
<?php
$i++;
endforeach;
}
?>
</table>
<br>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
I am getting the user answer from the form submit:
$NumberofQuestions = $_POST['NumberofQuestions'];
$ans = implode("", $_POST['AnswerId']);
I am retreiving the correct answer from the database table:
try {
$sql = "CALL spQuizAnswers(:quiz_num)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':quiz_num', $quiz_num, PDO::PARAM_STR, 50);
$stmt->execute();
$QuizAns=$stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $e) {
die("Error occurred:" . $e->getMessage());
}
I am comparing the user's answer and the correct answer:
for ($i=0; $i<$NumberofQuestions; $i++) {
if($QuizAns) {
foreach($QuizAns as $row):
if($row->CorrectAns == $ans[$i]){
$right++;
} elseif($ans[$i] == 4){
$not_answered++;
} else {
$wrong++;
}
endforeach;
}
}
$CorrectAnswer = $right;
$WrongAnswer = $wrong;
$NotAnswered = $not_answered;
$TotalQuestion = $right+$wrong+$not_answered;
It does not give correct calculation. For 5 questions it gives $TotalQuestion=25.
How can I achieve the correct calculation? Any help would be much appreciated.

PHP Fetches two rows but uploads file from last row only

Im trying to fetch records from database, then i have to upload a image which should be stored in folder and also link to be updated in table.. Everything working fine only with last row.. For first row its not updating.. Please help me where im goin on.. Below is my code
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" value="<?php echo $amt; ?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
upload.php is taken from https://www.w3schools.com/php/php_file_upload.asp
on your form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" name="fileToUpload []" class="fileToUpload" ></td>
<input type="hidden" value="<?php echo $amt; ?>" name="fileToUploadLink []" class="fileToUploadLink" >
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
on upload.php
<?php
if(isset($_FILES['fileToUpload']['tmp_name'])&&isset($_POST['fileToUploadLink'])){
for($i=0;$i<=(count($_FILES['fileToUpload']['tmp_name'])-1);$i++){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$i],$_POST['fileToUploadLink'][$i]);
}
}
?>
I have a few suggestions:
In your "php.ini" file, search for the file_uploads directive, and set it to On: file_uploads = On
...</button><!-- <<-- How are you using this? -->
Could try to echo $amt; at the bottom of the loop to see if the path is correct.
May want to check if the $amt with IF Empty condition/statement and maybe check the condition of your other variables.
$email1 = $_SESSION['email'];
$Vendor_id="SELECT Vendor_id FROM vendors where email = '$email1' ";
$result=mysqli_query($conn,$Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid=$row['Vendor_wallet_id'];
$tid=$row['request'];
$type=$row['amount'];
$pays=$row['status'];
$amt=$row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid;?></td>
<td><?php echo $tid;?></td>
<td><?php echo $type;?></td>
<td><?php echo $pays;?></td>
<td><input type="file" value="<?php echo $amt;?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button> <!-- WHY IS THIS HERE? -->
</td></tr>
<?php
echo $amt; //See if it is correct path
}//END WHILE ?>
</tbody>
</table>
</form>
There are a few ways in which you could do the file upload, one of which would be to make the form a child of a single table cell and have the file field as a direct child of that form. The submit button, to keep the layout, would not be a child of the form and would need to be changed to a simple button input type then use javascript to submit the form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_array( $query ) ) {
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
</form>
</td>
<td><input type="button" value="Submit" name="submit" /></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
var bttns=Array.prototype.slice.call(document.querySelectorAll('input[type="button"][name="submit"]'));
bttns.forEach(function(bttn){
bttn.addEventListener('click',function(evt){
this.parentNode.parentNode.querySelector('form').submit();
}.bind(bttn),false );
});
</script>
Seems like You want to make a form per row.
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td colspan="2">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="Vendor_wallet_id" value="<?php echo $cpid;?>" />
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
<button>Submit</button>
</form>
</td>
</tr>
</form>
and remove <form> tag that wraps table

Html table only last row instead of all rows is inserting in mysql database

I am implementing clothing shopping website. I am having a problem when I place order only last row item from the shopping cart HTML table is inserted into the database and not all rows. I want that when I click on place order button all rows values should be inserted in the database. so that customers all selected items in the shopping cart should be placed for order.
Here is the code,
<table align="center" >
<thead>
<tr >
<th>Product</th>
<th>Price</th>
<th>QTY</th>
<th>SubTotal</th>
</tr>
</thead>
<?php
if (!empty($_SESSION["shopping_cart"]))
{
$total=0;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td>
<p>Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_gender"]; ?><br/>
<?php echo $values["item_description"]; ?> </p></td>
<td>PKR
<input type="number" name="price1" id="price1" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty[<?php echo $values["item_id"]; ?>]" id="qty" value="<?php echo $values["item_quantity"];?>"></td>
<input type="hidden" name="qty" value="<?php echo $values["item_quantity"];?>" >
<td >PKR<input type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td>
</tr>
<?php
$total=$total+($values['item_quantity']*$values["item_price"]);
/* $total=$total+$values["item_price"]; */
}
} ?>
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$qry="INSERT INTO order ( order_description , product_code, order_quantity, order_price, customer_name, email, customer_id) VALUES ('$description', '$product_code', '$quantity', '$price', (SELECT name from users where email='$email'), '$email', (SELECT user_id from users where email='$email') ) ";
$result=mysql_query($qry,$con);
if($result)
{
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
}
<table align="center">
<thead>
<tr >
<th>Product</th>
<th>Price</th>
<th>QTY</th>
<th>SubTotal</th>
</tr>
</thead>
<?php
if (!empty($_SESSION["shopping_cart"]))
{
$total=0;
$i = 1;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td style=" text-align: left;" >
<p style="margin-top: -120px; margin-left: 150px;">Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_gender"]; ?><br/>
<?php echo $values["item_description"]; ?> </p></td>
<td style="text-align: left; width: 80px;">PKR<br/>
<input style="width: 70px;" type="number" name="price<?= $i; ?>" id="price<?= $i; ?>" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty_<?= $i; ?>" id="qty_<?= $i; ?>" value="<?php echo $values["item_quantity"];?>"></td>
<td style="text-align: left; width: 80px;"> <b>PKR</b>
<br/> <input style="width: 70px; " type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td>
</tr>
<?php
$total=$total+($values['item_quantity']*$values["item_price"]);
$i++;
/* $total=$total+$values["item_price"]; */
}
} ?>
<input hidden="iTotalProducts" value="<?php echo $i; ?>" name="product_count">
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
//here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$cnt = $_POST["iTotalProducts"];
for($i = 1; $i <= $cnt; $i++) {
// here you can write your insert code
}
if($result)
{
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
} `enter code here`

Printing Submit button for each row

I have problem with my buttons. I will try to explain.
1)I have users db from postgresql and new db in ms sql.
2)Created site with 2 columns in table ("SELECT * from users"-postgresql ): They are id/user
3)Then added new column "Operator" which contain submit buttons and functionality of submit buttons is updating "access" column from ms sql db.
PROBLEM:
It prints all buttons for all data that I have in ms sql(I have 7 rows data in ms sql, it prints 7 buttons for each row), I need to "echo" 1 button for each row which will be changable. If access==1 it should be named Active , else it should be named Diactive.
Here is my code and picture of what I got:
<?php
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$result2 = pg_query($db_connection, "SELECT * from users ORDER by id asc");
while ($row1 = pg_fetch_array($result2))
{
$iddrain= $row1['id'];
//echo $iddrain;
//echo $iddrain;
$q7= "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
//$rs7=$row7['ID_M'];
$rs8=$row7['access'];
//echo $rs8;
//break;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator-ON onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=DIavtive onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
}
?>
</form>
</td>
<?php
}
?>
</tr>
</table>
?>
You are doing typo mistake na dnot giving quote for value attributes near :
try like this :
if($rs8==1)
{
echo '<p><input type="submit" name="uid" value="Operator-ON" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.'" onchange=
"this.form.submit()"></p>';
}
else
{
echo '<p><input type="submit" name="uid" value="DIavtive" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.' onchange="this.form.submit()"></p>';
}
}
I have reditted your code to remove some errors that were making it fail to work;
<?php include ("db.php"); ?>
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
</thead>
<?php
//while ($row = pg_fetch_array($result)) {
<?php foreach( pg_fetch_array($result) as $row ) { ?>
?>
<tbody>
<tr>
<td<?php echo $row['id'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
<form method="POST" action="oper.php">
<?php
$result2 = pg_query($db_connection, 'SELECT * from users ORDER by id asc');
while ($row1 = pg_fetch_array($result2)) {
$iddrain = $row1['id'];
$q7 = "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid = sqlsrv_query($link, $q7, $params, $options);
while ($row7 = sqlsrv_fetch_array($resultid)) {
//$rs7=$row7['ID_M'];
$rs8 = $row7['access'];
if ($rs8 == 1) {
echo '<p><input type="submit" name="uid" value=Operator-ON onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
} else {
echo '<p><input type="submit" name="uid" value=DIavtive onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
}
}
}
?>
</form>
</td>
</tr>
</tbody>
</table>
I`ve changed code and now it is working, problem was additional useless fetch. Here is the code:
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$iddrain= $row['id'];
$q7= "Select * from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
$rs8=$row7['access'];
//echo $rs8;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=Nazeret onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
?>
</form>
</td>
</tr>
<?php
}
?>
</tr>
</table>

Deleting selected item

Good day. I want to delete selected item by the user. But it turns out what ever button clicked by user, the first data row is deleting. Im stuck here. Please help.
This is my code:
HTML
<form method="POST" action="<?php $_PHP_SELF ?>">
<?php
$query = "select * from tblsr";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table table-hover">
<tr>
<th>Softwares</th>
<th>Difficulty</th>
<th></th>
<th></th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody>
<tr class="success">
<td><?php echo $row['Software']; ?></td>
<td><?php echo $row['Difficulty']; ?></td>
<td><input type="submit" class="btn btn-sm btn-success" name="change" value="Change Difficulty"/></td>
<td><input type="submit" class="btn btn-sm btn-danger" name="remove" value="Delete"/></td>
<input type="hidden" name="id[]" value="<?php echo $row['id']; ?>"/>
</tr>
</tbody>
<?php
}
?> </form>
DELETE Query
if(isset($_POST['remove'])){
$count = count($_POST['id']); //get total number of array element
for ($i = 0; $i < $count; $i++) {
$id = $_POST['id'][$i];
$sql = "Delete FROM tblsr
Where id = '$id'";
$success = mysql_query($sql) or die (mysql_error());
mysql_close();
if ($success == TRUE){
?>
<script>
alert('Deleted.');
window.location.href='manageRequest.php';
</script>
<?php
}
}
}
Your code's problem is that you never increase $i ( $i++ ) , and always your delete query affect on the first row ( id ) . you can put $i++ in your while loop to fix it, but I recommend that you simply do like below and don't bother yourself with a loop.
Delete query :
$rm = $_POST['id'];
if (isset($_POST['remove'])) {
foreach ($rm as $key => $value) {
$rm[$key] = "'".$value."'";
}
$sql = "Delete FROM tblsr
Where id IN(" . implode(",", $rm) .")";
die($sql);
$success = mysql_query($sql) or die(mysql_error());
if ($success) {
?>
<script>
alert('Deleted.');
window.location.href = 'manageRequest.php';
</script>
<?php
}
}
your form example for delete users:
<form method="POST" action="<?php $_PHP_SELF ?>">
<?php
$query = "select * from tblsr";
$request = mysql_query($query) or die(mysql_error());
?>
<table class="table table-hover">
<tr>
<th>Softwares</th>
<th>Difficulty</th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($request)) {
?>
<tbody>
<tr class="success">
<td><?php echo $row['Software']; ?></td>
<td><?php echo $row['Difficulty']; ?></td>
<td>
<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"/>
delete
</td>
</tr>
</tbody>
<?php
}
?>
</table>
<button type="submit" name="remove" class="btn btn-success">
delete checked user
</button>
</form>

Categories