Submitting always gets the last value - php

<form action="" method="post">
<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims" ;
$result = mysql_query($sql,$con);
while($row = mysql_fetch_assoc($result)){
?>
<ul class="category_list">
<input type="hidden" value="$id1" name="hidden">
<li><?php echo $row['phonenr'];?><input type="hidden" value="<?php echo $row['id'];?>" name="id"></li>
</ul>
<?php
}
?>
<input type="submit" name="submit">
</form>
So i got the above form where you can select phonenumbers and when you submit them a database should be updated. And there are 23 id's in it. After submitting the form it always takes the last value. What am i doing wrong?
if(#$_POST ['submit'])
{
$id = $_POST["id"];
echo $id;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}

Change your hidden field name to array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
then on PHP side use loop to retrieve
foreach ($_POST['id'] as $val) {
$id = $val;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}

Slight modification specified by chandresh_cool, would get the result that you expect.
The input name is replaced with id, so the post key contains only the row[id], not the $_POST['id']
Instead change the name of the hidden field to accept as a array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
Then you can iterate id array as specified by chandresh_cool

Related

Post dynamic checkbox ids with values

Here is a form which contains dynamic checkbox retrieved from database table named "categories". All i am trying is to echo category id + name on a new page. I want all the selected categories with their ID's. the problem is the output shows the category names but it shows the last category id with each name.
Form
<form method="post" action="insert_try.php" enctype="multipart/form-data">
<label>Select categories</label>
<?php
$result = mysqli_query($con,"select * from categories");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
?>
<br><input type="checkbox" name="categories_chk[]" value="<?= $row["cat_name"]; ?>"> <?= $row["cat_name"]; ?>
<input type="text" name="cat_chk_id" value="<?= $row["cat_id"]; ?>">
<?php
}
}
?>
<input type="submit" name="submit" value="Add">
</form>
insert_try.php
if(isset($_POST['submit'])) {
$cat_chk = $_POST['categories_chk'];
$cat_chk_id=$_POST['cat_chk_id'];
foreach ($cat_chk as $checkbox) {
echo $checkbox;
}
echo $cat_chk_id;
I don't know why you need this but in your case I would do this:
while($row = mysqli_fetch_array($result)) {?>
<br><input type="checkbox" name="categories_chk[<?= $row["cat_id"]; ?>]" value="<?= $row["cat_name"]; ?>"> <?= $row["cat_name"]; ?>
<?php
}
See, I set index of categories_chk as a category id.
If you print_r($_POST['categories_chk']) you will have a key=>value array where key is category id and value is it's name.
You can iterate over it kinda:
foreach ($_POST['categories_chk'] as $id => $name) {
echo 'Category id ' . $id . ' with name ' . $name;
}
Of course, I need to mention that $row["cat_id"] must be unique for such approach, otherwise you will have two same keys in array and the latter overwrites the previous one.

Checkbox in a while loop php

I have a check box inside a while loop like this:
<form method="POST">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` ='$id' ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names" value="<? echo $get ['id'];?>"><?php echo $get ['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
The problem is at this part I am unable to get specific checkbox properties and even if the user selects two check boxes I am unable to echo the id out
<?php
if(isset($_POST['id_names']))
{
$id_names= $_POST['id_names'];
$email = mysql_query("SELECT `email` FROM users WHERE `id` = '$id_names' ");
while ($getemail = mysql_fetch_array($email))
{
echo $getemail['email'];
}
}
?>
I have tried searching for answers but I am unable to understand them. Is there a simple way to do this?
The form name name="id_names" needs to be an array to allow the parameter to carry more than one value: name="id_names[]".
$_POST['id_names'] will now be an array of all the posted values.
Here your input field is multiple so you have to use name attribute as a array:
FYI: You are using mysql that is deprecated you should use mysqli/pdo.
<form method="POST" action="test.php">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` =$id ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
Form action: test.php (If your query is okay.)
<?php
if(isset($_POST['id_names'])){
foreach ($_POST['id_names'] as $id) {
$email = mysql_query("SELECT `email` FROM users WHERE `id` = $id");
$getemail = mysql_fetch_array($email); //Here always data will single so no need while loop
print_r($getemail);
}
}
?>

Unable to echo value from html <option> from while loop

i want to echo selected parent value. but i am getting error- Notice: Undefined index:
How can i echo selected parent value then? Whats wrong i am doing?
$q = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select>';
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $parent = $_POST[$menu_name];
}
add name to the select box and get the value of select box by name.
Updated code:-
$q = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select name="menu_name">';
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $parent = $_POST['menu_name'];
}
$_POST[$menu_name] probably doesn't exist, because only two elements in your form have name attributes. The text input and the submit input.
option elements aren't posted as part of the form, but rather the selected option's value for the select element. But your select element has no name, therefore no key to use in the key/value pair, so it isn't posted.
Give the element a name:
<select name="someName">
Then in the POST, you would be able to fetch the selected value just as you do for any other form element:
$_POST['someName']
You need to add name attribute to select tag.
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select name="any_name">';
$q = mysql_query("SELECT * FROM menu");
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $select_option_name = $_POST['any_name'];
}
Note: mysql_* functions are depricated, use mysqli_* functions

Pass variables from a while to another while

I have a little problem with this form.
On the fprm page my code is:
<form action="?act=process" method="post">
<?php $sql = "SELECT * FROM schedule WHERE day = '$day' ORDER BY ID ASC";
$hours = mysql_query($sql);
while ($hour = mysql_fetch_array($hours)) { ?>
<input type="text" name="course" value="<?=$hour[course]?>">
<input type="hidden" name="id" value="<?=$hour[ID]?>">
<?php } ?>
<button type="submit">Submit</button>
On the process page looks like this:
while($id = each($_POST['id']) && $course = each($_POST['course']))
{
//echo "ID $id AND course $course<br/>";
}
All I need to do is to pass all variables in a while to update each of them in my sql database.
The error I get is: Warning: Variable passed to each() is not an array or object in /path_domain
How can I fix this?
The reason is you try to use id/course data as array, but you pass just strings. In order to avoid it, just make you form fields as arrays by adding [] to the fields names:
<input type="text" name="course[]" value="<?=$hour[course]?>">
<input type="hidden" name="id[]" value="<?=$hour[ID]?>">
By adding the [] brackets after the name of the fields.
<form action="?act=process" method="post">
<?php $sql = "SELECT * FROM schedule WHERE day = '$day' ORDER BY ID ASC";
$hours = mysql_query($sql);
while ($hour = mysql_fetch_array($hours)) { ?>
<input type="text" name="course[]" value="<?=$hour[course]?>">
<input type="hidden" name="id[]" value="<?=$hour[ID]?>">
<?php } ?>
<button type="submit">Submit</button>

Select tag with blank textbox

I have done a form which retrieves data from the database. This form allows you to display the list of students from the database but the problem is I cannot correctly input their scores individually. It does allow to insert scores but unfortunately, it only accepts the last score from the last student and inputs the same score to all students.
These are my codes:
FORM code
if ($result->num_rows > 0) { // output data of each row
while($row = $result->fetch_assoc()) {
if($row['status']=='p'){
<form name="result" method="post">
<?php { //this form will display the set of students
echo $row['lastname'] . ', ' . $row['firstname'];
echo '<input type="hidden" name="selected[]" value="'.$row['reviewee_idnumber'].'"/>'; ?>
<input type="text" name="score" required="required" size="20" placeholder="Score"/><br><br>
<?php echo '</br>';
}
} //if statement
} //while statement
?>
<input type="submit" name="submit" value="Submit"/>
<input type="hidden" name="code" value="<?php echo $code;?>"/>
<input type="hidden" name="subject" value="<?php echo $subject;?>"/>
<input type="hidden" name="items" value="<?php echo $items;?>"/>
<input type="hidden" name="date" value="<?php echo $date;?>"/>
</form>
This is where I insert my code into the database
if(isset($_POST['submit'])){
$code = $_POST['code'];
$subject = $_POST['subject'];
$items = $_POST['items'];
$date = $_POST['date'];
$score = $_POST['score']; //get score
$update = mysql_query("INSERT INTO exam (exam_code, subject, date, total_item)
VALUE ('$code','$subject', '$date', '$items')");
if(!empty($_POST['selected'])) {
$checked_count = count($_POST['selected']);// Counting number of checked checkboxes.
//echo "You have selected following ".$checked_count." option(s): <br/>";
foreach($_POST['selected'] as $selected){ // Loop to store and display values of individual inputs.
$updatedata="INSERT INTO result (score, exam_code, reviewee_idnumber)
VALUE ('$score', '$code', '$selected')";
if(#mysql_query($updatedata,$dbc)){
print '<p> successful!</p>';
}else{
print '<p> failed. '.mysql_error().'</p>';
}
It should display list students (which is fine) and right beside their name is a blank space that will accept their score (which doesn't work).
Because <form name="result" method="post"> is inside the while loop, you actually have multiple forms in your page. The submit button acts only on the last form, that's why only the last student's score gets updated.
Also, because there are multiple input elements with the same name, only a single value is sent in the POST request. A solution for this would be to include the student's ID in the name:
name="score'.$row['reviewee_idnumber'].'"
You need to update the form processing logic accordingly.
name =<?php echo "'score.$row['reviewee_idnumber']'" ?>

Categories