How to post many text boxes in one form to databases - php

I have this script where I want to post values from the form into my database. I want to post many text boxes into one form.
The problem is, only the last text box gets updated.
<form action='' method='post'>
<?php
for ($i=0; $i<$count; $i++) {
echo "<input type='text' name='".$i."' />
<input type='hidden' name='img_id' value='".$img_id."' />
<input type='hidden' name='loop' value='".$i."' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
//now i need help
$query = "UPDATE photos SET description = '".$_POST[$_POST['loop']]."' WHERE id = '".$img_id."'";
$res = mysql_query($query) or die(mysql_error);
}
?>
For example, loop goes 4 times, that means I have 4 text boxes.
In my script, which is not fully written here, the code updates only the last loop into the database.

First, you need to use the PHP array notation for the name attribute in your HTML. Otherwise you will have multiple inputs with the same name and only one of those will be posted
echo "<input type='text' name='".$i."' />
<input type='hidden' name='img_id[]' value='".$img_id."' />
<input type='hidden' name='loop[]' value='".$i."' />";
Now you can access those elements in your PHP as an array.
foreach ($_POST['img_id'] as $key=>$value) {
// img_id's value is in $value
// loop's value is in $_POST['loop'][$key]
}
Use the foreach to build your queries and after the loop execute all of them.
Your query is wide open to SQL injections. Use mysql_real_escape_string at least (even better: prepared statements, this will also be better performance wise since you always have the same query only different input). Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.

You need to make the names of your form elements unique so that they don't clash. One way would be to post them through as arrays instead:
Also, can't you use the same $count variable to loop over the potential results?
It looks like you're posting the hiddens with the name loop in order to pass through how many times over the loop you need to go. If not, you can post the loop variable through like this, but you use it in place of $count for your update loop. Also, I'd consider just adding that element in once, at the end - using the value of $count instead.
E.g. Something like this may work for you...
<form action='' method='post'>
<?php
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$count; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name' ] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>
Or, if you can't use $count:
<form action='' method='post'>
<?php
echo( "<input type='hidden' name='loop' value='".$count."' />" );
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$_POST['loop']; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name'] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][$i]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>

You have to use something like,
if (isset($_POST['update'])) {
for($i=0; $i<=$_POST['loop']; $i++){

Related

How to store POST values for each form in an array?

Basically the code is supposed to be simple yet it is not working!
First page:
<?php
$i = 1;
$var;
while($i != 10)
{
$var="
<form id='upload' action='test2.php' method='POST'>
<input type='hidden' name='test' value='{$i}'>
<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div> ";
$i = $i+1;
echo $var;
}
?>
Second page:
<?php
echo $_POST['test'];
?>
when I run the code I always get the last value only (9) ... I need to have different value for each button .. can you help me?
You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i.
echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';
In test2.php, $_POST['test'] will have the $i value of the button you clicked.
Create single form with multiple input elements name as an array to get multiple value's in single input
Try this:
<input type='hidden' name='test[]' value='{$i}'>
Now you will receive an array of test as $_POST['test'] with different values
The problem with the other proposed solution is that you will have 10 forms, but you won't be able to submit all of the items at once. You will only be able to submit the value of one of them.
I believe you're trying to create 10 input elements instead of 10 separate forms:
<?php
$i = 1;
$var;
$var .= "<form id='upload' action='test2.php' method='POST'>"
while($i != 10)
{
$var .= "<input type='hidden' name='test[]' value='{$i}'>"
$i = $i+1;
}
$var .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div>"
echo $var
?>
Here's code that I would suggest instead of what you've got:
<?php
$html = "<form id='upload' action='test2.php' method='POST'>";
for ($i = 1; $i <= 10; $i++){
$html .= "<input type='hidden' name='test[]' value='{$i}'>";
}
$html .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>"
$html .= "</form>";
echo $html
?>
It's a little more work than what Mahesh's answer would require
First, a question: do you really want 10 forms - or do you want one form with 10 questions. Keep in mind that $_POST will only contain values from the form which was submitted (read: one form).
I think you want something like
<form id="upload" action="test2.php" method="POST">
<?php for ($i = 0; $i < 10; $i++) { ?>
<input name="test[]" type="hidden" value="<?=$i?>">
<?php } ?>
<button type="submit">submit</button>
</form>
edit: given your response below, why not use query parameters?
Go to Album <?=$i?>

PHP While Loop - Hidden Input Values - SQL

I'm trying to shows a number of rows one after the other, with a verify button underneath, which one clicked will go to the update (php) method. I've got it working, however it is always the same value (first alphabetical last name).
What I need to try and do is get it to pick the lastname from the row when submitted. Here is the code:
<div class="container">
<form action="update.php" method="POST">
<?php
//executes the SQL query
$result = mysql_query("SELECT * FROM Sheet1");
//returns the result from the database
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
}
?>
</form>
</div>
You need to create array of input to get all the values like
<input type='hidden' name='HiddenInput[]' value='".$LastName."'>
Then $_POST['HiddenInput'] will return array that contain all value of $LastName
take a look at this code.. give different names to all the hidden fields..
$i = 0;
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput_".$i."' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
$i++;
}

how to loop one radio button with different id

