Reply to topic
Insert Multiple Rows Into A Mysql Table Using Php Array
gilbertsavier


Joined: 25 Jun 2009
Posts: 9
Reply with quote
Hi,
I have a form that asks the applicant for previous involvement. They have the option of entering up to three rows and after they press submit the computer needs to enter possible multiple rows. Here is my form code:

<form action="insert1.php" method="post">
<table width="77%">
<td height="63" colspan="5"><h3>Other involvement during high school, college (clubs, sports, work, volunteer, etc.): </h3></td>
</tr>
<tr>
<td width="20%"><h3>Activity</h3></td>
<td width="19%"><h3>Position</h3></td>
<td width="23%"><h3>Start Date</h3></td>
<td width="25%" height="60"><h3>End Date</h3></td>
</tr>
<tr>

<td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
<td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
<td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
<td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
</tr>
<tr>

<td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
<td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
<td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
<td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
</tr>
<tr>

<td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" />
<td height="63"><input name="Position[]" type="text" id="Position[]" size="15" />
<td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" />
<td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" />
</table>
<p>&nbsp;</p>
<p>

<input type="submit" name="Submit" id="Submit" value="Submit" />

</p>
</form>



Here is the insert1.php file
<?php
$con = mysql_connect("localhost","Application","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("CpaApp", $con);


//Assign each array to a variable
foreach($_POST['Activity'] as $row=>$Act)
{
$Activity=$Act;
$Position=$_POST['Position'][$row];
$StartDate=$_POST['StartDate'][$row];
$EndDate=$_POST['EndDate'][$row];
}


//enter rows into database
foreach($_POST['Activity'] as $row=>$Act)
{
$Activity=mysql_real_escape_string($Act);
$Position=mysql_real_escape_string($_POST['Position'][$row]);
$StartDate=mysql_real_escape_string($_POST['StartDate'][$row]);
$EndDate=mysql_real_escape_string($_POST['EndDate'][$row]);
}


$involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate)
VALUES ('.$Activity.','.$Position.','.$StartDate.','.$EndDate.')";


if (!mysql_query($involv,$con))
{
die('Error: ' . mysql_error());
}
echo "$row record added";

mysql_close($con)
?>
nathacof
Forum Admin

Joined: 24 Oct 2006
Posts: 192
Location: Dover, DE
Reply with quote
To insert multiple rows with one query see Mysql.com's INSERT SYNTAX documentation.

Code:
INSERT INTO `table` (id, value, valueN... ) VALUES (1, 'value', 'valueN'...), (2, 'value', 'valueN'...);


You can generate the query like so:
Code:
<?php
$query = "INSERT INTO `table` (id, value, valueN) VALUES ";
foreach ($row as $array) {
  $query .= "(" . implode(",", $array) . "),";
}
$query = rtrim($query, ",");
$result = mysql_query($query);


Make sure you properly validate and filter your untrusted inputs!!!
Insert Multiple Rows Into A Mysql Table Using Php Array
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic