Forum Moderators: coopster

Message Too Old, No Replies

Help with submitting multiple entries

         

dwighty

11:44 am on Jun 5, 2006 (gmt 0)

10+ Year Member



Hey Guys,

Am looking for some urgent help with submitting multiple entries from a form

The main problem being that the form entries are looped but the following code should explain things a bit better (hopefully).

*******************************CODE******************************
function groupAPredictions() {

/* Get Match Info */
global $db, $prefix;
$sql = "SELECT Date, time, venue, stage, hometeam, awayteam FROM matches WHERE stage='Group A' ORDER BY Date ASC";
$result = $db->sql_query($sql);

echo '
while ($row = $db->sql_fetchrow($result)) {

$Date = $row['Date'];
$time = $row['time'];
$venue = $row['venue'];
$stage = $row['stage'];
$hometeam = $row['hometeam'];
$awayteam = $row['awayteam'];

echo '<form action="'.getlink('WC_Predictions').'" method="post" enctype="multipart/form-data" accept-charset="utf-8">';
echo '
<input type="hidden" size="20" name="username" value="'.$userinfo[username].'" />
<input type="hidden" size="20" name="Date" value="'.$row[Date].'" />
<input type="hidden" size="20" name="time" value="'.$row[time].'" />
<input type="hidden" size="20" name="venue" value="'.$row[venue].'" />
<input type="hidden" size="20" name="stage" value="'.$row[stage].'" />
<input type="hidden" size="20" name="hometeam" value="'.$row[hometeam].'" />
<input type="hidden" size="20" name="awayteam" value="'.$row[awayteam].'" />
<tr height="25" align="center">
<td>'.$row['Date'].'</td>
<td>'.$row['time'].'</td>
<td>'.$row['venue'].'</td>
<td>'.$row['hometeam'].'</td>
<td>
<select name="homeresult">
<option selected="selected" label="none" value="0">0</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>verses</td>
<td>
<select name="awayresult">
<option selected="selected" label="none" value="0">0</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>'.$row['awayteam'].'</td>
</tr>
';

}
echo '<input type="submit" value="Enter" name="groupAPredictions" /></form>';

}

/* When the user submits form */
if (isset($_POST['groupAPredictions'])) {
entergroupAPredictions();
}

