I understand to call and update since rows using prepared statements but I'm having trouble understanding how to update multiple records.
I have a simple attendance register with a select box to define if they attended or not. I call the rows in a table format:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tbody>
<tr>
<th scope="col">Name</th>
<th scope="col">Attendance</th>
</tr>
<?php if($Event_list->rowCount() > 0) {
foreach ($Event_list->fetchAll() as $Event_list_row) { ?>
<tr>
<td>
<?php echo ucwords($Event_list_row['firstname'])." ".ucwords($Event_list_row['surname']); ?>
</td>
<td>
<input type="hidden" name="id" value="<?php echo $Event_list_row['booking_id']; ?>">
<select name="outcome">
<option value="0">Choose</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
<input type="submit" value="Update" name="update">
</form>
I thought I could just use foreach again to update each record, but clearly I'm doing something wrong:
if(isset($_POST["update"])) {
$Update_ID = $_POST["id"];
$Update_Outcome = $_POST["outcome"];
foreach($_POST['id'] as $count => $id) {
$Attendance_Update_SQL = "
UPDATE event_booking SET
`confirmed`= :outcome
WHERE `id`= :id";
$Attendance_Update = $dbconn->prepare($Attendance_Update_SQL);
$Attendance_Update->bindParam(':id', $Update_ID[$count], PDO::PARAM_STR);
$Attendance_Update->bindParam(':outcome', $Update_Outcome[$count], PDO::PARAM_STR);
if($Attendance_Update->execute()) {
// Add in success message?
}
}
}
I get the following error:
Warning: Invalid argument supplied for foreach()
relating to the foreach row
Quick Fix:
rename name="id" to name="id[]"
rename name="outcome" to name="outcome[]"
Note: this syntax and reliance on index is problematic when you add checkbox types to a form.
Ok, the biggest problem is your input names are going to conflict
eg <select name="outcome"> will only set the last records outcome from your form.
I would use var_dump($_POST) to understand your post data
Suggestion, rename your "name" attributes as name="record[id][property]"...
<select name="record[<?php echo $Event_list_row['booking_id']; ?>][outcome]">
<option value="0">Choose</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
You would then use
foreach( $_POST['record'] as $id => $data )
where $id and $data['outcome'] is now clearer to understand and use.
Related
I am creating rows of HTML table like this
<?php foreach($products as $key=>$val) { ?>
<tr class="tr_entry">
<td><input class="input_entry" id="no" name="no" type="text"></td>
<td>
<select id = "stage" name="stage">
<option value="1">01</option>
<option value="2">02</option>
</select>
</td>
</tr>
<?php } ?>
and posting this form in a controller using "post" method. In controller I am using this
print_r($_POST);exit;
but I am getting only data of last row of table. Need help.
For all the iterations of for loop your input's name are same.For each iteration of for loop, old values are replaced by new values,so
at the end for loop you will get values of last tr of the table.
Use below code to get all the inputs in form.
<?php foreach($products as $key=>$val) { ?>
<tr class="tr_entry">
<td><input class="input_entry" id="no" name="no<?php echo $key ?>" type="text"></td>
<td>
<select id = "stage" name="stage<?php echo $key ?>">
<option value="1">01</option>
<option value="2">02</option>
</select>
</td>
</tr>
<?php } ?>
I need a solution for this. Here I want to validate form elements. I did it with PHP but what my problem is on clicking submit button validation process takes place but select box value changes to select dept.
FOR EXAMPLE, if you just click submit button without giving student id by only select dept.. the message shows enter studentid but select box value changes to select dept again as it page reloads first..
php:
if($_POST['submit']!='')
{
$c=0;
if($_POST['studid']=='' ) {
$msg_id="Enter stud id";
$c++;
}
if($_POST[studdept]=='') {
$msg_dept="Enter stud dept";
$c++;
}
if($c==0) {
echo "form has been submitted..";
}
}
HTML:
<form id="myform" action="" method="post">
<table>
<tr>
<td> Studid: *</td> <td> <input type="text" name="studid" maxlength="10" value="<?=$_POST[studid]?>"> </td> <td> <?php echo $msg_id; ?> </td> </tr>
<tr>
<td> StudDept: *</td>
<td>
<select name="studdept">
<option value="" selected="selected" >select dept</option>
<option value="cse" >cse</option>
<option value="eee" >eee</option>
<option value="mech" >mech</option>
<option value="ece" >ece</option>
</select>
</td>
<td><?php echo $msg_dept; ?> </td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
can someone help me here?
Thanks in Advance...
You have to generate your <option></option> list through loop (if you're generating Department list from Database Table) where you have to put condition which you had put it for first <option> pair.
Or You can put a Turnary Operator (if you're using static list).
<select name="studdept">
<option value="none" <?php echo ($_REQUEST['studdept']=='none'?'selected="selected"'):'';?> >select dept</option>
<option value="cse" <?php echo ($_REQUEST['studdept']=='cse'?'selected="selected"'):'';?> >cse</option>
<option value="eee" <?php echo ($_REQUEST['studdept']=='eee'?'selected="selected"'):'';?> >eee</option>
<option value="mech" <?php echo ($_REQUEST['studdept']=='mech'?'selected="selected"'):'';?> >mech</option>
<option value="ece" <?php echo ($_REQUEST['studdept']=='ece'?'selected="selected"');:''?> >ece</option>
</select>
And use $_REQUEST instead $_GET or $_POST.
The method of your form is POST and you are trying to get the department using GET <? echo ($_GET['studdept'] == 'cse' ? 'selected="selected"' : '') ?>.
Anyway, you'll need to improve your HTML so you check if any of the deptartments are selected (not just 'cse'). A for/while loop will do the job.
In addition, I would add " " to the post variables, as you did in if($_POST['studid']=='' ) (but you didn't in if($_POST[studdept]==''))
Your specifies that the form data will be submitted using the POST method.
The way that PHP does this is to store all the "posted" values into an associative array called "$_POST".
Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Use: $_POST['studdept'] instead of: $_GET['studdept']
Beacause you use method POST for send data and you are trying to get value using $_GET['studdept'].
so you have two ways:
(1) just change the method to GET or
(2) change $_GET['studdept'] to $_POST['studdept'].
prepared statement : i am using two while loop, but my inner while loop is not working. when i use inner while loop its showing me only one record but when i remove inner while loop i can see all the records..how can i solve it?
here is my code
<table width="100%" style="background-color:#F2F2F2;border:1px solid #F2F2F2;border-radius:15px;">
<form name="form1" id="form1" method="post" action="">
<tr>
<td align="">
<table width="100%" style="background-color:#F2F2F2;border:1px solid #F2F2F2;border-radius:15px;">
<tr class="table-heading">
<td > Qty.</td>
<td > Writer</td>
<td > Status</td>
<td > </td>
</tr><?php
if($stmt->prepare("select id,qty,action_flag,status,writer from tbl_order where status=? and action_flag=? and order_id=?"))
{
$action='confirm';
$status='orderprocessing';
$ordid=$_GET["ordid"];
$stmt->bind_param('sss',$status,$action,$ordid);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$qty,$action_flag,$status,$writer);
}
while($stmt->fetch()) // outer while loop
{
?>
<tr align="left" style="line-height:30px;font-size:12px;">
<td > <?php echo $qty; ?></td>
<td align="center" width="160" >
<select name="selwrt" id="selwrt" class="reginput" style="text-transform:capitalize;width:150px;height:25px;" >
<option value="">Select Writer</option>
<?php
if($stmt->prepare("select id,writer_name from tbl_writer order by writer_name"))
{
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($wid,$writer_name);
}
while($stmt->fetch()) // inner while loop
{
?>
<option value="<?php echo $wid; ?>"<?php if($writer==$wid) echo 'selected="selected"'; ?>><?php echo $writer_name; ?></option><?php } ?>
</select>
</td>
<td align="center" width="160" ><select name="selst" id="selst" class="reginput" style="text-transform:capitalize;width:150px;height:25px;">
<option value="">Select Status</option>
<option value="inprogress"<?php if($status=='inprogress') echo 'selected="selected"'; ?>>In Progress</option>
<option value="jobdone"<?php if($status=='jobdone') echo 'selected="selected"'; ?>>Job Done</option>
</select>
</td>
<td style="vertical-align:top;" ><input type="submit" name="btnsave" id="btnsave" value="SAVE" class="btnstyle" style="padding:3px;"/>
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" ></td>
</tr><?php } ?>
<tr><td> </td></tr>
</table>
It's because you're replacing the initial result with the second one on the first time through the loop. Since you want to use both result sets at the same time, use separate variables for the two requests - i.e. replace $stmt inside the while loop with a different instance.
As Mark stated, You are using the same value for both. I don't really know what data you are calling however as they the same the following occurs.
While (outer loop) {
Starts with first record
While (Inner Loop) {
Goes through all the data
}
Goes back to the top and than realizes that all the data is completed (which was done in the inner loop)
}
2 Ways to solve this. Use a different object for each while which could be fairly impracticable. (two separate methods doing the same thing)
You could also maybe use a foreach loop. This would also solve the problem in a cleaner fashion if it works.
I need an example that can let me populate the 2nd,3rd,...,7th dropdown list (all same entries) after the 1st dropdown list has been selected. I have all the values stored in an array in PHP before the HTML head.
I've looked at some examples, and couldnt find a specific solution
For example:
Let's say I have a warehouse full of franchisees and outlets under those aforementioned franchisees, I need to create order lists for 7 days ahead for that specific franchisee with n outlets. My solution was that the user/warehouse operator would pick a franchisee, then the 2nd (up to 7th) drop down list would have the outlets who are under that picked franchisee.
Thanks.
<?php
$productClass = new product();
$productClass->setProductList();
$franchiseeClass = new users();
$franchiseeClass->getAndSetAllFranchisees();
$franchiseeArray = $franchiseeClass->getUserList();
$operatorOutletClass = new users();
$operatorOutletClass->getAndSetUserByLevel("5");
?>
<html><head></head><body>
<div align="center">
<form name="BBOFranchisee" method="post" action="?" onSubmit="return checkSubmit()">
<table align="center" border="1">
<tr>
<td>
Franchisee:
</td>
<td colspan="8">
<select name="displayFranchisee" size="1" onChange="populateOutlet('findOutletByFranchisee.php?fid='+this.value)">
<option label="franchisees" value="0">--Choose Franchisee--</option>
<?php
for($i=0;$i<count($franchiseeArray);$i++)
{
foreach($franchiseeArray[$i] as $key => $val)
{
echo "<option label=\"franchisees\" value=\"$key\">$val</option>\n";
}
} ?>
</select>
</td>
</tr>
<tr>
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
</tr>
</body>
</html>
I guess the continuation is the javascript part.. i tried to look at the solution that http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html provides, but it wont work with multiple drop downs.
In order to use that javascript example, you need to add an id to your 2nd-7th selects - id="displayOutlet<?php echo $j; ?>"
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" id="displayOutlet<?php echo $j; ?>"size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
Then in your javascript code, you can add a loop for the 2nd-7th selects.
if(req.status == 200) { // which represents ok status
for(var i=0;i<7;i++){
document.getElementById('displayOutlet'+i).innerHTML=req.responseText;
}
}
in my page when a user login into his acount there are two fields name and project so whenever he login his named filled in name field and his all projects are listed in dropdown in project field.my problem is tha when i edit this record it shows me the name and the project selected by him on submission but i want that on edit his all project will b listed there as on submission time.
Here is my project code for dropdown on submission time:
<tr>
<td>Select Project</td>
<td>
<select id="project" name="project">
<option value="">-- select --</option>
<?php while($row = mysql_fetch_array($result))
{
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)){ ?>
<option value="<?php echo htmlspecialchars($row['projectname']);?>"><?php echo htmlspecialchars($row['projectname']); ?></option>
<?php } }?>
</select></td>
</tr>
and here is my code for project field on editing:
<tr>
<td>Select Project</td>
<td><select name="project" id="project" style="width:145px">
<option>Select</option>
<option selected="selected" value="<?php echo $fetuser1['project'];?>"><?php echo $fetuser1['project'];?></option>
</select></td>
</tr>
So when editing you must do things much the same as on "submission time".
<tr>
<td>Select Project</td>
<td>
<select name="project" id="project" style="width:145px">
<option>Select</option>
<?php
while($row = mysql_fetch_array($result)) {
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)) {
?>
<option
<?php if($fetchuser1['project'] == $row['projectname']) echo "selected='selected'";?>
value="<?php echo htmlspecialchars($row['projectname']);?>"
>
<?php echo htmlspecialchars($row['projectname']); ?>
</option>
<?php } }?>
</select>
</td>
</tr>
depends...
what do your query look like?
one query? two querys? one on the userid, name and stuff and another on the projects...
or are all project in the same table as the fetched username?
and how are thy stored (blob, json, simple list)?
And last: mixing php inline with html is a very bad habbit ;)
say you have two querys
//query SELECT projectname FROM whatevertable WHERE userid = 'users-id'
$options = "<option value="">-- select --</option>".PHP_EOL;
while($row = mysql_fetch_array($result))
{
if(htmlspecialchars($row['projectname']) == $_SESSION['preselected']) //whatever
$options. = "<option selected="selected" value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
else
$options. = "<option value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
}
and after all the php code you can
echo "<td>Select Project</td>
<td>
<select id=/"project/" name=/"project/">
{$options}
</select>
</td>";
But without knowing any of your code but that little php-mixed-html it's hard to answer precisely