简体   繁体   中英

Inserting auto incremented fields into a SQL database

I have some simple PHP code that will generate new text boxes with the naming scheme of 'car_init$i' and 'car_num$i'. Where $i = 1 and I use i++ to increment it up. The problem I'm having is that while a user can have a maximum of 70 text boxes generated, it can be any number between 1 and 70. So I can have 46 text boxes as an example on a page if the user wanted just 46. So I would have car_num1, car_num2, car_init1, car_init2, etc. as my form names.

Car_ID would be my auto-incremented primary key, and I'd have 2 columns car_num and car_init. Is it possible to do something like this: INSERT INTO dbo (car_init, car_num) VALUES (car_init$i, car_num$i) and then use $i = 1 and i++ to increment it while adding all the values to new rows? Car_id = 1 would contain car_num1 and car_init1 information in their respective columns, Car_id = 2 would contain car_num2 and car_init2 information, and so on and so forth.

EDIT: So this is the code I have now:

$car_num = $_POST["car_num"];

foreach($_POST['car_init'] as $key => $car_init)
{
// your insert query
$sql = "INSERT INTO CustBill_cars (C_ID, car_init, car_num) VALUES ('1', '".$car_init."',          '".$car_num[$key]."')";
}

What happens is every time I add to my database, only the last thing I entered gets inputted. So if I have 3 cars needed, that's 6 text boxes, but only the last text boxes on the page are the ones that get inputted.

EDIT 2: This is how my text boxes are generated. All text boxes have it the way you said, using the 'car_init[]' and 'car_num[]'.

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

$i = 1; 

while ($i <= $_POST['carAmount'] AND $i <= 70) { 
     // Now print the text box to the screen 
     echo "<b>$i</b>. Car Initial: <input type=\"text\" class='element text small' name=\"car_init[]\" maxlength='4' id='car_init[]' /> ";
     echo "Number: <input type=\"text\" class='element text small' name=\"car_num[]\" maxlength='6' id='car_num[]' /><br>";

     $i++;
} 
} 

Change your html to something like this:

<input name="car_init[]" />
<input name="car_init[]" />
<input name="car_init[]" />
<input name="car_init[]" />
<input name="car_init[]" />

Then in php, your variable will be an array!

$_POST['car_init'] // is an array!

Loop through those and do multiple INSERT s.

foreach ($_POST['car_init'] as $car_num => $car_init) {
    // "INSERT INTO dbo (car_init, car_num) VALUES ('$car_init', $car_num)"
}

Edit based on your updates:

INSERT INTO CustBill_cars (C_ID, car_init, car_num) VALUES ('1', '".$car_init."',          '".$car_num[$key]."')"
  • Use PDO with prepared statements instead of using string interpolation. You seem to be susceptible to sql injection attacks.

Ah, I think I got it.

This is a problem:

foreach($_POST['car_init'] as $key => $car_init)
{
    // your insert query
    $sql = "INSERT INTO CustBill_cars (C_ID, car_init, car_num) VALUES ('1', '".$car_init."',          '".$car_num[$key]."')";
}

I assume you're then running the query $sql ? If so, that is running only the last value $sql contained! You need to:

foreach($_POST['car_init'] as $key => $car_init)
{
    // your insert query
    $sql = "INSERT INTO CustBill_cars (C_ID, car_init, car_num) VALUES ('1', '".$car_init."',          '".$car_num[$key]."')";
    // actually run the query!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM