Woodburn2006 Posted November 20, 2007 Share Posted November 20, 2007 i have this form: http://www.upks.co.uk/admincontrol/index2.php?go=results_add and i need to add each row on the form to a seperate row in a sql table. so there will be a row in the sql table for 1st place, a row for 2nd place, a row for 3rd place etc etc. what is the best way of doing this without setting up seperate sql queries for each of the 20 rows? Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted November 20, 2007 Share Posted November 20, 2007 As far as I know your best option is a foreach loop through the $_POST super global. Only make 1 connection to your database then just run your query over and over again. <?php $postarr = $_POST $link_id = mysqli_connect($user, $password, $address); mysqli_select_db($database); foreach($postarr as $key => $value){ $sql = 'UPDATE record SET WHERE 1=1; mysqli_query($sql , $link_id); } mysqli_close($link_id); ?> Not sure if that's what you are looking for but it would be the cleanest and easiest code. Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted November 20, 2007 Author Share Posted November 20, 2007 ok thanks, im quite new to this stuff so its a bit jibberish at the moment. how would that code know to add each row in the html table as a new rown in the sql table? there are 4 fields in each html table row: driver1, group1, group_position1 and time1. but if it is second place then the names are: driver2, group2, group_position2 and time2 and so on for 3rd, 4th, 5th etc. im struggling to see the way in which the code above can put the seperate rows into seperate rows in a DB table. Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 20, 2007 Share Posted November 20, 2007 Try using the name = '' part to be an array, similar with checkboxes, this way it's all stored inside one variable. <input type='text' name='driver[]'> and then $_GET/POST['driver'] is an array.. foreach($_GET/POST['driver'] as $driver){ $query = mysql_query("INSERT INTO blah ('driver') VALUES ('$driver')") or die(mysql_error()); mysql_free_result($query); } Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted November 20, 2007 Author Share Posted November 20, 2007 thanks, i can understand that method a bit more. but how would i insert other values into the table aswell. i can get it to add just the drivers names but i need to add the rest of the data to co-ordinating drivers too? Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted November 20, 2007 Author Share Posted November 20, 2007 im using this code: foreach($_POST['driver'] as $driver){ $query = mysql_query("INSERT INTO results (driver) VALUES ('$driver')") or die(mysql_error()); mysql_query($query, $connection); but it only adds the first record and no others Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 21, 2007 Share Posted November 21, 2007 Do you have an index in the table? Auto-increment id? 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.