PHP update multiple rows on submit - php

Hi I have echoed out multiple rows from a database. Each row has a select box which echoes out what is currently in that field related to that row.
With the select box I can select a different option. On submit nothing is happening, no errors. What it should do is update those fields. I have never managed to update multiple rows before so this is new to me.
The first query below is the update I'm trying to achieve to update all the fields with order_ref
The second query just echoes out all the data required to view.
<?php
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>
<form>
<table class="tbl-qa">
<thead>
<tr>
<th class="table-header"><p>Action</p></th>
<th class="table-header"><p>Campaign</p></th>
<th class="table-header"><p>Title</p></th>
<th class="table-header" width="100px"><p>Order Reference</p></th>
<th class="table-header"><p>Last updated</p></th>
</tr>
</thead>
<tbody>
<?php
require_once("../db/db_connection.php");
if (isset($_POST['submit'])) {
$sql = $db->prepare("UPDATE articles SET order_ref=? WHERE id=?
");
$order_ref = $_POST['order_ref'];
$sql->bind_param("ii", $order_ref, $_GET["id"]);
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
}
$sql = $db->prepare("SELECT * FROM articles WHERE campname=? ORDER BY order_ref ASC
");
$sql->bind_param("s",$_GET["campname"]);
$sql->execute();
$result = $sql->get_result();
?>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td class="table-row" width="100px"><div class="edit">Edit |</div><div class="bin"><a href="delete.php?id=<?php echo $row["id"]; ?>" class="link"><img content="delete" id="delete" title="Delete" onclick="return confirm('Are you sure you want to delete?')" src="/_admin/images/delete.png"/></div>
</a>
</td>
<td>
<?php echo $row["campname"]; ?>
</td>
<td class="table-row"><?php echo $row["art_title"]; ?></td>
<td class="contentedit">
<select name="order_ref">
<option value="<?php echo $row["order_ref"]?>"selected><?php echo $row["order_ref"]?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td class="table-row"><?php echo strftime("%b %d, %Y", strtotime($row["last_updated"])); ?></a></td>
<!-- action -->
</tr>
<?php
}
}
else {
echo "No results";
}
?>
<tr class="table-row">
<td colspan="5"><input name="submit" type="submit" value="Update" class="submit"></td>
</tr>
</tbody>
</table>
</form>
What do I need to do in order to update order_ref for every row that's echoed out?

Related

While loop finding the last iteration only

*How to solve this..??
My while loop is finding the last iteration only, If I select last attendance A then all the student's values will be 0, if I select P then all the values will be 1. So, how can I solve this?*
Index.php file
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
?>
<table border="1">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Attendance</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</td>
</tr>
<?php
}
?>
</table>
<br>
<button type="submit">Submit</button>
</form>
attendance.php
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if(isset($_POST['atten'])){
$atten = $_POST['atten'];
echo $atten.'<br>';
}
}
Here is output
Image for Index.php file
Image for attendance.php file
You're not submitting the student ID in the form. You can use a hidden input for this.
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<input name="stu_id" type="hidden" value="<?php echo $row['stu_id'];?>">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
Then in attendance.php you can update that student.
<?php
include'connection.php';
$stmt = $conn->prepare("UPDATE `student_info` set atten = ? WHERE stu_id = ?");
$stmt->bind_param("ii", $_POST['atten'], $_POST['stu_id']);
$stmt->execute();
echo "Attendance for student {$_POST['stu_id']} updated";

how to refresh tabs data when update table?

index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You
Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home

Display <select> in each table row by using the same database table data

