This is my HTML Dynamic Form. You can add "n" Rows, to add multiple products.
<input type="text" class="form-control cantClass" name="n_cantidad[]"/>
<input type="text" class="form-control autocompletModelo" id="modelo1" name="n_modelo[]"/>
<input type="text" class="form-control serieClass" name="n_serie[]"/>
<textarea readonly class="form-control descrClass" id ="descripcion1" rows="2" name="n_descripcion[]"/></textarea>
Then im using serialize() in my jQuery Functions.
In PHP, I have the POST variables (Arrays):
$value_cantidad = ($_POST['n_cantidad']);
$value_modelo = ($_POST['n_modelo']);
$value_serie = ($_POST['n_serie']);
$value_descripcion = ($_POST['n_descripcion']);
I'm able to do and INSERT into MYSQL like this and it works :
$sql = 'INSERT INTO ' . $bd_base . '.'.$bd_table.' (cantidad, modelo, serie, descripcion) VALUES ("' . $v_cantidad[0] . '", "' . $v_modelo[0] . '", "' . $v_serie[0] . '", "' . $v_descripcion[0] . '")';
How can Insert into MySQL table multiple rows? For example
$sql = INSERT ....var [0] var [0] var [0] var [0]
$sql = INSERT ....var [1] var [1] var [1] var [1]
Depending on the version of php you are using, you can use "mysql" or "mysqli".
Mysqli is preferred if you use a recent php version.
A regular insert request would look like this:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
In php, you can store the request in a string:
$sql = "INSERT INTO table_name (column1, column2, column3,...)
VALUES ($val[0], $val[1], $val[2],...);"
then execute it using mysqli.
Here is a sample mysqli request taken from w3school:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
http://www.w3schools.com/php/php_mysql_insert.asp
If this website is to be open to the public, I would also suggest you to filter the input using a prepared query to prevent sql injections: http://www.w3schools.com/php/php_mysql_prepared_statements.asp