1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Insert Multiple Rows Into A Mysql Table Using Php Array

Discussion in 'PHP' started by gilbertsavier, Jun 30, 2009.

  1. gilbertsavier

    gilbertsavier New Member

    Joined:
    Jun 25, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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:
    PHP:
    <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)
    ?>
     
    Last edited by a moderator: Jun 30, 2009

Share This Page