I'm going to construct a table to let my user enter their SPM result then my system will filter and show them which course is eligible to apply.
I've tried to make 10 rows in a table and each row consisted of 2 <select>, one is SPM subject and another one is the grade they obtained. The options of Subject and Grade were fetch from my database.
Here is my coding for my table:
<?php
$result = mysql_query("SELECT * FROM spm_subject");
$result2 = mysql_query("SELECT * FROM spm_grade");
?>
<table class="p1" bgcolor="#FFFFCC" bordercolor="#000000" align="center" width="771" border="2">
<tr>
<td><div align="center"><strong>No.</strong></div></td>
<td><div align="center"><strong>Subject Name</strong></div></td>
<td><div align="center"><strong>Grade</strong></div></td>
</tr>
<form action="checkresult2.php">
<?php
for($i=1; $i<=10; $i++)
{?>
<tr>
<td width="44"><div align="center"><?php echo $i; ?></div></td>
<td width="601">
<select>
<option value="">--- Please choose a subject ---</option>
<?php
while($s = mysql_fetch_assoc($result))
{?>
<option name="subj"><?php echo $s["name"]; ?></option>
<?php } ?>
</select>
</td>
<td width="104"><div align="center">
<select>
<option value=""> </option>
<?php
while($g = mysql_fetch_assoc($result2))
{?>
<option name="grad"><?php echo $g["grade"]; ?></option>
<?php } ?>
</select>
</div>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">
<div align="center">
<input type="submit" value="Submit">
</div>
</td>
</tr>
</form>
</table>
The table was constructed but only the FIRST ROW able to display data from the both database tables inside the both <select>.
Any solution to solve this problem?
1) Remove <form> inside <table>. Because, A form is not allowed to be a child element of a table, tbody or tr. You can have an entire table inside a form. You can have a form inside a table cell. You cannot have part of a table inside a form. For more info, click this
2) <option> don't have name attribute. Check here
3) Give name to <select></select> . Check here
4) Since, multiple values are being submitted to checkresult2.php page, you have to define name as name="subj[]" and name="grad[]" as array type.
5) An Alternative way for showing data in rest 9 rows is : before while loop give that query there itself, like below.
Updated Code.
<form action="checkresult2.php">
<table class="p1" bgcolor="#FFFFCC" bordercolor="#000000" align="center" width="771" border="2">
<tr>
<td><div align="center"><strong>No.</strong></div></td>
<td><div align="center"><strong>Subject Name</strong></div></td>
<td><div align="center"><strong>Grade</strong></div></td>
</tr>
<?php
for($i=1; $i<=10; $i++)
{?>
<tr>
<td width="44"><div align="center"><?php echo $i; ?></div></td>
<td width="601">
<select name="subj[]">
<option value="">--- Please choose a subject ---</option>
<?php
$result = mysql_query("SELECT * FROM spm_subject");
while($s = mysql_fetch_assoc($result))
{?>
<option value="<?php echo $s['name']; ?>"><?php echo $s["name"]; ?></option>
<?php } ?>
</select>
</td>
<td width="104"><div align="center">
<select name="grad[]">
<option value=""> </option>
<?php
$result2 = mysql_query("SELECT * FROM spm_grade");
while($g = mysql_fetch_assoc($result2))
{?>
<option value="<?php echo $g['grade']; ?>"><?php echo $g["grade"]; ?></option>
<?php } ?>
</select>
</div>
</td>
</tr>
<?php }?>
<tr>
<td colspan="3">
<div align="center">
<input type="submit" value="Submit">
</div>
</td>
</tr>
</table>
</form>
[NOTE: mysql_ is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Use MySQLi or PDO_MySQL extension should be used.]
You have to preprocess the query populating two arrays:
$names = $grades = array();
for( $i=1; $i<=10; $i++ )
{
$names[] = mysql_fetch_assoc( $result );
$grades[] = mysql_fetch_assoc( $result2 );
}
Then, in our code:
<?php foreach( $names as $key => $name ) {?>
<option name="subj"><?php echo $name['name']; ?></option>
<?php } ?>
and
<?php foreach( $grades as $key => $grade ) {?>
<option name="grad"><?php echo $grade['grade']; ?></option>
<?php } ?>
mysql_fetch_assoc fetch the current row from results and then ‘go’ to next row, so when the first for loop is terminate, the cursor has reached the end of results and - in next for loops, there is nothing to fetch.
Please note:
mysql_ syntax was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

SQL statement to populate more than one listbox at a time

I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>

I cannot update multiple columns, only one will update

I figured out how to update a table with multiple columns using checkboxes, except when more than one checkbox is selected the update will only happen for one of the columns not both. Could someone please help? Thank you!
Here is the working code for the table:
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="GET" action="assigncases1.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<select name="assigned_to">
<option value=""></option>
<option value="Kristin Dodd"> Kristin Dodd </option>
<option value="Matt Ursetto"> Matt Ursetto </option>
<option value="Derek Bird"> Derek Bird </option>
<option value="John Castle"> John Castle </option>
<option value="Martin Delgado"> Martin Delgado </option>
</select>
<br>
<input type='submit' value = 'Assign'>
</tr>
<tr>
<td> </td>
<td colspan="4" align="center"><strong>Update multiple rows in mysql with checkbox<br>
</strong></td>
</tr><tr>
<td align="center" bgcolor="#FFFFFF"></td>
<td align="left"><strong>Name</strong></td>
<td align="left"><strong>SSO</strong></td>
<td align="left"><strong>case_status</strong></td>
<td align="left"><strong>Assigned To:</strong></td>
</tr>
<?php
$result=mysql_query($sql);
$count=mysql_num_rows($result);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox" type="checkbox" id="checkbox[]" value="<?
echo $rows['id']; ?>"></td>
<td><? echo $rows['full_name']; ?></td>
<td><? echo $rows['sso']; ?></td>
<td><? echo $rows['case_status']; ?></td>
<td><? echo $rows['assigned_to']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
And this is what I have for the php page that take the info.
// Retrieve data from database
$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];
// update data in mysql database
$sql="UPDATE rmstable2 SET assigned_to='$assigned_to' WHERE id='$checkbox'";
$result = mysql_query($sql);
$count = mysql_numrows($result);
{
mysql_query("UPDATE `rmstable2` SET `assigned_to` = '$assigned_to'
WHERE `id` = '$checkbox'")
or die(mysql_error());
}
//If they did not enter a search term we give them an error
if ($assigned_to == "")
{
echo "<h3>You forgot to enter a required field. Please try again.</h3><br>";
}
// if successfully updated.
else if($assigned_to !== "")
{
echo "<b>Update Successful</b><br>";
echo "<br>This case was assigned to:<b> $assigned_to</b><br>";
echo "<br>To see the change go back and click on <b>Refresh Page</b> if you assigned it
to someone other than you, the case will disappear and show up in their list of
assigned cases.";
}
else {
echo "ERROR";
}
?>
In your HTML, you're defining your checkboxes with the name checkbox - so only one value will be sent to PHP. To get an array of values, update the checkboxes to be created with name="checkbox[]" and when the form is submitted, you can use the selected values like an array.
In that case, you'll be able to loop through each item with:
$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];
foreach ($checkbox as $id) {
// do a very basic validation to see if the ID is numeric
if (!is_numeric($id)) continue;
$sql = 'UPDATE rmstable2 SET assigned_to="' . mysql_real_escape_string($assigned_to) . '" WHERE id=' . (int)$id;
mysql_query($sql);
}
Also, as always worth noting, the mysql_* functions are being deprecated and you should consider to start using either mysqli_* or PDO methods.

Categories