Loop inserting only last values
i am using the php to get all the values from textbox, inside a loop i am getting the values.
inside a loop i also have the insert statement and the count.
The flow is: i will select the value from drop-down list, when i select the number from the drop down list, the corresponding number of text-boxes will be created.
for ex:- i have selected the number 3 from drop-down list. so it will create 3 text-boxes.
now i will enter data into those 3 text-boxes and click submit. when i click submit only the value of last text-box is getting inserted into database.
ie when i select 3 and enter data to 3 text-box it is inserting only 3rd text-box value 3 times, instead of inserting 1st,2nd and 3rd value..
how can i solve it..?
here is code i have used..
php:
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
}
?>
range is a value what i have selected in dropdown.
javascript code:
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
}
this is generating the textbox dynamically
the problem is that you are creating new input with the same name as previously created ones, php's $_POST[] goes on the inputs name to get arround this you will need to create inputs with unique names something like
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname_' + i + '">';
}
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
}
?>
this will however only fork for adding inputs once or you will create inputs with identicle names again, to get arround this i would reccomend storing the total count in a hidden input called count <input type="hidden" value="0" name="count" />
the basing the names off of that count
var count = $('input[name=count]');
for(i = 0+count; i < param+count; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
}
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
}
?>
i havnt tested this code so you might need to tinker a bit to make it work but the concept is there
尝试
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i <= count($_POST["Fname"]); $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname][$i]','$_POST[language][$i]') ") or die(mysql_error());
}
?>
You are using $_POST["Fname"] for the insert. So instead you should use count($_POST["Fname"]) in the for statement
链接地址: http://www.djcxy.com/p/58578.html上一篇: 价值大于或小于..困惑
下一篇: 循环只插入最后一个值