0

I am busy working on a university assignment which must do the following:

  • Show list of sporting events for week
  • User Submits the teams they think will win
  • At end of week, calculate which users had the most correct picks

In working on above I have created a form like this, which lets the user select who they think will win.

enter image description here

The form data is collected from a database called events which looks like this:

enter image description here

My form is generate with the following code

   //create form
    echo'<form name="" action="" method="post">';
    echo'<fieldset>';
    echo'<legend>Make Your Picks</legend>';

//create query
$sql = "Select * FROM events";
$result = mysql_query($sql);

while($row=mysql_fetch_array($result)){
    $team1 = $row['team1'];
    $team2 = $row['team2'];

    //dislay teams
    echo'<h3>'.$team1.' VS '.$team2.'</h3>';
    echo'<select name="'.$row['event_id'].'">';
            echo'<option value="'.$row['team1'].'">'.$team1.'</option>';
            echo'<option value="'.$row['team2'].'">'.$team2.'</option>';
            echo'</select>';    
}//while

echo'</legend></fieldset>';
echo'<input type="submit" class="buttono" value="Submit" name="submit" />';
echo'</form>'

I would like to get a more experienced users opinion on my logic and perhaps a tip if I am on the correct path and what I could be doing better, in terms of implementation,since I have ZERO confidence in my current approach.

  1. I am creating multiple selectboxes inside the while loop, each select box has a unique name of the event_id value from db which will get inserted into the picks database (see below), this doesn't seem very efficient is there an alternative way I can approach this?

enter image description here

  1. Am I correct in assigning the name attribute of the selctbox the value of the fixture_id?

  2. Is there perhaps a more efficient way I can approach this problem that you can suggest?

Just thought it would be interesting getting an experienced users view on this

Marilee
  • 1,598
  • 4
  • 22
  • 52
  • 4
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 16 '15 at 20:17
  • 1
    use an array for your `name` variable like `name="event[7]"` then in php you can do a `foreach($_POST['event'] as $event_id => $winner){ // do something here }` – cmorrissey Apr 16 '15 at 20:20
  • 2
    @cmorrissey Post that as an answer. – Barmar Apr 16 '15 at 20:20
  • @JayBlanchard i know its out dated but what is the reason i should stop using it...if it aint broken dont fixit right – Marilee Apr 17 '15 at 04:01
  • Some day soon @Marilee someone is going to update your server and when they do PHP will be upgraded along with it. When that happens all of the functions using the `mysql_*` API will quit working and start throwing errors. Better to transition while you have control over the situation. – Jay Blanchard Apr 17 '15 at 11:45

1 Answers1

5

use an array for your name variable like name="event[7]" which would look like

echo'<select name="event['.$row['event_id'].']">';

then when you are gathering data you run a loop like

foreach($_POST['event'] as $event_id => $winner){ 
// do something here 
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27