0

My site contains several forms for users to fill. One of those forms could result in a lot of work. That´s why I´m reaching out for consent. You see, in this form, there will be 40 games. And every game gives the users 2 fields to enter a number (depending on how they think the game will end). So with 40-50 games it means that I have 80-90 fields to keep track of. The two fields will be paired with the right game in the database, and the users answer will be remembered at all time. This is a snap of my code:

<div class="form-left">England- Italy</div>
                <div class="form-right"><input type="result" id="g1r1" name="g1r1" class="form-input" /></div>
                <div class="form-dash">-</div>
                <div class="form-right"><input type="result" id="g1r2" name="g1r2" class="form-input" /></div>
                <div class="form-error"></div>

<div class="form-left">Spain- Mexico</div>
                <div class="form-right"><input type="result" id="g2r1" name="g2r1" class="form-input" /></div>
                <div class="form-dash">-</div>
                <div class="form-right"><input type="result" id="g2r2" name="g2r2" class="form-input" /></div>
                <div class="form-error"></div>

My thought is, naming the games game1row1 and game1row2. To be able to match them to the correct game in the database. Problem is that an insertion to a table with 90 fields seems hugh. So my question would be: How can this be done in a better way? With less lines of code on the insert-database side..

  insert into tablename ( g1r1, g1r2 values 1, 0 where MATCH_ID = MATCHID)

EDIT

To clarify, My table contains ID, user_id, match_id(g), homeScore(r1), awayScore(r2). My idea of g1r1 and g1r2 is to save r1 and r2 at right g(game).

2
  • You can dynamically make the insert query. I don't know how else you would do it. What do you have so far? Commented Aug 29, 2013 at 15:59
  • I don't have much more than making 40 insert querys, but I was thinking something in line with an array to store matchers och rows, but then again I have no idea, thats why im asking. What do you mean by dynamically? Commented Aug 29, 2013 at 16:01

3 Answers 3

2

Problem is that an insertion to a table with 90 fields seems hug[e]

your user table should be seperate from your games table and a third table for scores I think is how you are using this. That way none of the tables will ever have 90 columns. This will also allow a design that allows database growth

What I think your current design is:

usser_id | g1r1 | g1r2 | g2r1 | g2r2 | g3r1 | g3r2 | ... | g50r1 | g50r2

What your design should be like

Users:
user_id | ..[user info]...

Games:
game_id | ...[game info]...

Scores:
score_id | game_id | user_id | score_1 | score_2

And by all means also follow @bart2puck with his recommendation of writing html dynamically with php. This will make life so much easier. After changing database design, you will be able to insert the scores like:

$insert = "INSERT INTO Scores (user_id,game_id,homeScore,awayScore) VALUES ";
foreach ($_POST['games'] as $key => $value){
    $insert = "(".$_POST["user_id"].",".$key.",".$value[0].",".$value[1].")";
}
//run query insert

That is if you name your input fields according to name="games[game_id[]]" where game_id is equivalent to your database game_id for instance for game 1 : name="game[1[]]" for both of the score fields for that game.

IMPORTANT!!

Please make sure that you either use query parameters or mysql escape your post input before running query! That way you will be protected from Bobby Tables AKA SQL Injection

EDIT:

From the OP comment with table names you may want to use

$insert = "INSERT INTO Scores (user_id, game_id, homeScore, awayScore) VALUES ";
foreach ($_POST["homeScore"] as $key => $value){
    $insert = "(".$_POST["user_id"].",".$key.",".$value.",".$_POST["awayScore"][$key].")";
}
//run query insert

Where you then name your form fileds homeScore[game_id] and awayScore[game_id] so the home score field of the game with a game id of 1 would be name="homeScore[1]"

EDIT 2:

Here is also a better way to write your html using php with the correct form names if you follow my last edit.

for ($i = 1;$i<=40;$i++){
   echo "<div class='form-right'><input type='result' id='homeScore[$i]' name='awayScore[$i]' class='form-input' /></div>\n";
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I accually have a Scores table -> id, user_id, game_id, homeScore, awayScore. But the insertion part looks promising, just gonna try it and then I leave my verdict, thanks :)
@Rocksteady Ok from your question and 90 fields, I thought you had a table with 90 columns.. anyways I hope that it helps. and remember to watch out for Bobby Tables
@Rocksteady please see my edited answer, As I believe it will help.
user id wont be posted, I can always get that, but the most importand is that G1 gets placed at match_id and r1 and r2 is matched to right match_id. Im setting up a test for you code right now
You have found a good way here, the problem now is that ".$_POST["awayScore"][$key]." does not display the user input, it displays the match_id...
1

1) you could write your page using a loop and make the form names an array

 echo "<form action='$PHP_SELF' method='POST'>";
 for ($b = 1;$b<=40;$b++)
 {
   $gameId = "g" . $b;
   for ($a = 1;$a<=2;$a++)
   {
     $roundId = "r" . $a;
     $fieldName = $gameId . $roundId;

      echo "<div class='form-right'><input type='result' id='games[$fieldName]' name='games[$fieldName]' class='form-input' /></div>\n";
   }
}
 echo "<input type='submit' value='submit'>";

the post result would give you something like:

[games] => Array
    (
        [g1r1] => a
        [g1r2] => b
        [g2r1] => c
        [g2r2] => d
        [g3r1] => e
        [g3r2] => f
        [g4r1] => g
        [g4r2] => h
        [g5r1] => i
        [g5r2] => j
        [g6r1] => k
        [g6r2] => l
        [g7r1] => m
        [g7r2] => n
        [g8r1] => o
        [g8r2] => p
        [g9r1] => q
        [g9r2] => r
        [g10r1] => s
        [g10r2] => t
    )

and in your processing page:

 foreach ($_POST['games'] as $key => $value)
 {
    $insert = "INSERT into games (" . $key . ") values ('" . $value. "')";
   //or whatever query you need here.
 } 

1 Comment

@Piero This way he can write 80 lines of a form in 10 lines with the php loop.
1

bart2puck, what's the reason of outputting html with a php loop? Regular html would work just fine there, helping save bits of server resources. Form fields just should be named appropriately like so:

<input type="text" name="games[g1r1]" value="g1r1" />

Then in php get not just $_POST but $_POST['games'] and you have the array of all form fields.

Then you could create your query like so:

$query = 'INSERT INTO `tablename`(`'.implode('`, `', array_keys($_POST['games'])).'`) VALUES("'.implode('", "', $_POST['games']).'")';

Just one liner.

Remember to clean all your data before saving to database or your application might be vulnerable to SQL injections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.