I'm trying to create a radio button in a loop, but I want to assign a unique id so it becomes a group where only one radio can be selected. Is this possible? My loop duplicates the same radio and I can select every button. I only want to select one.
<?php
$i = 0;
$i = $i++;
while($i>=0)
{
echo "<input type='radio' name='test[$i++]' value='test[$i++]'>test[$i++] ";
echo "<BR>";
$i++;
}
?>
UPDATE THIS WORKS!!!
<?php
$i = 0;
while($i++ < 5)
{
echo "<input type='radio' name='test' value='test[$i]'>test[$i] ";
}
?>
Learn basic PHP syntax. "$i++" is a string that contains a variable ($i), and two + characters. it's NOT going to increment your $i variable.
You're literally generating the following html:
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
As well, as written, your while() is going to be an infinite loop, since $i will ALWAYS be greater than 0.
Try this instead:
while($i++ < $limit) {
echo "<input type='radio' name='test[$i]' value='test[$i]'>test[$i] ";
}
you can use of bottom code.please preuse input tag(value).
`<?php for($i=1;$i<10;$i++) { `?>`
<input name="RadioBox" type="radio" value="<?php print($i); ?>" />
<?php } ?>

Check boxes checked?

Now I've done a lot of research, tried a lot of methods in PHP, including $_POST isset..
foreach
etc
But i need some help!
Basically, just want to check if the checkboxes have been checked. And THEN, add 1 to the number of $visits made if the checkbox has been checked.
As long as I can check if the checkbox is checked, i think i can figure it out from there!
Thanks in advance
( note: $visits is the number of times a property has been visited. This coding displays property information read from a file)
<?
$filename = "house.txt";
$filepointer = fopen($filename,"r"); // open for read
?>
<html>
<head>
<h1> Search for properties</h1>
<form method = "post" action= "visit.php">
Enter max price
<input type = "text" name = "max" value="<?=$Max;?>">
<input type = "submit" name = "submit">
<br><i><p>Properties found</p></i></br>
</form>
</head>
</html>
<?
$myarray = file ($filename);
for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time
$aline = $myarray[$mycount];
$postcode = getvalue($aline,0);
$value = getvalue($aline,1);
$image = getvalue ($aline,2);
$visits = getvalue($aline,3);
$Max = $_POST['max'];
if ($value < $Max) {
print "<table border = 2>";
print "<FORM METHOD='POST' ACTION='visit.php' >";
print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
print "<td>$value <BR>";
print "<td>$image<BR>";
print "<td>$visits<BR><p>";
print "</table>";
print "</form>";
}
}
function getvalue ($aline, $commaToLookFor) {
$intoarray = explode(",",$aline);
return $intoarray[ $commaToLookFor];
}
if (isset($_POST['check']) && $_POST['check'] == 'Yes') {
echo "checked!";
} else {
echo "not checked!.";
}
?>
You're submitting a different form than the one you think you are...you have two forms on the page, both submitting to "visit.php". This line shouldn't exist:
print "<FORM METHOD='POST' ACTION='visit.php' >";
...since you've already created the form at the top of your file.
This will require a little reorganization of your code, but the basic idea is that you want one and only one form that contains all the fields and the submit button, otherwise you're submitting the form that contains the max price and nothing else.
Alternatively, if you actually do need separate forms (I'm not familiar enough with your use case to be sure), then the second form would need its own submit button.
Simplified working code:
print "<table border = 2>";
print "<FORM METHOD='POST' ACTION='visit.php' >";
print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
print "<td> <button type='submit'>Submit</button> </td><BR> \n";
print "</table>";
print "</form>";
//personally I would just check for isset($_POST['check']), but it doesn't really matter...
if (isset($_POST['check']) && $_POST['check'] == 'Yes')
{
echo "checked!";
}
else
{
echo "not checked!.";
}

What am I doing wrong with my PHP text boxes piece of code?

// Hy..I have the following piece of code:
mysql_select_db("baza_chestionar", $con);
$result = mysql_query("SELECT intrebari.descriere_intrebare,intrebari.nume_admin,intrebari.id_chestionar FROM intrebari WHERE intrebari.nume_admin='".$_SESSION['nume_admin']."' AND intrebari.id_chestionar='".$_SESSION['nr_chestionar']."' ");
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.")&nbsp". $row['descriere_intrebare'];
echo "<br><br>";
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >
<input type='text' size='30' name='intrebare[]'>
</form> ";
echo "<br><br>";
}
echo "<input type='submit' value='Salveaza raspunsuri' onclick='myform.submit()'/>";
mysql_close($con);
// This select some data from a table and display it and for each data a textbox. I have another page that takes the data from the textboxes and insert it in another table. Here is the code:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO raspunsuri values ('','$textbox')";
var_dump($sql);
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
// But it only inserts the first value of the first text box. What am I doing wrong? I declared the name of the text box as an array an I loop thru with a foreach statement..
You're looping the myform form as as well. The FORM tag should be taken out of the while loop and the submit button should be within the form tag (so you can use it without JS). Also, just print_r your POST data to confirm you have the data there.
Your problem is that you use more than one form tag in your page. So only one value text box is send to your second page. You must declare the form tag before the while loop and you close the tag after your loop.
Your form tag is echoed inside the loop. Put is outside.
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >\n";
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'] ."<br /><br /><input type='text' size='30' name='intrebare[]'><br /><br />";
}
echo "<input type='submit' value='Salveaza raspunsuri' name="submit" />\n";
."</form>\n";

Categories