Trying to approve, deny, or delete multiple users - php

I am creating a list of users that I would like to be able to either approve, deny, or delete. I don't want to do one by one, because it would be too much work.
I have the following code for the approve, deny, and delete columns:
<tr>
<input type="hidden" name="user_id" value="'.$row['user_id'].'">
<td style="text-align:center; padding:20px; background-color:#DFF0D8;">
<input type="radio" name="approve[]" value="1">
</td>
<td style="text-align:center; padding:20px; background-color:#FCF8E3;">
<input type="radio" name="approve[]" value="0">
</td>
<td style="text-align:center; padding:20px; background-color:#FCDEDE;">
<input type="radio" name="approve[]" value="3">
</td>
</tr>
I want to be able to set the "approved" column in the table "users" in MySQL to 1 if it is approved and to keep it to 0 if it is denied. If "delete" is chosen, then I want to be able to delete that row of data. How can I do this for a list of users? If it was just one by one it would be easy, but I don't know how to do it for multiple users.
I thought of looping through the "approve" array, but I can't change the values in the database because I don't know how to match this to a user_id.
Any help is appreciated.

here is a sample for multi delete:
index.php
<?php include('dbcon.php'); ?>
<form method="post" action="delete.php" >
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong>Check the radion Button and click the Delete button to Delete Data</strong>
</div><thead>
<tr>
<th></th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Address</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from member")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['member_id'];
?>
<tr>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['firstname'] ?></td>
<td><?php echo $row['lastname'] ?></td>
<td><?php echo $row['middlename'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['email'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" class="btn btn-danger" value="Delete" name="delete">
delete.php
<?php
include('dbcon.php');
if (isset($_POST['delete'])){
$id=$_POST['selector'];
$N = count($id);
for($i=0; $i < $N; $i++){
$result = mysql_query("DELETE FROM member where member_id='$id[$i]'");}
header("location: index.php"); } ?>

For multiple users you just need to add a dimension to all your form variable names:
<tr>
<td style="text-align:center; padding:20px; background-color:#DFF0D8;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="1">
</td>
<td style="text-align:center; padding:20px; background-color:#FCF8E3;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="0">
</td>
<td style="text-align:center; padding:20px; background-color:#FCDEDE;">
<input type="radio" name="approve[<?= $row['user_id'] ?>]" value="3">
</td>
</tr>
When the form is posted you will receive something like this:
approve[123]=1&approve[456]=2&...
This will turn $_POST['approve'] into an array:
array(
123 => 1
456 => 2
);
...

Related

Why I only get the value of the last row in my table when I click the 'AddToCart' in other row

I have this code in php
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<form method="POST" action="addtocart.php">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item=mysqli_query($conn,"select * from store");
while($pqrow=mysqli_fetch_array($item)){
$iid=$pqrow['id_item'];
?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td"><input type="hidden" name="item" value="<?php echo $iid; ?>"><input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>"></td>
</tr><?php } ?>
</table></form>
</div>
When I click AddToCart in first row, the value of the last row has been fetch. I want to get the id and the input quantity when I click the AddToCart in 1st row.
As you have only one form tag and multiple inputs with same name on this form, value of next input overwrites previous one. As a result you always have value of the last row.
You can fix it for example with creating a separate form for every row, e.g:
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item = mysqli_query($conn,"select * from store");
while ($pqrow=mysqli_fetch_array($item)) {
$iid=$pqrow['id_item'];?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td">
<!-- Separate form for each record -->
<form method="POST" action="addtocart.php">
<input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>">
<input type="hidden" name="item" value="<?php echo $iid; ?>">
<input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>">
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
In this case you should track with javascript the value of quantity field. Of course, you can move <input type="number"> in a form too, but this will break your markup.
Also, if you're familiar with javascript, you can add some client preprocessing (getting values from inputs of a certain row) and use ajax-request to send these values to server.

How to get the input value from multiple value when button pressed php

i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....

php update radio button state in db

I am new in php, I just starting learn php and have some problems.
In admin panel I have some form for adding info, after that in output I have radio buttons for selecting which row show into website. The problem is in update function the query is ok but it doesn't work, and I need you help.
thanks.
submit if.
if(isset($_POST['update_info'])){
$selected_info=$_POST['selected'];
$selected_id = $_POST['id'];
updateHomeInfo($selected_info, $selected_id);
}
output added rows.
<div class="home-output">
<form action="/admin/" method="post">
<table class="table table-hover">
<thead>
<tr>
<td>#</td>
<td>slected</td>
<td>title</td>
<td>descriptiom</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php foreach($homeInfo as $row) { ?>
<tr>
<td><?php echo $row['id']; ?><input type="hidden" name="id" value="<?php echo $row['id']; ?>"></td>
<td><input type="radio" name="selected" value="home-info" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" class="btn btn-default" name="clear_info" value="delete all">
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input type="submit" class="btn btn-default" name="update_info" value="update">
</td>
</tr>
</tbody>
</table>
</form>
</div>
and update function.
function updateHomeInfo($selected_info, $selected_id) {
global $db;
$db->exec("UPDATE home SET selected = '$selected_info' where id = '$selected_id'");
}
All your hidden inputs have the same name; they'll all be submitted, and the value of $_POST['id'] will just be the last one, not the one next to the selected radio button. And all the radio buttons have the same value as well.
You should put the ID into the value of the radio button. You only need one copy of the hidden input, and it can contain the selected info.
<form action="/admin/" method="post">
<input type="hidden" name="selected" value="home-info">
<table class="table table-hover">
...
<td><?php echo $row['id']; ?>
<td><input type="radio" name="id" value="<?php echo $row['id']; ?>" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
...
Actually, it seems like you don't need the selected input at all, if selected is a boolean. It should just be:
function updateHomeInfo($selected_id) {
$db->exec("UPDATE home SET selected = (id = '$selected_id')");
}
This will set selected to 1 for the selected ID, and 0 for all the other IDs.

Editing data from mysql table using php

I have retrieved data from my table and displayed them using the code below.
<?php require_once('../Connections/bidco.php'); ?>
<body>
<table width="671" height="43" border="1" align="center">
<table width="781" height="190" align="center">
<tr>
<td height="61" colspan="4"><div align="center"><strong> Inventory </strong></div></td>
</tr>
<tr>
<td width="77" height="68"><strong>ID</strong></td>
<td width="152"><strong>Item name.</strong> </td>
<td width="253"><strong>unit price</strong> </td>
<td width="253"><strong>Update price</strong></td>
</tr>
<?php
$query=mysql_query("SELECT *FROM manuf ") or die (mysql_error());
while($row=mysql_fetch_array($query))
{
$id=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['itemname']; ?></td>
<td><?php echo $row['unitprice']; ?></td>
<td>change</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Now this PHP code is supposed to allow me to edit individual rows that have been displayed when i click on 'change' but it it not selecting the row. Any ideas how to solve this?
<?php require_once('../Connections/bidco.php'); ?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
$query=mysql_query("SELECT * FROM manuf where id='$id' ")or die(mysql_error());
$row=mysql_fetch_array($query);
?>
<form action="updateprice.php" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td> <label><strong>Item Name</strong></label></td>
<td> <input type='text' name='itemname' value=" <?php echo $row['itemname']; ?>" />
<input type="hidden" name="id" value="<?php echo $id; ?> " /> <br /></td>
</tr>
<tr>
<td><label><strong>Unit price </strong></label></td>
<td> <input type="text" name="unitprice" value="<?php echo $row['unitprice']; ?> " /><br /></td>
</tr>
<tr>
<td>
<input type="reset" name="Reset" value="CANCEL" />
<br></td>
<td>
<input type="submit" name="Submit2" value="Update" /> </td>
</tr>
</table>
</form>
</body>
</html>
Thank you in advance
Your forget to add you id to your link
<td>change</td>
I think you need
<a href="change.php?id=$row['id']">
Edit - use Arif_suhail_123's answer - I did not notice you have PHP mixed in with HTML
You need to pass rowid to href of Edit Link.
<td>change</td>
You are looking for id in change.php, but you are not sending that in the header. You must change change with
change
Also, I suggest you STOP using mysql_* commands, since the are Depreceted, and will no longer be supported. Use mysqli_* commands instead.

I want to submit this form into DB php

i want to insert this below given form data to database, but i dont know how to do this, it is about inserting product to multiple warehouse with different quantities, when checkbox against the warehouse name is checked than the quantity textbox appears.
and this is my PHP code
<form name="form1" method="post" action="product_insert.php" enctype="multipart/form-data">
<table>
<tr>
<td width="274" align="right" height="25"><strong>Product Name :</strong></td>
<td><input type="text" name="wproname" value="" /></td>
</tr>
<tr>
<td width="274" align="right" height="25"><strong>Select Warehouse :</strong></td>
<td width="500"><table style="border:none">
<tr >
<td> Select </td>
<td> Name </td>
<td> Quantity </td>
</tr>
<?php
$sql="select * from tbl_warehouse where w_flag='1'";
$result=ExecuteGetRows($sql);
$num_rows=count($result); ?>
<?php for($i=0;$i<$num_rows;$i++){ ?>
<tr>
<td><input type="checkbox" name="chk<?php echo $result[$i]['w_id'];?>" value="<?php echo $result[$i]['w_id'];?>" id="chk<?php echo $result[$i]['w_id'];?>" onChange="display<?php echo $result[$i]['w_id'];?>();" />
</td>
<td><?php echo $result[$i]['w_name'];?></td>
<td><input type="text" name="qty<?php echo $result[$i]['w_id'];?>" id="qty<?php echo $result[$i]['w_id'];?>" style="display:none" />
</td>
</tr>
<script>
function display<?php echo $result[$i]['w_id'];?>()
{
if(document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="block";
}
if(!document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="none";
}
}
</script>
<?php } ?>
</table></td>
</tr>
<tr>
<td align="right"></td>
<td width="296"><input type="submit" name="Submit" value="Add New" align="middle" class="button login_btn"/></td>
</tr>
</table>
</form>
And I am using this php code for inserting data to the database
$sqls=mysql_query("select * from tbl_warehouse");
while($result=mysql_fetch_array($sqls)) {
$w = $result['w_id'];
echo $_POST['chk'.$w];
foreach($_POST['chk'.$w] as $key=>$val) {
echo $_POST['chk'.$w];
$sql = "INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
mysql_query($sql);
}
}
You don't need the leading comma in your values list in your insert statement. So change this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
To this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('".$wid."','".$wproname."','".$qty."')";

Categories