Array insert problems

Discussion in 'PHP' started by ladieballer2004, Aug 23, 2009.

  1. #1
    So I have a portion of my form that has three rows and four columns. The form is asking my users to enter activities or jobs they have been involved in. They have to fill in at least one row but they don't have to fill in all three. Right now my code entered all three even if a row was blank. So i put a while loop in it. It skips the empty row now but the loop won't stop. I get the same two records entered into my database over 40,000 times. I tried using a for loop but I still have the same problem. Here is my code.

    
    
    //enter rows into database
    foreach($_POST['Activity'] as $row=>$Act)
    {
    while (!empty($_POST['Activity']))
    { 
    
      $Activity=($Act);
      $Position=($_POST['Position'][$row]);
      $StartDate=($_POST['StartDate'][$row]);
      $EndDate=($_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 "$i<br/>";
      }
     
    
    }
    
    
    Code (markup):

    Here is my form:
    
    <body>
    <form action="insert.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>
    </body>
    </html>
    
    Code (markup):

    Please Help. Thankyou in advance
     
    ladieballer2004, Aug 23, 2009 IP
  2. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you have a while loop without change the value innert the loop... it's so an endless loop...

    you can better use "if" to prevent empty inserts...

    
    //enter rows into database
    foreach($_POST['Activity'] as $row=>$Act)
    {
    if (!empty($_POST['Activity']))
    { 
    ....
    ....
    
    Code (markup):
     
    Sudoku-Master, Aug 24, 2009 IP