Update sql data from form and checkboxes - php

I have created a form that update the data in a sql column, so far all good.
The problem I have is that I would like to choose what columns are going to be updated, with checkboxes.
So I want to categorize two files as movies, I just check the checkbox and write movies in the form.
But its not only that. Every column is already connected to a checkbox, but I use this checkbox with an delete query, so I can delete multiple rows.
So my question is more like, how can I combine this two function with "one" checkbox.
This is some of my code.
This is my table with the sql outputs, and with the delete button
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
echo "<tr>";
echo "<td>".$row['user_name'] . "</td> ";
?>
<td class="download"> <?php echo $row['file_name']; ?></td>
<?php echo "<td>".$row['file_time'] . "</td> "; ?>
<?php echo "<td>".$row['file_ip'] . "</td> "; ?>
<?php echo "<td>".$row['category'] . "</td> "; ?>
<td>
<form action="delete.php" method="post">
<input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>">
</td>
<?php
}
echo "</tr>";
echo "</table>";
?>
<input type ="submit" value ="Delete">
</form>
This form is the one with the update function
<form action="function.php" method="post">
category: <input type="text" name="category" />
<input type="submit" />
</form>
and
function.php
include('core/inc/init.inc.php');
if(isset($_POST['category'])){
$category = $_POST['category'];
mysql_query("UPDATE files SET category='$category'
/*WHERE category='genom' */ ");
header("Location: profile.php?uid=" . $_SESSION['uid']);
}else{
header("Location: profile.php?uid=" . $_SESSION['uid']);
};
How can I combine these two functions?

You can have multiple submit buttons in the same form. So you can do something like this:
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<form action="updateordelete.php" method="post">
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
?>
<tr>
<td> <?php echo $row['user_name']; ?></td>
<td class="download"> <?php echo $row['file_name']; ?></td>
<td><input type="text" name="file_time[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_time']; ?>" /></td>
<td><input type="text" name="file_ip[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_ip']; ?>" /></td>
<td><input type="text" name="file_category[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['category']; ?>" /></td>
<td><input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>"></td>
</tr>
<?php } ?>
</table>
<input type ="submit" name="submittype" value = "Update">
<input type ="submit" name="submittype" value = "Delete">
</form>
And then, in updateordelete.php:
if($_POST['submittype']=="Delete"){
(do delete code...)
}elseif($_POST['submittype']=="Update"){
(do update code...)
}
For each of the values, you can access them using $_POST['file_category']. For example:
$file_categories=$_POST['file_category'];
foreach($_POST['done'] as $file_id){
echo $file_categories[$file_id];
}

Related

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

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>

Mysql UPDATE error that only affects first row?

