Forum Moderators: coopster

Message Too Old, No Replies

Inserting Dynamic Array

Trying to insert Dynamic table

         

MATT_ta_Tat

6:39 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Hello
The page creates a table where some of the fields are pulled from a MySQL DB table, and some are input. When you click submit, I want the table (including the info input by user) inserted to a different table in the DB.
Below is the code I have so far:
-------This creates the table, and works--------
$count = 0;

while(($row = mysql_fetch_array($sql)) AND ($count < 1))
{

$count = $count + 1;

include("$sitepath/html/includes/smilies.php");
global $username, $amount, $forfeit;

print("

<tr>
<td class='content' colspan=1><li></li> $row[username]</td>
<input name='username' type='hidden' value='$row[username]'>
<input name='dues' type='hidden' value='$row[dues]'>
<input name='game_code' type='hidden' value='$row[game_code]'>
<td class='content' colspan=1><input type='DECIMAL(12,2)' name='amount' value='0.00' maxlength='10'></a></td>
<td class='content' colspan=1><input type='text' name='forfeit' value='no' maxlength='3'></td>
");
---------this inserts the data to the DB------------
if($action==close_code)
{

$insert = mysql_query("INSERT into closed_players (game_code, dues, forfeit, amount) VALUES ($_POST[game_code], $_POST[username], $_POST[dues], $_POST-[forfeit], $_POST[amount])");

---------------------------------------
What I have works, excpet that it only inserts one row to the DB, and it is the last row of the table:
i.e.
if the table is:
1 2 3
4 5 6
7 8 8

only 7 8 9 is inserted. I do not know how to accomplish this. Can someone help? Thanks.

Warboss Alex

7:16 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



You'd need one insert statement per row, as far as I know.

Salsa

7:31 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Welcome to Webmaster World, Matt,

I'm not sure I'm following all that you're doing. I guess you know that you're only selecting one row from the database, because you only allow the while loop to make one iteration, essentially while $count==0.

If you're table/form has two additional rows from user input, and you're using the same names for multiple fields, like three $username fields, in the form you're going to need to name them like $username[] so that submitting the form will generate an array that you can loop through to insert the data into closed_players. As it is, you're probably only getting the last row because the second row is overwriting the first and the third is overwriting the second because they all have the same names.

Also, I'm surprised that your INSERT didn't break altogether. Assuming that at least $_POST[username] is not a number, it should need to be enclosed in quotes, like '$_POST[username]'.

I hope this helps.

MATT_ta_Tat

11:31 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Salsa and Warboss Alex - Thank you for responding
I did not ask the question very well last time. I appologize. Let me try again
---Below is the part of a PHP page that fetches data from a MySQL DB and prints it out in a table:

$count = 0;
while(($row = mysql_fetch_array($sql)) AND ($count < 1))
{
global $username, $amount, $forfeit;

print("

<tr>
<td class='content' colspan=1><li></li> $row[username]</td>
<input name='username' type='hidden' value='$row[username]'>
<input name='dues' type='hidden' value='$row[dues]'>
<input name='game_code' type='hidden' value='$row[game_code]'>
<td class='content' colspan=1><input type='DECIMAL(12,2)' name='amount' value='0.00' maxlength='10'></a></td>
<td class='content' colspan=1><input type='text' name='forfeit' value='no' maxlength='3'></td>
");
print("</tr><br>");
}
$count = $count + 1;
__________
**********
The only thing that is missing here is the $sql with is just a query Select From etc etc etc...

This part works, and you can see the table fine.
So, for example, it might look like:
Username ¦ Amount ¦ Forfeit
Bob ¦ [10] ¦ no
Steve ¦ [20] ¦ no **user enters data in amount
Rick ¦ [30] ¦ no

Note also, as you can see there are some hidden fields.

When you click to submit, i would like the info from this table inserted into the DB(different table). So each row is a different record.

I used to have it so the last record would appear in the DB, but I've since ruined it from trying many things that don't work.
I believe my problem may be in two places:
1)in the table:
<input name='username' type='hidden' value='$row[username]'> (these lines)
2) in the insert statement.

Once again I am probably not clear here. I guess if I could see an example of a php page that retrieves records from a DB table, displays a table, lets you insert additional info (form) then on action inserts that data into another DB table. The table could have 2 lines or 100.
Do you know of a good website that has lots of PHP examples?
Thanks again
MATT

coopster

2:12 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you looked through some of the examples in our PHP Forum Library [webmasterworld.com]?

dreamcatcher

2:29 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Salsa is right, you need to use build your query as an array. Here is a quick example:

<input name='username[]' type='hidden' value='$row[username]'>
<input name='dues[]' type='hidden' value='$row[dues]'>
<input name='game_code[]' type='hidden' value='$row[game_code]'>
<input type='DECIMAL(12,2)' name='amount[]' value='0.00' maxlength='10'>
<input type='text' name='forfeit[]' value='no' maxlength='3'>

Your code would then be something like:

for ($i=0; $i<count($_POST['username']); $i++)
{

mysql_query("INSERT into closed_players (game_code, dues, forfeit, amount) VALUES ($_POST[game_code][$i], $_POST[username][$i], $_POST[dues][$i], $_POST[forfeit][$i], $_POST[amount][$i])");

}

Think that should work ok. Or at least give you some help. Oh and welcome to WebmasterWorld MATT_ta_Tat. :)

MATT_ta_Tat

1:27 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Guys
Thank you for your posts. They are very helpful, and I think I am very close
But i can't get the info to insert into the DB.
I put in the code based on Dreamcatcher's suggestions.
This is what i've got:
The table builds sucessfully. When I go to post it into the database, it won't insert. I tried the folloing print statement to see:

print($_POST[username][$i]);
print("<br>");

it prints the usernames exactly as it should.

however, with the statment:
$insert = mysql_query("INSERT INTO closed_players (username) values ($_POST[username][$i])");

it will not work. Is it a syntax thing?

Salsa

2:41 am on Dec 22, 2004 (gmt 0)

10+ Year Member



You need to put $_POST[username][$i] in single quotes, like:

$insert = mysql_query("INSERT INTO closed_players (username) values ('$_POST[username][$i]')");

dreamcatcher

3:29 am on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, Salsa is correct. Your values need to be between apostrophes (or single quotes). I just copied and pasted your code. Sorry about that.

MATT_ta_Tat

3:23 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



I tried that, along with several variations of that syntax. When i have the single quotes, I don't get a SQL error, but it inserts Array[0], Array[1], Array[2] into the database, instead of the correct info.
Here is all of the code i have:

if($action==close_code)
{
for ($i=0; $i<count($_POST['username']); $i++)
{
print($_POST[username][$i]);
print("<br>");
print($_POST[amount][$i]);
print("<br>");
print($_POST[forfeit][$i]);
print("<br>");

$insert = mysql_query("INSERT INTO closed_players (username, amount, forfeit) VALUES ('$_POST[username][$i]', '$_POST[amount][$i]', '$_POST[forfeit][$i]')");
}
}
if(!$insert){
print("There was a mysql error, please try again.");
}
else{
print("<br>You have successfully Posted the results!");
}

You can see the Print statements I have just before the insert. I don't get why it will display the correct info, but in the DB, it says Array[0], etc...

Salsa

4:45 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Well, gosh, I think you need to also put [username] in quotes, like: $_POST['username'][$i]--as you did in your for statement. You need to do that with all non-integer or constant array elements. The reason why your "print" echoed out okay was probably because when PHP couldn't find a constant defined as "username" it assumed that you meant to put element name in quotes. PHP will still generate an error in that case, but then try to fix it for you. Evidently it won't do that inside a mysql_query(), however--but I've never tried it.

Now, however, I'm also worried about how MySQL will handle all the single quotes in the query. I think you may also have to do some concatenation, like:

$insert = mysql_query("INSERT INTO closed_players (username, amount, forfeit) VALUES ('".$_POST['username'][$i]."', '".$_POST['amount'][$i]."', '".$_POST['forfeit'][$i]."')");

Either that, or put your values into other variables just before your INSERT, like:

$user = $_POST['username'][$i];

...then use $user in the insert rather than the value from the array, e.g.:

...VALUES ('$user',...

I hope this helps.

MATT_ta_Tat

5:11 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



That worked. Thank you very much everyone who helped me out with this.

coopster

6:28 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's not the
mysql_query()
function, Salsa, it is how PHP interprets arrays. It would actually be acceptable to not put quotation marks around the keys within the brackets in this case since the entire sql statement itself is a string (enclosed in double-quotes) and constants are not looked for within strings. However, that can get confusing, so I tend to use the braces syntax (or concatenate as you have shown).

More information on how this works is found in the manual under Array do's and don'ts [php.net].