Jump to content

inserting multiple lines


Woodburn2006

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.