When I use the sql UPDATE query, it only affects the first row of my members.
<?php
$query = mysqli_query($db, "SELECT * FROM `members` ORDER BY `siteRank`");
$result = mysqli_fetch_assoc($query);
?>
<?php
if($_POST['AccountUpdate'])
{
//mysqli_query($db, "UPDATE members SET username='$Username' WHERE id='$$IdentifcationNumber' ORDER BY id DESC LIMIT 1");
echo $result['id'];
echo $result['username'];
echo 'separeator';
echo $IdentifcationNumber;
}
?>
<form method="post" action="viewprofile.php">
<table border="1px">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Browser</th>
<th>IP</th>
<th>Site Rank</th>
<th>PyroCoins</th>
<th>PIN</th>
<th>Status</th>
<th>Date Joined</th>
<th>Update Account</th>
</tr>
<?php
while($result = mysqli_fetch_assoc($query)){
?>
<form method="post" action="viewprofile.php">
<tr>
<td><input readonly="readonly" value="<?php echo $result['id'];?>" /></td>
<td><?php echo $result['username'];?></td>
<td><input text="text" name="ChangePassword" value="<?php echo $result['password'];?>" /></td>
<td><?php echo $result['email'];?></td>
<td><?php echo $result['browser'];?></td>
<td><?php echo $result['ip'];?></td>
<td><?php echo $result['siteRank'];?></td>
<td><input type="text" name="ChangePyroCoins" value="<?php echo $result['PyroCoins'];?>" /></td>
<td><?php echo $result['pin'];?></td>
<td>
Current Status:
<input readonly="readonly" value="<?php echo $result['status'];?>" />
<select name="ChangeStatus">
<option value="<?php echo $result['status'];?>"><?php echo $result['status'];?></option>
<option value="[ Content Deleted ]">[ Content Deleted ]</option>
</select>
</td>
<td><?php echo $result['date'];?></td>
<td><input type="submit" name="AccountUpdate" value="Update Account"></td>
</tr>
<?php
}
?>
</table>
</form>
My $_POST data should be self explanatory, but its only affecting the first row and not the row that Im trying to affect. When I click my html update button, it only displays only the first ID and not the ID or the account credentials that i'm trying to update. For example, When I try to edit somes details with an ID of 28, it affects the first table ID which is 1
'ORDER BY id DESC LIMIT 1' in the query is unnecessary.
UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber'
#Isaak Markopoulos - this is not tested but there are no nested forms so hopefully there should be less confusion when posting the form. The id field in the html has been named so at least there should be an id available in the posted vars
<html>
<head>
<title>crazy little monkey</title>
</head>
<body>
<?php
/* This only gets executed if it is a POST request */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['AccountUpdate'] ) && isset( $_POST['IdentifcationNumber'] ) ){
/* I assume this is the correct id - there was no name for the form element previously */
$IdentifcationNumber=$_POST['IdentifcationNumber'];
$sql="UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber';";
mysqli_query( $db, $sql );
echo $result['id'];
echo $result['username'];
echo 'separeator';
echo $IdentifcationNumber;
}
/* This gets executed for any request */
$query = mysqli_query( $db, "SELECT * FROM `members` ORDER BY `siteRank`;" );
$result = mysqli_fetch_assoc( $query );
?>
<!-- parent table -->
<table border="1px">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Browser</th>
<th>IP</th>
<th>Site Rank</th>
<th>PyroCoins</th>
<th>PIN</th>
<th>Status</th>
<th>Date Joined</th>
<th>Update Account</th>
</tr>
<tr>
<td colspan=12>
<!-- begin a form / table for each row in rs -->
<?php
$rc=0;
while( $result = mysqli_fetch_assoc( $query ) ){
$rc++;
echo "
<!-- unique name for each form - not essential -->
<form name='vpf_{$rc}' action='viewprofile.php' method='post'>
<!-- nested child table fully contained within it's parent form element -->
<table>
<td><input type='text' name='IdentifcationNumber' readonly='readonly' value='".$result['id']."' /></td>
<td>".$result['username']."</td>
<td><input text='text' name='ChangePassword' value='".$result['password']."' /></td>
<td>".$result['email']."</td>
<td>".$result['browser']."</td>
<td>".$result['ip']."</td>
<td>".$result['siteRank']."</td>
<td><input type='text' name='ChangePyroCoins' value='".$result['PyroCoins']."' /></td>
<td>".$result['pin']."</td>
<td>
Current Status:
<input readonly='readonly' value='".$result['status']."' />
<select name='ChangeStatus'>
<option value='".$result['status']."'>".$result['status']."
<option value='[ Content Deleted ]'>[ Content Deleted ]
</select>
</td>
<td>".$result['date']."></td>
<td><input type='submit' name='AccountUpdate' value='Update Account'></td>
</table>
</form>";
}/* Close the while loop - note that the form(s) is/are FULLY contained within the loop - NO nesting */
?>
</td>
</tr>
</table>
</body>
</html>

Updating a html form with multiple checkboxs using php

