Update Mysql rows in foreach loop based on selected itemid - php

I am trying to update some rows in mysql based on selected checkbox,but my query is updating all the rows in the table. Please check below code:
if (!empty($_POST['checkbox']))
{
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['checkbox'] as $selected) {
$sql1="UPDATE Items SET Status='1' WHERE Item_Id='$selected'";
$result1=mysql_query($sql1);
if (!$result1)
{
echo "Error While updating";
}
else
{
header("Refresh: 2;url=Main_Menu.php");
mysql_close($conn);
echo "<p>Todays Menu Updated <a href='Main_Menu.php>Click here</a> if you are not redirected automatically in 2 seconds<br /></p>";
}
}

Here if (!empty($_POST['checkbox'])) you should use the name of your checkbox.
For instance, if your html is like below:
<input type="checkbox" name="checkbox_name" />
You should write the if statement as below:
if (!empty($_POST['checkbox_name']))
The same holds for the foreach statement and the use of this in your sql statement.

Related

pass on a row ID onto another page in php

I am trying to pass on a row ID by user click on the specified row, onto another page. I have a table with ID and info column.
code below displays the wanted row ID and info
if ($info = $stmnt2->fetch()) {
echo '<p>Your Info:</p>';
do {
echo "$info[id] . $info[review] . <a href=edit.php?edit=$info[id]>edit</a></br> </br>" ; //The info id is contained in the $info['id']
} while ($info = $stmnt2->fetch());
} else {
echo "<p>No Info</p>";
}
I want the user to be able to click on any of the rows and the selected row to pass on its ID onto another page. How do I do this?
This is the code on the other page and I want the ID on which the user clicked to replace "$info[id]" in the sql query. This replaces the whole column and not the specified row.
if(isset($_POST['id'])){
$update=$_POST['id'];
$db->exec("UPDATE infos SET info = '$update' WHERE reviewid = '$info[id]'");
}
In the edit page I have an input which the user can write to replace the selcted row (from the ID that gets passed on)
<form action="edit.php" method="POST">
<input type="text" name="id" value="">
<input type="submit" value=" Update "/>
</form>
So I want the ID that was passed from the first page to be used to replace the info row with the user input from the edit page
Pass the ID to the URL to the next page, navigate to the next page, then use $id =$_GET['id'];
Your edit=$info['id'] part is right but you're using $_POST and $_POST['id'], on the next page. The GET global is needed and it's named edit, not id
if ($info = $stmnt2->fetch()) {
echo '<p>Your Info:</p>';
do {
echo "$info[id] . $info[review] . <a href=edit.php?edit=$info[id]>edit</a></br> </br>" ; //The info id is contained in the $info['id']
} while ($info = $stmnt2->fetch());
} else {
echo "<p>No Info</p>";
}
edit.php:
if(isset($_GET['edit'])){
$update = $_GET['edit'];
$db->exec("UPDATE infos SET info = '$update' WHERE reviewid = '$info[id]'");
}
Also for future expansion and learning, read into how to do prepared statements with bound parameters if you are going to be using queries with variables built in. You're prone to sql injection currently and it's good practice to learn the newer and safer methods.
you can get the parameter value by using $_REQUEST
$update = $_REQUEST['edit'] in edit.php file
when you use $_REQUEST method you can catch both $_GET and $_POST values.

Retrieve multiple checkbox and display other related information from database in a table

I got this list of checkboxes that print out from database. I am able to retrieve data from database if user makes multiple checkbox and display it in table, but i cannot retrieved other information from database and display it in the same table.
This is my code:
<table border='1'>
<tr>
<th>TITLE</th>
<th>PERCENTAGE RESULT</th>
</tr>
<?php
if(isset ($_POST["submit1"]))
{
$selectedcheckbox = $_POST["selectedcheck"];
$query = "SELECT * FROM compareresult where subject=$selectedcheckbox";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query,MYSQL_ASSOC)){
$result=$data['result'];
}
foreach($selectedcheckbox as $title)
{
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
}
echo "</tr>";
}
?>
I want to display result after user select multiple checkbox, so I wrote:
echo $result in table
so that the result can be displayed in table beside the selected title, but I am getting an error:
Array to string conversion in C:\xampp\htdocs\sam\c.php on line 31 Error 3 :Unknown column 'Array' in 'where clause'
I am not at the moment in the environment to test your problem, and I do this out of my head, but try something like the following.
if(isset($_POST['submit1'])){
$checkbox = isset($_POST['selectedcheck']) ? $_POST['selectedcheck'] : array();
foreach($checkbox as $title){
$query = "SELECT * FROM compareresult where subject='".$title."'";
$result=mysql_query($query); // <-- to avoid SQL injections, please change your method from mysql_query to mysqli_query (use the mysqli functions instead of mysql)
while($data = mysql_fetch_array($result)){
$result=$data['result'];
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
echo "</tr>
}
}
}
It checks first if your checkboxes are checked and if they are they put it in an array.
Then it runs the foreach statement, where it sets the checkbox value as title. It runs there if the query if it the title is in the database, if so, spit it out through the while loop. Rinse and repeat until done.
edit
I see you put your open tablerow statement in the foreach but close it outside. So either you want it all printed on one line or is it an error? If its on one line, make sure you open the table row BEFORE the while loop and end it AFTER, else it's like how i spit it out.

