how to insert multiple data from looped table? - php

I want to insert multiple records from a looped table. Each row have hidden inputs.
I will POST the total number of rows for the table which will be used in the loop condition.
Here is my view:
<input type="hidden" name="rowcount">//total row of the table
<table class="table" id="ctable" style="border:0;">
<?php
foreach ($row12 as $row12) {
echo"<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta' value='$row13->id_peserta'>
<input type='hidden' name='id_un' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai' value='$nilai' />
</td>
</tr>";
}
?>
</table>
Here is my model:
function simpan_nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta')."',
id_un = '".$this->input->post('id_un')."',
nilai = '".$this->input->post('nilai')."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}
I've only been able to insert the last row of data. For example, if the data is like (001, 1, 100), (001, 2, 90), (001, 3, 95), only the last row of record is inserted into the database i.e. (001, 3, 95).
Please help me. Thanks!

closed.
i already fix my problem.
thanks to all who helped me
View:
<input type='text' name='rowcount'>
<table class="table" id="ctable" style="border:0;">
<?php
foreach ($row12 as $row12) {
echo"<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta' value='$row13->id_peserta'>
<input type='hidden' name='id_un$row12->id_un' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai$row12->id_un' value='$nilai' />
</td>
</tr>";
}
?>
</table>
Model:
function simpan_nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$nilai = 'nilai' . $x;
$id_un = 'id_un' . $x;
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta')."',
id_un = '".$this->input->post($id_un)."',
nilai = '".$this->input->post($nilai)."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}

The reason is because you have multiple hidden inputs of the same name id_peserta. In your PHP codes, the only input that is received is the last input of the same name.
You can try to append a count after each of your input name to make it unique.
<input type="hidden" name="rowcount"> <!-- total row of the table -->
<table class="table" id="ctable" style="border:0;">
<?php
$count = 1; // You can start from zero. Either works.
foreach ($row12 as $row12) {
echo "<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta{$count}' value='$row13->id_peserta'>
<input type='hidden' name='id_un{$count}' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai{$count}' value='$nilai' />
</td>
</tr>";
$count++;
}
?>
</table>
And for your PHP codes.
function simpan nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta' + $x)."',
id_un = '".$this->input->post('id_un' + $x)."',
nilai = '".$this->input->post('nilai' + $x)."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}

Related

PHP post of td content inside a for

