By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

text array not posting as null when empty!

P: 8
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
Expand|Select|Wrap|Line Numbers
  1. <form method='POST' action='entry2.php' name='entry'>
  2. <table>
  3. <tr><td colspan='4' align='center'>Mailing Address</td></tr>
  4. <tr><td>Street:<input type='text' name='mStreet[]' /></td><td>Zip:
  5. <?php
  6. selectDistinct($connection,'CityCode','zip','mzip[]','97465');
  7. ?>
  8. </td></tr>
  9. <tr><td>Street:<input type='text' name='mStreet[]' /></td><td>Zip:
  10. <?php
  11. selectDistinct($connection,'CityCode','zip','mzip[]','97465');
  12. ?>
  13. </td></tr></table></form>
  14.  
php to save to db
Expand|Select|Wrap|Line Numbers
  1. if(!empty($mStreet)){
  2. for($i=0;$i<2;$i++){
  3.   $Cityquery = "SELECT cityCode FROM CityCode WHERE zip='{$mzip[$i]}'";
  4.   if(!($result = @ mysql_query($Cityquery,$connection))){showerror();}
  5.   $row = mysql_fetch_array($result);
  6.   $mcityCode[$i] = $row['cityCode'];
  7.   $insertQuery = "INSERT INTO Address VALUES(NULL,1,'{$BID}','{$mStreet[$i]}','{$mcityCode[$i]}')";
  8.   if(!(@ mysql_query($insertQuery, $connection))){showerror();}
  9.   }
  10.   }
  11.  
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
Feb 27 '08 #1
Share this Question
Share on Google+
1 Reply


hsriat
Expert 100+
P: 1,654
Why didn't you used $_POST[ ] ...?

And do the validation inside the for loop as[php]for ($i=0; $i<2; $i++)
{
if(isset($mStreet[$i]))
{
//save data to db
}
}[/php]
Feb 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.