How can i update specific records using a looped drop down list where its records are from mysql

So here's the thing, I want to make a simple page or module that will update the quantity of a certain product in my inventory but the items are listed already on a drop down list(select tag) where its elements are from my records in the database so the user can only update products that is IN the database.
Here's my code:
$result=mysql_query("SELECT * from inventory order by item_name");
$ctr=0;
while($inventory=mysql_fetch_array($result))
{
$print=$inventory['item_name'];
$cquantity=$inventory['quantity'];
$selected=array();
$selected[$ctr]=$print;
?>
<option value="<?php echo "$selected[$ctr]";?>"> <?php echo $print; ?></option>
<?php $ctr++;}
if (isset($_POST['submit']))
{
$add=$_POST['add'];
$subtract=$_POST['subtract'];
if ($add>0)
{
$quantity=$cquantity+$add;
$sql="Update inventory set quantity='$quantity' where item_name='$selected[$ctr]'";
mysql_query($sql) or die(mysql_error());
?>
<script>
alert("Inventory was successfully updated.");
</script>
<?php
}
else{
echo "<font color='red'>Please fill up the form correctly.</font>";}
if ($subtract>0)
{
$quantity=$cquantity-$subtract;
$sql="Update inventory set quantity='$quantity' where item_name='$selected[$ctr]'";
mysql_query($sql);
?>
<script>
alert("Inventory was successfully updated.");
</script>
<?php
}
else {echo "<font color='red'>Please fill up the form correctly.</font>";}
}
?>
I know that my problem is in the SQL command:
$sql="Update inventory set quantity='$quantity' where item_name='$selected[$ctr]'";
because ctr will be incremented at the last part of the loop.
How will I make a right sql code that will make the where clause recognize the value or name of the selected data in the drop down list?
I have found the solution. I was thinking too much deep, I didn't notice that all i have to do is to assign a variable and set its value to the posted value of my <select> and that's all.

Grabbing a value from a SELECT box in PHP

I've got a drop down select box that grabs each relevant value from an SQL database in a loop.
I'm creating a form so that when the "Submit" button is pressed it redirects to a PHP file that carries out the INSERT SQL statement. However because the select options are coming from a loop I'm unsure of how to grab the right value when its selected as it just grabs the last value gained from the loop.
I'm pretty sure that the way I have done it is the wrong way to go
<?php
echo"<select name='ModuleTitle' id='ModuleTitle' style='width:100%;'>";
echo"<option>Select...</option>";
//3. Perform database query
$result = mysql_query("SELECT * FROM Module
ORDER BY `ModTitle` ASC;", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row5 = mysql_fetch_array($result)) {
$module = $row5[2];
echo "<option name='{$module}'>".$row5[2]."</option><br />";
}
echo"</select>";
echo "<a href='submitREQ.php?id={$module}'><img src='images/submit.jpg' height='27'></a>";
?>
Instead of using <a href you should use <input type="image" value="submit" src="images/submit.jpg" />
To grab the value after the form is submitted you should use: $ModuleTitle = $_POST['ModuleTitle']; or $_GET if the method is get.

Delete selected data in a checkbox

I'm making a website on which I have a set of data with checkbox. I need to delete multiple rows from database when I select multiple checkboxes. I don't know how to pass the id of the selected checkboxes to next page. Please help me.
Here is my code:
$select_qry="select * from data";
$result=mysql_query($select_qry);
$rows=mysql_num_rows($result);
if($rows>0)
{
?>
<form action="delete_submit.php" method="post">
<?php
for($i=0;$i<$rows;$i++)
{
$arr=mysql_fetch_assoc($result);
$id=$arr['id'];
$name=$arr['name'];
//echo $id;echo "<br>";echo $name;
?>
<input name="checkbox[]" type="checkbox" value="<?php echo $id;?>">
<?php
}
}
?>
How can I pass the selected checkboxes' id to the next page? Please help me.
Thanks
when form will be submitted then $_POST['checkbox'] will be set and contain the array of selected ids.
In your delete script
if(isset($_POST['checkbox']) && count($_POST['checkbox']) > 0){
$deleteIds = $_POST['checkbox']; // it will be an array
$sql = "DEFRE FROM tablename WHERE id in (".implode("," , $deleteIds).") ";
// run the query
}

Categories