Problem with entering data from multiple table rows.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UrButtFullOfArr0ws
Forum Commoner
Posts: 64
Joined: Wed Feb 21, 2007 11:42 am
Location: Up a tree >.>"

Problem with entering data from multiple table rows.

Post by UrButtFullOfArr0ws »

I had entered this in the Theory and Design section but decided to move it over here for 2 reasons... 1) it's not that complicated or advanced actually :) and 2) Noone seemed to give me an answer for 3 days :( and i'm in a bit of a hurry. Sorry to mods.

After a roughly superficial search of Theory And Design section and a bit deeper search of Code Snippets i decided to ask for help with this:

I have the need for a code that would echo as many lines (comprised of multiple text-boxes) as the user wants (with radio buttons of course so that it doesnt get over-flowed) in a table and would enter the data in the database that the user entered in the text-boxes.

I've kind of figured out a way of doing the first part, however i have a problem with entering it in the database. However both parts are found in the same file:

Code: Select all

//These are the lines
for($a = 1; $a <= $_POST[rows]; $a++)
{
echo "<td>";
echo "<tr ......details about the box....><form method='POST' action='dummy.php'><input type='text' name='something" . $a ."'></td>";
echo "<tr ......details about the box....><input type='text' name='somethingelse" . $a ."'></td>";
//.
//.
//.
//.
//goes in same way for some rows, as many as the columns are,
//.
//.
//.
}
	echo "</tr><input type='hidden' value='" . $_POST[rows] . "' name='rows'>";
?>

</table>
<p>
<input type="hidden" name="submit" value="0">
<input type="submit" value="Submit">
</form>
</p>
//here the php stops so i don't make too many echos, it just doesnt look well
All of that seems to work just fine, so that's not the problem. Any suggestions of course for that part are welcome.

The problem comes when i try to do the same thing with entering the data in the database: I use the For statement again in this way:

Code: Select all

for($a = 1; $a <= $_POST[rows]; $a++)
{
$something="something" . $a;
$somethingelse="somethingelse" . $a;
//.
//.
//.
//again going on for some rows...
//.
//.
//Now comes the query, where the error in my thought actually is:
$sqlstatement = "INSERT INTO dummy VALUES('" . mysql_real_escape_string($_POST[$something]) . "', '" . mysql_real_escape_string($_POST[$somethingelse]) . "','"; //it of course doesnt stop there.
$query = mysql_query($sqlstatement, $con); //$con is defined at the beggining of the file and db is considered fully working
So basically what it is supposed to do is add a counter at the end of the name for obvious reasons.
I figured out immediately that $_POST[$variable] most probably wouldn't work.
Any ideas and suggestions?
Thx in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Each of your textareas are in their own form block, so you will only ever return one text area. No need for a loop on the receiving end. If you want them all submitted then you need the form block outside your generation loop. And you should probably pass the fields as an array or suffix the names with a incremented number.
(#10850)
UrButtFullOfArr0ws
Forum Commoner
Posts: 64
Joined: Wed Feb 21, 2007 11:42 am
Location: Up a tree >.>"

Post by UrButtFullOfArr0ws »

Each of your textareas are in their own form block
You mean the input tag?
so you will only ever return one text area.
??
If you want them all submitted then you need the form block outside your generation loop.
Ehm if u mean the table row generation ...it's already outside the loop.
And you should probably pass the fields as an array or suffix the names with a incremented number.
Does $_POST pass arrays? And that's what im actually trying to do...suffix the names with an incremented number. The loop does 2 things increment the index number and insert each table row in the database, so unless there is another way to insert ALL of the rows at the same time i don't see how im able to do that without a loop.
Thx for trying to help...looking forward for ur answer. Going to sleep now :wink:

P.S. I'm not an expert so some sample code would definately be useful :)
Post Reply