text array not posting as null when empty!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishctr
    New Member
    • Feb 2008
    • 8

    text array not posting as null when empty!

    Hi There,
    I am building a form that allows a business to enter at most 2 mailing addresses. i have the form set up so both inputs are there, storing as a post array. the problem is, when i try to use php to enter the data into in the mysql database, it enters empty data even when a field is null.
    html
    Code:
    <form method='POST' action='entry2.php' name='entry'>
    <table>
    <tr><td colspan='4' align='center'>Mailing Address</td></tr>
    <tr><td>Street:<input type='text' name='mStreet[]' /></td><td>Zip:
    <?php
    selectDistinct($connection,'CityCode','zip','mzip[]','97465');
    ?>
    </td></tr>
    <tr><td>Street:<input type='text' name='mStreet[]' /></td><td>Zip:
    <?php
    selectDistinct($connection,'CityCode','zip','mzip[]','97465');
    ?>
    </td></tr></table></form>
    php to save to db
    Code:
    if(!empty($mStreet)){
    for($i=0;$i<2;$i++){
      $Cityquery = "SELECT cityCode FROM CityCode WHERE zip='{$mzip[$i]}'";
      if(!($result = @ mysql_query($Cityquery,$connection))){showerror();}
      $row = mysql_fetch_array($result);
      $mcityCode[$i] = $row['cityCode'];
      $insertQuery = "INSERT INTO Address VALUES(NULL,1,'{$BID}','{$mStreet[$i]}','{$mcityCode[$i]}')";
      if(!(@ mysql_query($insertQuery, $connection))){showerror();}
      }
      }
    p.s. the selectDistinct functions return a list of zip codes that are accepted, and in the php it is used to determine the code that is associated with the zip
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Why didn't you used $_POST[ ] ...?

    And do the validation inside the for loop as[php]for ($i=0; $i<2; $i++)
    {
    if(isset($mStre et[$i]))
    {
    //save data to db
    }
    }[/php]

    Comment

    Working...