I have a html form which i want to check some items in the form and then update the form.
(actually there is no order in which checkbox is going to be mark, it's totally random)
my problem is when i mark some of the checkboxes and press update my code will mark the top checkboxes in order.
This is what i mean :
before submiting :
after submiting :
this is my form code :
$row_counter=0;
if ($_SESSION['user_row_num']=="1")
{
?>
<form method="POST" action="" name="frm1">
<table class='styled-table' cellspacing='0' border='1'>
<tr>
<th scope='col' style='font-size:13px;'>number</th>
<th scope='col' style='font-size:13px;'>view</th>
<th scope='col' style='font-size:13px;'>edit</th>
<th scope='col' style='font-size:13px;'></th>
</tr>
<?php while($row_form = mysqli_fetch_assoc($query_formsearch))
{
//fetching from profile previous valuse of form access
$query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
$row_profile = mysqli_fetch_assoc($query_profile_check);
//creating form name from forms table for profile table
$profile_form_name="form_".$row_form['num'];
$profile_frm_name=$row_profile[$profile_form_name];
$a_form_aces=explode("-", $profile_frm_name);
$frm_view=$a_form_aces[0];
$frm_edit=$a_form_aces[1];?>
<tr>
<td align='center'><input class='styled-input' type='hidden' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
<td align='center'><input class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
<td align='center'><input class='styled-input' type='checkbox' name='view[]' id='view' <?php if($frm_view=="1")echo "checked"; ?> /></td>
<td align='center'><input class='styled-input' type='checkbox' name='edit[]' id='edit' <?php if($frm_edit=="1")echo "checked"; ?> /></td>
<td align='center'><input type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
</tr>
<?php } ?>
</table>
<input class='styled-input_2' style='padding: 5px; width: 140px;' type='submit' name='save_setting' id='save_setting' value="update" >
<div class='cleaner h30'></div>
</form>
this is my update code :
<?php
} // end of if($_SESSION['user_row_num']=="1")
// Check if button name "edit-msb" is active, do this
if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
{
for($i=0;$i<=$_SESSION['user_count'];$i++)
{
$row_no = ($_REQUEST['row_counter'][$i]);
$form_numb = $_REQUEST['form_num'][$row_no];
if(isset($_REQUEST['view'][$row_no])){$_REQUEST['view'][$row_no]="1";}else{$_REQUEST['view'][$row_no]="0";}
if(isset($_REQUEST['edit'][$row_no])){$_REQUEST['edit'][$row_no]="1";}else{$_REQUEST['edit'][$row_no]="0";}
$form_access=$_REQUEST['view'][$row_no]. "-" .$_REQUEST['edit'][$row_no];
$profile_form_num="form_". $form_numb;
$access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
}
if($access_query!='')
{
echo "<div class='cleaner h30'></div>";
echo "<b style='color:green;margin-left:10px;font-size:15px;'>the form successfully updated.</b>";
}
}
?>
I want each check box shows it's updated value after i submiting the form and in the order that i marking them but i don't know where is my mistake.
I've changed your code from checkbox into select option and it's working see if it'll work for you
form code :
<form method="POST" action="" name="frm1">
<table class='styled-table' cellspacing='0' border='1'>
<tr>
<th scope='col' style='font-size:13px;'>form number</th>
<th scope='col' style='font-size:13px;'>form name</th>
<th scope='col' style='font-size:13px;'>view</th>
<th scope='col' style='font-size:13px;'>edit</th>
<th scope='col' style='font-size:13px;'></th>
</tr>
<?php while($row_form = mysqli_fetch_assoc($query_formsearch))
{
//fetching from profile previous valuse of form access
$query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
$row_profile = mysqli_fetch_assoc($query_profile_check);
//creating form name from forms table for profile table
$profile_form_name="form_".$row_form['num'];
$profile_frm_name=$row_profile[$profile_form_name];
$a_form_aces=explode("-", $profile_frm_name);
$frm_view=$a_form_aces[0];
$frm_edit=$a_form_aces[1];?>
<tr>
<td align='center'><input class='styled-input' type='text' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
<td align='center'><input class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
<td align='center'>
<select class='styled-input' name='view[]' id='view' />
<option value="<?php echo $frm_view; ?>"><?php if($frm_view=="1"){echo "yes";}else{echo "no";} ?></option>
<option></option>
<option value="1" style="color:#1A75FF;">yes</option>
<option value="0" style="color:red;">no</option>
</select>
</td>
<td align='center'>
<select class='styled-input' name='edit[]' id='edit' />
<option value="<?php echo $frm_edit; ?>" ><?php if($frm_edit=="1"){echo "yes";}else{echo "no";} ?></option>
<option></option>
<option value="1" style="color:#1A75FF;">yes</option>
<option value="0" style="color:red;">no</option>
</select>
</td>
<td align='center'><input type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
</tr>
<?php } ?>
</table>
update code :
<?php
// Check if button name "edit-msb" is active, do this
if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
{
for($i=0;$i<$_SESSION['user_count'];$i++)
{
//$row_no = ($_REQUEST['row_counter'][$i]);
$form_access=$_POST['view'][$i]. "-" .$_POST['edit'][$i];
echo $profile_form_num="form_". $_POST['form_num'][$i];echo"<br>";
echo $form_access;echo"<br>";
$access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
}
if($access_query!='')
{
echo "<div class='cleaner h30'></div>";
echo "<b style='color:green;margin-left:10px;font-size:15px;'>form successfully updated.</b>";
}
}
?>

How to update multiple records on a nested recordsets in php mysql?

Help! I have a nested recordsets namely questions and answers and a corresponsing checkbox for each question. How can I update multiple values in a recordset? shall i loop the Update query? Any help would be extremely appreciated.
here is my code:
<?php
mysql_select_db($database_iexam, $iexam);
$query_Recordset1 = "SELECT * FROM exam_questions WHERE question_exam_id = '$exam_id'";
$Recordset1 = mysql_query($query_Recordset1, $iexam) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
do { ?>
<tr>
<th width="170" scope="col">
<input type="checkbox" name="checkbox"
value="<?php echo $row_Recordset1['question_id']; ?>"/>
Question:
</th>
<td colspan="2" scope="col">
<input name="textfield" type="text"
value="<?php echo $row_Recordset1['question_description']; ?>"
size="50"/></td>
<td width="549" colspan="2" scope="col"></td>
</tr>
<tr>
<td>Answers:</td>
<?php
mysql_select_db($database_iexam, $iexam);
$query_Recordset2 = "SELECT * FROM exam_answers WHERE answer_question_set_id = '" . $row_Recordset1['question_id'] . "'";
$Recordset2 = mysql_query($query_Recordset2, $iexam) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
do { ?>
<tr>
<td width="170"> </td>
<td colspan="2">
<input name="textfield2" type="text" size="20"
value="<?php echo $row_Recordset2['answer_description']; ?>"/>
<input
name="<?php echo $row_Recordset2['answer_question_set_id']; ?>"
type="radio"
value="<?php echo $row_Recordset2['answer_iscorrect']; ?>"/>
</td>
</tr>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
You can set checkbox name like name="checkbox[]" for question_id and name="textfield[]" for question_description and finally when you submitted this form you will get a array of this records then you set a for-loop to get separate value and update it into database or any where

Categories