January 7, 200917 yr Hi I am trying to enter results from club comp's into mysql using php, something like this: player A result - listing 4 fields player B result - listing 4 fields etc Having set up a form for secretaries to log into I can enter one entry ok but do not know how to set up a form to do multiple entries. Any help much appreciated. Ray
January 8, 200917 yr Author Can you post your code so we can take a look at what you've got? This is what I am using for sungle entries: <!-- USED WITH FETCHRESULTS.PHP TO ENTER PLAYER RESULTS INTO BOWLS.DB - RESULTS TABLE--> <html><body> <?php include 'opendb.php'; // THIS WORKS FOR SINGLE ENTRIES BUT NOT FOR MULTI ENTRIES $id=$_POST['id']; $shots_for=$_POST['shots_for']; $shots_against=$_POST['shots_against']; $points=$_POST['points']; mysql_connect(localhost,$root,$******); @mysql_select_db("bowls") or die( "Unable to select database"); $query = "INSERT INTO results VALUES ('$id','$shots_for','$shots_against','$points')"; mysql_query($query); if($result = mysql_query($query,$results)) { echo "<h3>Thank you</h3>Your information has been entered into our database<br><br>"; } mysql_close(); ?> </body></html> Thanks for your reply Ray
January 8, 200917 yr Simple: Insert each row of your data into an array by placing parentheses at the end of the variable like this: $query[] = $id,$shots_for,$shots_against,$points; (You can place the quotations inside your variables if the values are strings.) then use a foreach loop to process the array as a query foreach ($query as $q) {$q = "INSERT INTO results VALUES ($q); $r = mysqli_query($connect,$q); Haven't tested it , but that should do it (I've used the same thing in my own code.
January 8, 200917 yr Author Simple: Insert each row of your data into an array by placing parentheses at the end of the variable like this: $query[] = $id,$shots_for,$shots_against,$points; (You can place the quotations inside your variables if the values are strings.) then use a foreach loop to process the array as a query foreach ($query as $q) {$q = "INSERT INTO results VALUES ($q); $r = mysqli_query($connect,$q); Haven't tested it , but that should do it (I've used the same thing in my own code. I am sorry if I seem a bit dim but I am just learning mysql/php. Do you mean use this code you printed instead of mine or incorporate it into mine some-how. Regards Ray
January 8, 200917 yr Ah - no, it was more a demonstation of the principle. How are you retrieving the results? Can you post your form code?
January 8, 200917 yr Author Ah - no, it was more a demonstation of the principle. How are you retrieving the results? Can you post your form code? Just using a simple form within html. <form action="proresults.php" method="post"> id: <input type="text" name="id"><br> Shots For: <input type="text" name="shots_for"><br> Shots Against: <input type="text" name="shots_against"><br> Points: <input type="text" name="points"><br> id: <input type="text" name="id"><br> Shots For: <input type="text" name="shots_for"><br> Shots Against: <input type="text" name="shots_against"><br> Points: <input type="text" name="points"><br> <input type="Submit"> Trying it out with 2 entries, if that works I can add the full team results in later. If I only set it up for 1 entry it works fine. Ray
January 8, 200917 yr okaaaaay: With that form, you are overwriting your $_POST variables by having them twice. I'm assuming this is a record of a match between two folks - in which case you need to indicate that separation in your input names - i.e: <form action="proresults.php" method="post"> id: <input type="text" name="id_1"><br /> Shots For: <input type="text" name="shots_for_1"><br /> Shots Against: <input type="text" name="shots_against_1"><br /> Points: <input type="text" name="points_1"><br /> id: <input type="text" name="id_2"><br /> Shots For: <input type="text" name="shots_for_2"><br /> Shots Against: <input type="text" name="shots_against_2"><br /> Points: <input type="text" name="points_2"><br /> <input type="Submit"> Then, you can separate the variable into two sets: <?php //player 1 $id_1 = $_POST['id_1']; $shots_for_1 = $_POST['shots_for_1']; $shots_against_1 = $_POST['shots_against_1']; $points_1 = $_POST['points_1']; //player 2 $id_2 = $_POST['id_2']; $shots_for_2 = $_POST['shots_for_2']; $shots_against_2 = $_POST['shots_against_2']; $points_2 = $_POST['points_2']; ?> Then you can add these to your DB using the following query $q_player_a = "INSERT INTO database VALUES ($id_1,$shots_for_1,$shots_against_1,$points_1)"; $r_player_a = mysqli_query($connection,$q_player_a); $q_player_b = "INSERT INTO database VALUES ($id_2,$shots_for_2,$shots_against_2,$points_2)"; $r_player_b = mysqli_query($connection,$q_player_b); This could be done more elegantly with an array, but since I don't know the structure of your tables it's hard to know how to construct the query. Are the player records already on file, are they given a unique id as their results are added to the database? If they exist already, then you would need to use an UPDATE query, and if they don't - then I imagine you need to add a unique identifier column in your database.
January 9, 200917 yr Author okaaaaay:With that form, you are overwriting your $_POST variables by having them twice. I'm assuming this is a record of a match between two folks - in which case you need to indicate that separation in your input names - i.e: <form action="proresults.php" method="post"> id: <input type="text" name="id_1"><br /> Shots For: <input type="text" name="shots_for_1"><br /> Shots Against: <input type="text" name="shots_against_1"><br /> Points: <input type="text" name="points_1"><br /> id: <input type="text" name="id_2"><br /> Shots For: <input type="text" name="shots_for_2"><br /> Shots Against: <input type="text" name="shots_against_2"><br /> Points: <input type="text" name="points_2"><br /> <input type="Submit"> Then, you can separate the variable into two sets: <?php //player 1 $id_1 = $_POST['id_1']; $shots_for_1 = $_POST['shots_for_1']; $shots_against_1 = $_POST['shots_against_1']; $points_1 = $_POST['points_1']; //player 2 $id_2 = $_POST['id_2']; $shots_for_2 = $_POST['shots_for_2']; $shots_against_2 = $_POST['shots_against_2']; $points_2 = $_POST['points_2']; ?> Then you can add these to your DB using the following query $q_player_a = "INSERT INTO database VALUES ($id_1,$shots_for_1,$shots_against_1,$points_1)"; $r_player_a = mysqli_query($connection,$q_player_a); $q_player_b = "INSERT INTO database VALUES ($id_2,$shots_for_2,$shots_against_2,$points_2)"; $r_player_b = mysqli_query($connection,$q_player_b); This could be done more elegantly with an array, but since I don't know the structure of your tables it's hard to know how to construct the query. Are the player records already on file, are they given a unique id as their results are added to the database? If they exist already, then you would need to use an UPDATE query, and if they don't - then I imagine you need to add a unique identifier column in your database. We have 10 teams with 8 players in each team, each team and each player have unique numbers within the database spread over several tables which I can join to bring back league tables and individual averages. I started this back in October as a winter exercise to see if I could learn php and mysql and build a web site for the league I play in. (all done for free of course). I seem to making some progress using css/html/apache/mysql/php and with the very generous help of you very kind people on this forum. I have put the players details in one table, results in another table, league details in another table and join them up when needed. this makes it easy to put results in by using the unique id which is the same in each table. Sorry for the long speel but I thought it might help if you knew where I was coming from. Ray
Create an account or sign in to comment