Forum Moderators: coopster
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.
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.
$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
<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. :)
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?
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...
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.
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].