Jump to content

Insert Multiple Rows Into A Mysql Table Using Php Array


gilbertsavier

Recommended Posts

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:

 

Other involvement during high school, college (clubs, sports, work, volunteer, etc.):

Activity

Position

Start Date

End Date

 

 

 

 

 

 

 

 

 

 

 

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)

?>

-------------------------------

Thanks & regards

Lokananth

 

[Link Deleted]

Edited by Andrea
Link to comment
Share on other sites

Is this what you wanted? Then will each one beome it's own row in the database.

Hope it helps.

 

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')";

   mysql_query($involv);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...