backinblack Posted March 4, 2007 Share Posted March 4, 2007 I am having a little dificult figuring this out. Hope some on cane help. Still pretty new here. I am doing a website for a race track and what I am trying to do is when I get the results all the info goes to the database. The form is like this: It has Car#, Drives Name, Points. 1. #50 Bob Smith 50pts 2. #20 John Doe 48pts and so on up 25 fields, but when I send the it, it will only show the last one. It will not send all of them. I want to have all the car numbers in the car numbers field in the database with the drivers name and points the same way. This is probably simple but I am drawing a blank. Can any one help? Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 4, 2007 Share Posted March 4, 2007 Hard to tell what the problem is without seeing any code at all. It sounds as though you have a number of form fields all with the same name ... Quote Link to comment Share on other sites More sharing options...
play_ Posted March 4, 2007 Share Posted March 4, 2007 What query are you using? It should be something like: INSERT INTO table_name (car_number, driver_name, points) VALUES ('$car_number', '$driver_name', '$points') Quote Link to comment Share on other sites More sharing options...
hukadeeze Posted March 4, 2007 Share Posted March 4, 2007 Are you entering one car and driver every time you submit the form, or do you have a form that is 25 rows long that you submit only once? Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 Form I just put a couple of the fields to keep it short. Only submit the form once and it just puts the last one in the database. <form action="ptsprocess.php" method="post"> <table width="30%"><tr><td width="5%"><b>Car #:</b> </td><td width="15%"><b>Name:</b></td> <td width="5%"><b>Points:</b></td></tr> <td><input type="text" name="carnumber"></td><td><input type="text" name="name"></td><td><input type="text" name="points"></td></tr> <tr><td><input type="text" name="carnumber"></td><td><input type="text" name="name"></td><td><input type="text" name="points"></td></tr> <tr><td><input type="submit" value="Send Results"></td></tr> </table> </form> <?php $carnumber = $_POST['carnumber']; $name = $_POST['name']; $points = $_POST['points']; $host = "XXXXXXX"; $user = "XXXXXXX"; $password = XXXXXXX"; $database = "XXXXXXX"; $connection = mysql_connect($host, $user, $password) or die ("Could not retrieve information"); $db = mysql_select_db("databasename",$connection) or die ("Could not make connection"); $query = "INSERT INTO points (carnumber, name, points)" . "VALUES ('$carnumber','$name','$points')"; $results=mysql_query($query) or die (mysql_error()); if ($results); echo 'Result have been posted into database!'; ?> AndyB-- You are correct. That is what is messing things up I think. Trying to figure out the correct way to do this. Quote Link to comment Share on other sites More sharing options...
hukadeeze Posted March 4, 2007 Share Posted March 4, 2007 What are you using as your primary key? If your using an auto incrementing column, like pointsID, I believe you have to include that in your query string, using 'null' as its value. Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 I have driver_id as my primary key set to auto increment but did not have it set to null. Changed that and put the driver_id in the query but still does the same thing. Quote Link to comment Share on other sites More sharing options...
play_ Posted March 4, 2007 Share Posted March 4, 2007 $query = "INSERT INTO points (carnumber, name, points) VALUES ('$carnumber','$name','$points')"; try that Quote Link to comment Share on other sites More sharing options...
hukadeeze Posted March 4, 2007 Share Posted March 4, 2007 that wont work INSERT INTO points (driver_id, carnumber, name, points) VALUES (NULL, "$carnumber", "$name", "$points") Quote Link to comment Share on other sites More sharing options...
play_ Posted March 4, 2007 Share Posted March 4, 2007 I know andy's mentioned this and you said you've looked over it, but two of your input fields are named "carnumber" Quote Link to comment Share on other sites More sharing options...
play_ Posted March 4, 2007 Share Posted March 4, 2007 that wont work INSERT INTO points (driver_id, carnumber, name, points) VALUES (NULL, "$carnumber", "$name", "$points") if driver_id is a primary key with auto_increment, you don't need to send NULL to it. Quote Link to comment Share on other sites More sharing options...
hukadeeze Posted March 4, 2007 Share Posted March 4, 2007 hahaha, seriously? I've always done it, I never thought to try doing it without it. Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 4, 2007 Share Posted March 4, 2007 The example form you posted has the same name for different inputs. You need to have every form input NAMEd differently. For your case, name them as an array .... input type="text" name="carnumber[]" .. etc. and then you can retrieve the values of the carnumber[] array. Handle the other form variables the same way. Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 I am pretty new to this so please bare with me. I have renamed all the form values. For instance... carnumber[0] name[1] points[3] on the first row of the form input fields and carnumber[4] name[5] points[6] on the second row of fields. For the array can I just name it anything like this $carnumber = array('carnumber', 'name', 'points') Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 Ok I think I am getting closer. What I have is the six form fields I have carnumber[] name[] points[] etc... then to process it I have $query = "INSERT INTO points values ('$carnumber[0]', '$name[1]', '$april15pts[2]'), ('$carnumber[3]', '$name[3]', '$april15pts[4]')"; But by doing it this way I get an error saying: Column count doesn't match value count at row 1 From all the different turtorials that I have read, it looks like there are several ways to do this and I can seem to figure it out. Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted March 4, 2007 Share Posted March 4, 2007 what are the names of your rows in your db tables Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted March 4, 2007 Share Posted March 4, 2007 This is what I came up for you...this will allow you to submit your records to your db one at a time. So basically you can't add all 25 at once. <form action="ptsprocess.php" method="post"> <table width="30%"> <tr> <td width="5%"> <b>Car #:</b> </td> <td width="15%"> <b>Name:</b></td> <td width="5%"> <b>Points:</b> </td> </tr> <tr> <td> <input type="text" name="carnumber"> </td> <td> <input type="text" name="name"> </td> <td> <input type="text" name="points"> </td> </tr> <tr> <td> <input type="submit" value="Send Results"> </td> </tr> </table> </form> <?php $host = "XXXXXXX"; $user = "XXXXXXX"; $password = XXXXXXX"; $database = "XXXXXXX"; mysql_connect($host,$user,$password); @mysql_select_db($database) or die( "Unable to select database"); $carnumber = $_POST['carnumber']; $name = $_POST['name']; $points = $_POST['points']; $query = "INSERT INTO points VALUES ('''$carnumber','$name','$points')"; $results=mysql_query($query) or die (mysql_error()); if ($results); echo 'Result have been posted into database!'; ?> Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 That is what I already had . I am trying to avoid that if I can. I am pretty sure there is a way. Just trying to figure out how. My rows in my table is driver_id which is the primary key set to auto increment and notnull carnumber name points Quote Link to comment Share on other sites More sharing options...
backinblack Posted March 4, 2007 Author Share Posted March 4, 2007 Whew! Got it figured out. Thank you all for the help. AndyB you put me on the right track. Usually when people start throwing out ideas, the light bulb will usually come on for me and can figure it out myself. My next problem though is each week of course the points will be different and when I do that it duplicates the drivers car number and name. If I get stuck I will hit you guys up for more info. THANKS AGAIN!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.