I have a problem when I want to insert multiple fields into one table.
Here\'s my form:
Add user
below is the fxample how to inset multi row at one time
$query_string = "INSERT INTO YOURTBL_NAME(column_1,column_2)VALUES";
$data ="";
for($i=0;$i<count($YOUR FILE ARRAY);$i++)
{
$data .='("'.$paramater_1[$i].'","'.$paramater_2.'"),';
}
$qry = substr($query_string.$data, 0, -1);
$result = mysql_query($qry);
if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $val) {
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
$i++;
}
}
This will solve your problem !
$education_institute_array = $_POST['education_institute'];
$education_qualification_array = $_POST['education_qualification'];
$education_start_date_array = $_POST['education_start_date'];
$education_end_date_array = $_POST['education_end_date'];
$education_note_array = $_POST['education_note'];
for ($i = 0; $i < count($education_institute_array); $i++) {
$education_institute = mysql_real_escape_string($education_institute_array[$i]);
$education_qualification = mysql_real_escape_string($education_qualification_array[$i]);
$education_start_date = mysql_real_escape_string($education_start_date_array[$i]);
$education_end_date = mysql_real_escape_string($education_end_date_array[$i]);
$education_note = mysql_real_escape_string($education_note_array[$i]);
$sql_education_insert = "INSERT INTO `education` (`user_id`, `education_institute`, `education_qualification`, `education_start_date`, `education_end_date`, `education_note`) VALUES ('$user_id', '$education_institute', '$education_qualification', '$education_start_date', '$education_end_date', '$education_note')";
$sql_result_education = mysql_query($sql_education_insert);
}
A little bit easier code which works for me well.
if (isset($_POST['submit'])) {
$i = 0;
for ((array) $_POST as $val) {
$sql = "INSERT INTO users (name, age) VALUES (
'{$_POST["name"][$i]}','{$_POST["age"][$i]}'
);
mysql_query($sql);
echo (!mysql_affetced_rows()) ? "Query Wrong" : "Query Okay";
$i++;
}
}
You are doing a foreach on $_POST
rather than on the name/age arrays. You should be doing foreach on name or age array like this:
if (
!empty($_POST['name']) && !empty($_POST['age']) &&
is_array($_POST['name']) && is_array($_POST['age']) &&
count($_POST['name']) === count($_POST['age'])
) {
$name_array = $_POST['name'];
$age_array = $_POST['age'];
for ($i = 0; $i < count($name_array); $i++) {
$name = mysql_real_escape_string($name_array[$i]);
$age = mysql_real_escape_string($age_array[$i]);
mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
}
}
I would also note that you are currently susceptible to SQL injection so I added the step of escaping your strings for name/age.
I would also highly suggest simply making a single bulk insert into the DB instead of an insert of each record individually (I will leave that up to you to implement). This approach is almost always preferable from a performance standpoint.
Finally, you REALLY should not be using mysql_*
functions as they are deprecated. Consider changing to mysqli or PDO.
foreach($_POST['firstname'] as $key=>$value) {
$firstname = $_POST['firstname'][$key];
$lastname = $_POST['tipo'][$key];
echo "Parte: $lastname";
echo "<br>";
echo "Tipo: $firstname";
echo "<br>";
}