/* Functions for Post Submited */
function entergroupAPredictions () {
global $db, $prefix, $userinfo;

$Date = $_POST['Date'];
$time = $_POST['time'];
$venue = $_POST['venue'];
$stage = $_POST['stage'];
$hometeam = $_POST['hometeam'];
$homeresult = $_POST['homeresult'];
$awayresult = $_POST['awayresult'];
$awayteam = $_POST['awayteam'];

$db->sql_query("INSERT INTO "predictions (username, Date, time, venue, stage, hometeam, homeresult, awayresult, awayteam) VALUES ('".$userinfo[username]."', '$Date', '$time', '$venue', '$stage', '$hometeam', '$homeresult', '$awayresult', '$awayteam')");

}
*******************END OF CODE*******************

Any help would be much appreciated.

Thanks

Dwighty

dreamcatcher

12:05 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the following code should explain things a bit better

Its got me beat dwighty, what are you trying to do exactly?

dc

dwighty

12:14 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



dreamcatcher,

Thanks for your reply.

Lets try and make it clearer...

I have a db table which stores actualy football match details such as the date, time, teams, results etc.

Our Members then log on and predict what the scores will be of those games prior to them taking place.

so the first instance, the sql selects all games which refer to group A (as we only want the members to predict 1 group at a time).

To display this information i placed in in a loop
***************CODE******************
while ($row = $db->sql_fetchrow($result)) {

$Date = $row['Date'];
$time = $row['time'];
$venue = $row['venue'];
$stage = $row['stage'];
$hometeam = $row['hometeam'];
$awayteam = $row['awayteam']
*************************************
then echoed it...
In the echo I added homeresult and awayresult as select boxes as this is what the members changes to enter their predictions.

Once submitted I want the details from each match to be entered into a new db table.

Does that help?

dreamcatcher

1:02 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dwighty, sorry, now I can see. Too much caffeine. :)

So, what happens? Do you get errors? Does the query fail?

dc

dwighty

1:08 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



no errors, it only submits the last match details into the db.

e.g. the loop may produce something like 4 lines
each with different match details.

when the form is submitted it will only register the last match row on the form to the database.

Sorry, dont think i mentioned the problem in the earlier messages.

Thanks

dreamcatcher

2:00 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, now we are getting somewhere.

Right, firstly, you need to create some arrays in your forms. You may be doing this already, but lets start there.

So, the form needs to be like this:


echo '
<input type="hidden" size="20" name="username" value="'.$userinfo[username].'" />
<input type="hidden" size="20" name="Date[]" value="'.$row[Date].'" />
<input type="hidden" size="20" name="time[]" value="'.$row[time].'" />
<input type="hidden" size="20" name="venue[]" value="'.$row[venue].'" />
<input type="hidden" size="20" name="stage[]" value="'.$row[stage].'" />
<input type="hidden" size="20" name="hometeam[]" value="'.$row[hometeam].'" />
<input type="hidden" size="20" name="awayteam[]" value="'.$row[awayteam].'" />
<tr height="25" align="center">
<td>'.$row['Date'].'</td>
<td>'.$row['time'].'</td>
<td>'.$row['venue'].'</td>
<td>'.$row['hometeam'].'</td>
<td>
<select name="homeresult[]">
<option selected="selected" label="none" value="0">0</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>verses</td>
<td>
<select name="awayresult[]">
<option selected="selected" label="none" value="0">0</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>'.$row['awayteam'].'</td>
</tr>
';

Notice I have added square brackets after each name. This will create arrays of each field. Next change your entergroupAPredictions () function to this:


function entergroupAPredictions ($Date, $time, $venue, $stage,$hometeam, $homeresult, $awayresult, $awayteam, $username) {
global $db, $prefix;
$db->sql_query("INSERT INTO "predictions (username, Date, time, venue, stage, hometeam, homeresult, awayresult, awayteam) VALUES ('$username', '$Date', '$time', '$venue', '$stage', '$hometeam', '$homeresult', '$awayresult', '$awayteam')");
}

And finally, your processing if statement from this:


if (isset($_POST['groupAPredictions'])) {
entergroupAPredictions();
}

to this:


if (isset($_POST['Date'])) {

for ($i=0; $i<count($_POST['Date']); $i++)
{
entergroupAPredictions ($_POST['Date'][$i], $_POST['time'][$i], $_POST['venue'][$i], $_POST['stage'][$i], $_POST['hometeam'][$i], $_POST['homeresult'][$i], $_POST['awayresult'][$i], $_POST['awayteam'][$i], $_POST['username'][$i])
}

}

So, you are assigning each field as an array and then looping through them accessing each indice seperately in the for loop.

Give that a try.

dc

dwighty

6:51 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



dreamcatcher,

Thanks for that, works exactly like what I was after.

This has prompted a further question...

if (isset($_POST['Date'])) {
for ($i=0; $i<count($_POST['Date']); $i++)
{
entergroupAPredictions ($_POST['Date'][$i], $_POST['time'][$i], $_POST['venue'][$i], $_POST['stage'][$i], $_POST['hometeam'][$i], $_POST['homeresult'][$i], $_POST['awayresult'][$i], $_POST['awayteam'][$i], $_POST['username'][$i]);
}
}

Why if (isset($_POST['Date']))? and not

**********************
if (isset($_POST['entergroupAPrediction'])) {
for ($i=0; $i<count($_POST['Date']); $i++)
{
entergroupAPredictions ($_POST['Date'][$i], $_POST['time'][$i], $_POST['venue'][$i], $_POST['stage'][$i], $_POST['hometeam'][$i], $_POST['homeresult'][$i], $_POST['awayresult'][$i], $_POST['awayteam'][$i], $_POST['username'][$i]);
}
}

dreamcatcher

7:06 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad it worked ok.

I couldn`t see where you had assigned the 'entergroupAPrediction' variable, so I changed it just in case. Doesn`t matter as long as it works ok.

dc

dwighty

8:02 am on Jun 7, 2006 (gmt 0)

10+ Year Member



dreamcatcher,

I have run into another issue with this.

For submitting for the first time this works great.

If the user then wants to update each record rather than the whole array is where I am having a few problems.
************************CODE*********************
if ($result > 0 ) {

while ($row = $db->sql_fetchrow($result)){

$id = $row['id']; //id relates to the match id once the form has originally been submitted
$Date = $row['Date'];
$time = $row['time'];
$venue = $row['venue'];
$stage = $row['stage'];
$hometeam = $row['hometeam'];
$homeresult = $row['homeresult'];
$awayresult = $row['awayresult'];
$awayteam = $row['awayteam'];

echo '
<form action="'.getlink('Test_Predictions').'" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<input type="hidden" size="20" name="username" value="'.$userinfo[username].'" />
<input type="hidden" size="20" name="id" value="'.$row[id].'" />
<tr height="25" align="center">
<td>'.$row['Date'].'</td>
<td>'.$row['time'].'</td>
<td>'.$row['venue'].'</td>
<td>'.$row['hometeam'].'</td>
<td>
<select name="homeresult">
<option selected="selected" label="none" value="'.$row['homeresult'].'">'.$row['homeresult'].'</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>verses</td>
<td>
<select name="awayresult">
<option selected="selected" label="none" value="'.$row['awayresult'].'">'.$row['awayresult'].'</option>
<option label="" value="1">1</option>
<option label="" value="2">2</option>
<option label="" value="3">3</option>
</select>
</td>
<td>'.$row['awayteam'].'</td>
<td><input type="submit" value="Update" name="updategroupPredictions" /></td>
</tr>
';
}

/* When form is submitted it goes to */
// If User Updates Group Predictions
if (isset($_POST['updateAdditionalPredictions'])) {
updategroupPredictions();
}

/* updategroupPredictions() function is */
function updategroupPredictions () {
global $db, $prefix, $userinfo;

$id = ($_POST['id']);
$homeresult = ($_POST['homeresult']);
$awayresult = ($_POST['awayresult']);

$db->sql_query("UPDATE ".$prefix."_test_predictions SET homeresult='$homeresult', awayresult='$awayresult' WHERE id='$id'");

}

The problem is that when a match prediction is updated, it does not update the record in the db table instead it enters scores 0 for home and away results for another game not yet entered. I have checked the id and it echos the correct result in the form.

Is this not working because of the other function for entering predictions and the array?

Cheers

dwighty

8:10 am on Jun 7, 2006 (gmt 0)

10+ Year Member



Right,

I have resolved the issue with entering random 0-0 scores for other games, forgot to end the <form> D'oh.

Problem is i cant get the individual games to be updated.

Thanks in advance

dreamcatcher

2:33 pm on Jun 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Out of curiosity, where is this variable defined?

$_POST['updateAdditionalPredictions']

Should it actually be:

$_POST['updategroupPredictions']

?

Also, set your error_reporting to E_ALL for testing:

error_reporting(E_ALL);

dc

dwighty

5:45 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



I dont believe it, what a miss!

Thanks Dreamcatcher.

I normally do display errors but have been working on a different system to normal so didnt get round to changing it yet.

Thanks again.

dreamcatcher

5:56 pm on Jun 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anytime dwighty. Glad I could help.

dc