PDA

View Full Version : php/mysql insert.



Utech22
March 30th, 2008, 10:36 AM
I am displaying a form fields '$amount times' (4 times) and want the value to go to the database '$amount times' (4 times) . Such that form feilds 1,form feilds 2 & form feilds 3
are inserted into the table all at once.


$amount = 4;
for ($i=0;$i<$amount;$i++)
{
$queryE = ( "insert into table(value1,value2,value3)
values('$value1','$value2','$value3')");
mysql_query($queryE);

}

With this code only the last value (form field 3) is inserted, Why?

Utech22
March 30th, 2008, 10:40 AM
Will this work?


$amount = 4;
for ($i=0;$i<$amount;$i++)
{
$queryE[$i] = ( "insert into table(value1,value2,value3)
values ('$value1','$value2','$value3')");

mysql_query($queryE[$i]);
}


oh no this just insert the data from the last feilds (form field 3) '$amount times' (4 times);

kdd
March 30th, 2008, 01:31 PM
insert into database??? ooooohhhh, that's not goooood...
I hope you have all the linking and selecting done correctly...
After that,


for ( $i = 0 ; $i < $amt ; $i++ )
{
$query = "INSERT INTO table (col1,col2,col3) VALUES ('$val1','$val2','$val3')";
mysql_query($query);
}
Meaning, you don't need array.
The problem is you've 3 cols, and you're inserting 4 values??? What's the first empty string ('') for?

Hope this helps! :)

Edit: Are you working on some big project or something? Just curious, as there are many threads from you here. :)

Utech22
March 30th, 2008, 03:11 PM
.........
The problem is you've 3 cols, and you're inserting 4 values??? What's the first empty string ('') for?...........
That's a typo.

I have a for loop to display the form fields : typical eg


$amount = 4;
for ($i=0; $i<$amount; $i++)
{
#EVENTS
Event Number $i+1 #+1 to change 0 to 1
What<input type=text name="what" ... />
Where<input type=text name="where" ... />
When<input type=text name="when" ... />
}
Now base and the value of $amount (4), the above fields will display 4 times,
then I can input 4 different events and save them to the table.

for ($i=0; $i<$amount; $i++)
{
$queryE = ( "insert into table(value1,value2,value3)
values ('$value1','$value2','$value3')");

mysql_query($queryE);
}

This only save the last form field that the for loop displayed (that would be Event Number 4)

I want All the events to be saved, once I click save button.
that is : Event Number 1, Event Number 2, Event Number 3, Event Number 4

kdd
March 30th, 2008, 03:55 PM
Sounds much different from your original question.

Anyways, this is how I'd do it:


for ( $i = 0 ; $i < $amt ; $i++ )
{
echo "What<input type='text' id='what$i' />";
...
}
This correctly outputs the form.

Then, I guess, when you submit using POST, you do:


for ( $i = 0 ; $i < $amt ; $i++ )
{
$what = $_POST['what' . $i];
... //means get other stuff...
//and then do query stuff here
$query = "INSERT ... VALUES ($what,$where...)";
}
Hope this helps. :)

Utech22
March 30th, 2008, 07:39 PM
not working.
I am trying to echo the info first, to see if it will output correctly
before going to the database.



for ($i=0; $i<$amount; $i++)
{
//$queryE = ( "insert into table(value1,value2,value3)
//values ('$value1','$value2','$value3')");
//mysql_query($queryE);
echo "What: ".$value1;
echo "Where: ".$value2;
echo "When: ".$value3;
}