January 19, 200917 yr I am trying to enter several records from one form but all I get is the last record entered, I am hoping some-one can see my errors, just learning php so please be gentle with me. <form action="process2.php" method="post"> Number: <input type= "text" name="number" size='3'/> Shots Scores: <input type= "text" name="shots_for" size='3'/> Shots Against: <input type= "text" name="shots_against" size='3'/> Points : <input type= "text" name="points" size='3'/><br /><br /> Number: <input type= "text" name="number" size='3'/> Shots Scores: <input type= "text" name="shots_for" size='3'/> Shots Against: <input type= "text" name="shots_against" size='3'/> Points : <input type="text" name="points" size='3'/><br /> <input name="submit" type="submit" value="submit" /> </form> <?php $number=$_POST['number']; $shots_for=$_POST['shots_for']; $shots_against=$_POST['shots_against']; $points=$_POST['points']; mysql_connect("localhost", "root", "*******") or die(mysql_error('Could not connect to mysql')); mysql_select_db("bowls") or die(mysql_error('could not connect to db')); mysql_query("INSERT INTO results VALUES ('$number', '$shots_for', '$shots_against', '$points')"); mysql_query($query); $num=mysql_numrows($query); $i=0; while ($i<$num) { $query="('$i.$_GET[number]','$i.$_GET[shots_for]','$i.$_GET[shots_against]','$i.$_GET[points]')"; $i++; } $mysql_query=substr($mysql_query,0,-1); //This will remove last comma Print "Your information has been successfully added to the database."; ?> Any help much appreciated Ray Edited February 7, 201016 yr by Sam G added [code] tags
January 19, 200917 yr your input names are the same for each set of records, so the first set will get overwritten by the second. They should have a different value appended to the end, like "shots_against_a". Still struggling with this one eh? Also, why are you using $_GET when your form uses $_POST?
January 19, 200917 yr Author your input names are the same for each set of records, so the first set will get overwritten by the second. They should have a different value appended to the end, like "shots_against_a". Still struggling with this one eh? Also, why are you using $_GET when your form uses $_POST? Thanks for your reply Yes still struggling - but learning a lot along the way - the GET came from trying different things, forgot to change it back, done that now. If I change the input names for the records after the first one how will the info show in the relevant fields, ie, number field will be different to number_a field, or is my thinking wrong. I could enter a list of results directly into mysql using a comma, I am trying to replicate that through php, am I trying the impossible. ? Ray
January 19, 200917 yr The problem you have is twofold: if you are recording the results for two players, you need to let the script know the results are different, in both the form construction and the $_POST retrieval: <input type='text' name='input_a'> <input type='text' name='input_b'> $input_a = $_POST['input_a']; $input_b = $_POST['input_b']; Then you need to enter these values through a MySQL query. You can execute multiple queries with one call in PHP5+, or as one string i.e: INSERT INTO tablename (fields) VALUES (values),(values) but I recommend you just make two separate queries (populated with the two groups of results) until you're more comfortable with PHP.
January 20, 200917 yr Author The problem you have is twofold: if you are recording the results for two players, you need to let the script know the results are different, in both the form construction and the $_POST retrieval: <input type='text' name='input_a'> <input type='text' name='input_b'> $input_a = $_POST['input_a']; $input_b = $_POST['input_b']; Then you need to enter these values through a MySQL query. You can execute multiple queries with one call in PHP5+, or as one string i.e: INSERT INTO tablename (fields) VALUES (values),(values) but I recommend you just make two separate queries (populated with the two groups of results) until you're more comfortable with PHP. I'll give that a try and I can see the sense in taking it slowly but this is a two edged sword, my main target has been to learn php-mysql and I am using our bowls club to learn with, it would be nice to end up being able to service a web site for our bowls league. (impatients I'm afraid). Thanks again Ray
Create an account or sign in to comment