I just need to send a line of a table in my PHP page after click on button.
This is my code :
$login = $_POST['login'];
$requete = Connexion::query("SELECT id
FROM session
WHERE login='$login'");
$requete = $requete[0][0];
$application = Connexion::query("SELECT titre,cleAk,cleAs,cleCk
FROM cles
WHERE sessionId='$requete'");
echo "<form class='login-form' method='POST'><table class='table'><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><tr>";
for($i=0;$i<sizeof($application);$i++)
{
for($j=0;$j<4;$j++)
{
echo "<td><input type='hidden' name='titre' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
}
echo "<td><button type='submit' name='api'>Submit</button></td></tr></form>";
}
And this var_dump($_POST['titre']); return the last cell of the last line.
Can you help me ?
As was briefly mentioned in the comments of a previous answer, if you only want to submit the contents of one line, then you will need a separate <form> on every line (table row). You were trying this with a closing </form> inside the loop, but you opened it outside the loop resulting in invalid HTML with lots of closing </form> tags but only one opening <form> tag.
Another problem with your form inputs is that they were all named the same after the first column name='titre' but you had specified each column separately in the table header. To fix this, others offered an imprecise option by creating an array name='titre[]', but I think it's better to dynamically name the inputs to match the column key.
I don't know what database API you're using, but it appears to only produce a numeric multidimensional array without associative keys. There's probably a class method to get the column names, or you can manually assign it based on your SQL query as I have done with the array $cols
<?php
// debugging to show you everything posted
print_r($_POST);
// generate some random data for example
$application[0][0] = "first titre";
$application[0][1] = "first cleAk";
$application[0][2] = "first cleAs";
$application[0][3] = "first cleCk";
$application[1][0] = "second titre";
$application[1][1] = "second cleAk";
$application[1][2] = "second cleAs";
$application[1][3] = "second cleCk";
$application[2][0] = "third titre";
$application[2][1] = "third cleAk";
$application[2][2] = "third cleAs";
$application[2][3] = "third cleCk";
// get the columns for input names
$cols = array('titre','cleAk','cleAs','cleCk');
// semantically complete HTML table
echo "
<table class='table'>
<thead>
<tr><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><th>Submit</th></tr>
</thead>
<tbody>
";
for($i=0;$i<sizeof($application);$i++)
{
echo "
<tr>
<form class='login-form' method='POST'>";
for($j=0;$j<sizeof($cols);$j++)
{
echo "
<td><input type='hidden' name='".$cols[$j]."' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
}
echo "
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>\n";
}
echo "
</tbody>
</table>
";
Results in this rendered output when I click on the first row's submit button. Note that you can't post data in the snippet below, this is purely for static HTML demonstration purposes.
Array
(
[titre] => first titre
[cleAk] => first cleAk
[cleAs] => first cleAs
[cleCk] => first cleCk
[api] =>
)
<table class='table'>
<thead>
<tr><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><th>Submit</th></tr>
</thead>
<tbody>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='first titre'>first titre</td>
<td><input type='hidden' name='cleAk' value='first cleAk'>first cleAk</td>
<td><input type='hidden' name='cleAs' value='first cleAs'>first cleAs</td>
<td><input type='hidden' name='cleCk' value='first cleCk'>first cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='second titre'>second titre</td>
<td><input type='hidden' name='cleAk' value='second cleAk'>second cleAk</td>
<td><input type='hidden' name='cleAs' value='second cleAs'>second cleAs</td>
<td><input type='hidden' name='cleCk' value='second cleCk'>second cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='third titre'>third titre</td>
<td><input type='hidden' name='cleAk' value='third cleAk'>third cleAk</td>
<td><input type='hidden' name='cleAs' value='third cleAs'>third cleAs</td>
<td><input type='hidden' name='cleCk' value='third cleCk'>third cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
</tbody>
</table>
You should pass an array to handle the multiple elements with the inputs. In Your code it should be look like this:
for($j=0;$j<4;$j++)
{
echo "<td><input type='hidden' name='titre[$j]' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
echo "<td><button type='submit' name='api'>Submit</button></td></tr></form>";
}
The modification is name='titre[]'. Hope this will help you

Editing multiple selection based on user choice

I've been trying to solve this for awhile, and I'd appreciate it if someone can help me. Here is my problem.
Here is my code; it's simply view the content of a Job Table in the Database and perform a edition as needed, based on the selection. The checkbox is next to each job, and there is an update button at the end of the page to submit..
I'm getting an error updating it. Please help me.
<?php
session_start();
if( isset($_SESSION['username']) ){
include('../CIEcon.php');
echo "<form action= 'adminCleaning.php' method = 'post'>" ;
// when the user click update..
if(isset($_POST['update'])){
if( empty($_POST['Id']) || $_POST['Id'] == 0 ){
echo"<h4> please choose something to update </h4>";
echo"test(1): pass <br> ";
}else{
// comes here even though u dind't chhose, cause
// it set IDs next to each feild..
echo"!!....HERE....!! ";
}
}// end of update $_POAT[update]
$sql = "SELECT * FROM Cleaning ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
/// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...
echo "
<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>
<th class='tt' >Check </th>
<th class='tt'> Job's Name</th>
<th class='tt' >Description</th>
<th class='tt' > No Students needed</th>
<th class='tt' >Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<br>";
echo "<tr>";
echo "<td> <input type='checkbox' name='Id[]' value='".$row['Id']."' /> </td>"; // array[] cause to edit more than one record...
echo "<td>".'<input type="text" name="jobname['.$row['Id'].']" value='.$row['JobName'].' >'."</td>";
echo "<td>".'<input type="text" name="description['.$row['Id'].']" value='.$row['Description'].'> '."</td>";
echo "<td>".'<input type="text" name="nostudent['.$row['Id'].']" value='.$row['NoStudent'].'>'."</td>";
echo "<td>".'<input type="text" name="duedate['.$row['Id'].']" value='.$row['DueDate'].'>'."</td>";
echo "<input type=hidden name='Id[]' value='".$row['Id']."' >";
echo "</tr>";
echo "jobname['.$row[Id].']" ; // testing.
echo "description['.$row[Id].']" ; // testing.
echo "nostudent['.$row[Id].']" ; // testing.
}
echo "</table>";
/// END THE SEARCH HERE...........
echo " <br>
<div align='center'>
<input type='reset' value='clear' />
<input type='submit' name='update' value='update' />
</div> ";
mysqli_close($dbCIE);
echo "</form>";
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> ..Cleanding.... </title></head>
<style type="text/css">
body{
margin-top: 70px; /*space above the table....*/
background-color: #23438e;
}
table{
background-color: white;
}
.tt{
background: #f26822;
color: white ;
}
</style>
<body>
<!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a> -->
</body>
</html>
First of all, your form and table are outside of <body> tag. You have to display everything like this: <body> **display here** </body>
Second, remove this line echo "<input type=hidden name='Id[]' value='".$row['Id']."' >"; from your code, it's not required.
And now comes to your question, $_POST['Id'] is an array of job ids, so use count() function to check if the array is empty or not and use foreach loop to update each individual row. So you should process your form like this:
// when the user click update..
if(isset($_POST['update'])){
if(count($_POST['Id'])){
// $_POST['Id'] is an array of job id
foreach($_POST['Id'] as $v){
$sqlUpdate = "UPDATE Cleaning SET JobName='" . $_POST['jobname'][$v] . "', Description='" . $_POST['description'][$v] . "', NoStudent='" . $_POST['nostudent'][$v] ."', DueDate='" . $_POST['duedate'][$v] . "' WHERE Id = '" . $v . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
// If you want you can use mysqli_affected_rows() function to
// check how many were affected by the UPDATE query
}
}else{
echo"<h4>please choose something to update</h4>";
}
}
And your entire code should be like this:
<?php
session_start();
?>
<html>
<head>
<title> ..Cleanding.... </title>
<style type="text/css">
body{
margin-top: 70px; /*space above the table....*/
background-color: #23438e;
}
table{
background-color: white;
}
.tt{
background: #f26822;
color: white ;
}
</style>
</head>
<body>
<?php
if( isset($_SESSION['username']) ){
include('../CIEcon.php');
// when the user click update..
if(isset($_POST['update'])){
if(count($_POST['Id'])){
// $_POST['Id'] is an array of job id
foreach($_POST['Id'] as $v){
$sqlUpdate = "UPDATE Cleaning SET JobName='" . $_POST['jobname'][$v] . "', Description='" . $_POST['description'][$v] . "', NoStudent='" . $_POST['nostudent'][$v] ."', DueDate='" . $_POST['duedate'][$v] . "' WHERE Id = '" . $v . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
// If you want you can mysqli_affected_rows() function to
// check how many were affected by the UPDATE query
}
}else{
echo"<h4>please choose something to update</h4>";
}
}
$sql = "SELECT * FROM Cleaning ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
?>
<form action= 'adminCleaning.php' method = 'post'>
<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>
<th class='tt' >Check </th>
<th class='tt'> Job's Name</th>
<th class='tt' >Description</th>
<th class='tt' > No Students needed</th>
<th class='tt' >Due Date</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td> <input type="checkbox" name="Id[]" value="<?php echo $row['Id']; ?>" /> </td>
<td><input type="text" name="jobname[<?php echo $row['Id']; ?>]" value="<?php echo $row['JobName']; ?>" /></td>
<td><input type="text" name="description[<?php echo $row['Id']; ?>]" value="<?php echo $row['Description']; ?>" /></td>
<td><input type="text" name="nostudent[<?php echo $row['Id']; ?>]" value="<?php echo $row['NoStudent']; ?>" /></td>
<td><input type="text" name="duedate[<?php echo $row['Id']; ?>]" value="<?php echo $row['DueDate']; ?>" /></td>
</tr>
<?php
}
?>
</table>
<input type="reset" value="clear" />
<input type="submit" name="update" value="update" />
</form>
<br />
<div align='center'>
</div>
<?php
mysqli_close($dbCIE);
}
else{
echo "must logout to see this page..!!";
}
?>
</body>
</html>

Can you update in a div tag

I have a little question about something. I have some forms, where I send the input to a MySQL database. I get the return from the database, out in some div tags. Here I have 2 buttons. Button number 1 can delete the row, and button number 2 should have a function, where I can update a specific row, if I want to change the content of the row. But can I update a div tag? I can see on the net, that a lot of people use tables to do that.
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/arrangeTables.css">
</head>
<body>
<?php
include 'connection.php';
if(isset($_POST['addto'])){
// Insert to database
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];
$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', '$totime')";
$result = mysql_query($sql, $dbhandle) or die(mysql_error($dbhandle));
// Update of the row
if(isset($_POST['update'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$UpdateQuery = "UPDATE addWorkTime
SET date='$_POST[date]',
day='$_POST[day]',
fromtime='$_POST[fromtime]',
totime='$_POST[totime]'
WHERE p_id='$_POST[hidden]'";
$update = mysql_query($UpdateQuery, $dbhandle) or die(mysql_error($dbhandle));
if($update) {
echo "Succes";
} else {
echo "Der er en fejl";
}
}; // brace for if(isset($_POST['update']))
if($result){
echo "Insert successful.";
}
}; // brace for if(isset($_POST['addto']))
if(isset($_POST['delete'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$DeleteQuery = "DELETE FROM addWorkTime WHERE p_id=$hidden";
$delete = mysql_query($DeleteQuery, $dbhandle) or die(mysql_error($dbhandle));
if($delete){
echo "Delete successful";
}
}
//Return records from database
$result = mysql_query("SELECT p_id, date, day, fromtime, totime FROM addWorkTime");
?>
<form method="post">
<h3>Add your worktime to database</h3><br>
Date:
<input type="date" name="date"><br><br>
Day
<select name="day">
<option value="Mandag">Mandag</option>
<option value="Tirsdag">Tirsdag</option>
<option value="Onsdag">Onsdag</option>
<option value="Torsdag">Torsdag</option>
<option value="Fredag">Fredag</option>
<option value="Lørdag">Lørdag</option>
<option value="Søndag">Søndag</option>
</select>
From time:
<input type="time" name="fromtime">
To time:
<input type="time" name="totime">
<input type="submit" name="addto" value="submit"><br><br>
<!-- Return from the database -->
<h3>Return from database:</h3><br>
<!-- headers -->
<table>
<tr>
<th class="column0">Primary Key</th>
<th class="column1">Date</th>
<th class="column2">Day</th>
<th class="column3">From</th>
<th class="column4">To</th>
</tr>
</table>
</form>
<!--loop through through the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<form method="post">
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
<?php endwhile; ?>
</body>
</html>
If you want to update your cell table like your recording, you can do a tricky way like this code snippet:
function change1() {
var inp1 = document.getElementById('myTable').rows[1].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[1].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
function change2() {
var inp1 = document.getElementById('myTable').rows[2].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[2].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
table, td {
border: 1px solid black;
}
<table id="myTable">
<tr>
<td>column 1</td>
<td>column 2</td>
<td>action</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
<td>
<button onclick="change1()">Update</button>
</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
<td>
<button onclick="change2()">Update</button>
</td>
</tr>
</table>
<button onclick="alert('whatever function to save the edit')">Save</button>
you can improve that to give <form> tag to your <table> tag and save that using another Save button. Or you can use if conditional to your update button for change to textfield and post it to your database.
<form method="post" action='function_to_update_or_delete.php'>
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><button onclick="<?php echo change($some_information_to_your_cell)?>">Update</button></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
And, please try to make a function dynamically each row, don't hardcode it like function change1(), function change2(), function change3(), etc
Hope this will help you out. :)

Fetching multiple looped textboxes' values

So I have this program where the user sets up a database table. First, I ask them how many fields they want.
first.php
<html>
<form name="formCreateFields" method="post" align="center" action="second.php">
<p>Number of fields: <input type ="text" name="fieldsNum"/>
<input type="submit" name="submitFieldsNum" value=" Submit "/></p><br>
</form>
</html>
Then I loop the fields, depending on their input above.
second.php
<?php
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
for ($z=1; $z<=$_POST['fieldsNum']; $z++) {
echo "<table align='center'>
<tr>
<th rowspan=2> <big> $z </big> </th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>
<tr>
<td><input type='text' name='fieldName$z'></td>
<td><input type='text' name='fieldType$z'></td>
<td><input type='text' name='fieldLength$z'></td>
</tr>
</table><br><br>";
}
<input type='submit' name='submitFieldSetup'>
</form>";
?>
I'm having problems after this. I've been trying to test fetching them by putting them in an array and using foreach to view them but can't seem to get anywhere. I thought it was okay to use something like $_POST['fieldName$z'] but I guess I was wrong.
I just need to find out how I could fetch all the inputs in the second file. Any ideas? Thanks in advance! :)
If fieldsNum is a number, the for should be fine in this case, just properly concatenate the values:
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
echo "<table align='center'>";
echo '
<tr>
<th rowspan=2></th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>';
for ($z = 1; $z <= $_POST['fieldsNum']; $z++) {
echo "
<tr>
<td><input type='text' name='inputs[$z][name]'></td>
<td><input type='text' name='inputs[$z][type]'></td>
<td><input type='text' name='inputs[$z][length]'></td>
</tr>
";
}
echo '</table><br><br>';
echo "<input type='submit' name='submitFieldSetup'>";
echo '</form>';
Then in third.php;
if(isset($_POST['inputs'])) {
$inputs = $_POST['inputs'];
foreach($inputs as $input) {
echo $input['name'];
echo $input['type'];
echo $input['length'];
}
}

Update Multiple rows at one time in PHP

I am trying to update multiple rows on submit of a form (in particular this one is the "hours" field.
I have it working but only one of the value updates vs all of them.
There is the possibility of having different values for each update.
The form code:
$query2 = "select * FROM work_hours WHERE formid = $formid ";
$result = $mysqli->query( $query2 );
$num_results = $result->num_rows;
if( $num_results > 0){
echo " <table border='0' align='center'>
<tr>
<td colspan='2' align='center'>
<strong> Time Away Break Down</strong>
</td>
</tr>
<tr>
<td align='center'>Date</td>
<td align='left'>Hours</td>
</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo " <tr>
<td class='hidden_sm' align='center'>
<input type='text' name='id' size='10' value='$id' class='dept' readonly style='width:30px;'>
<input type='text' name='date' size='40' value='$date' class='dept' readonly> <input type='text' name='end_date' size='40' value='$end_date' class='dept' readonly>
</td>
<td class='hidden_sm' align='left' >
<input type='text' name='hours' size='10' style='width:30px;' value='$hours' class='dept' >
</td>
</tr>
";
}
echo "<tr>
<td colspan='2' align='center'>
<input type='submit' name='Submit' value='Submit Request'>
</td>
</tr>
</form>
</table>";//end table
Submit Code:
$id = $_POST['id'];
$formid = $_POST['formid'];
$hours = $_POST['hours'];
include 'connect-db.php';
$stmt = $mysqli->prepare("UPDATE work_hours SET hours = ? WHERE formid = ?");
$stmt->bind_param('si',
$_POST['hours'],
$_POST['formid']);
$stmt->execute();
if ( $stmt ) {
echo "<p align='center'>Thank you, this request has been approved.<BR>You will be redirected in 5 seconds</p>";
} else {
echo "Error, you status cannot be updated. <BR> Please contact your system administrator.";
}
$stmt->close();
?>
Could anyone point me in the right direction to have all values update on submit, as I have had zero luck.
As well I do understand the need to prevent SQL Injections, and that I am working, so no need to remind me.
Thanks in advance!
Looks like you'll want to use a CASE statement as explained here:
How does MySQL CASE work?
Use a loop to build the statement and you're better off using the id as the identifier instead of formid, since the id is the unique value and you could have different results in the form.

Categories