The if statement and checking if empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

The if statement and checking if empty

Post by Brad7928 »

Question... Is it possible to do this? I haven't used if before and when i do this it still inserts all my fields whether blank or not...

Code: Select all

if (isset($_POST['submit'])) { 
 $i=1;
while($i<=5) {
 
if(!empty('".$_POST["pick".$i]."') {
 
$i++;
 //do nothing except add 1 to i
 
}else{
 
$fld = 'pick'.$i;
$fld1 = 'addr'.$i;
$fld2 = 'dest'.$i;
$fld3 = 'tonn'.$i;
$fld4 = 'driv'.$i;
$fld5 = 'invn'.$i;
$fld6 = 'subc'.$i;
$fld7 = 'span'.$i;
 
 
if(isset($_POST[$fld])) {
   mysql_query("INSERT INTO `jobs` (`ts`, `pick`, `addr`, `dest`, `tonn`, `driv`, `invc`, `subc`, `span`, `completed`) VALUES ('ts', '".$_POST["pick".$i]."', '".$_POST["addr".$i]."', '".$_POST["dest".$i]."', '".$_POST["tonn".$i]."', '".$_POST["driv".$i]."', '".$_POST["invn".$i]."', '".$_POST["subc".$i]."', '".$_POST["span".$i]."', 'no')", $connect);
   echo ("<br>Inserted<br>");
  }
$i++;
}
}
}
 
?>
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: The if statement and checking if empty

Post by SimonMayer »

I'm not sure I understand the problem exactly, but this looks like bad syntax

Code: Select all

if(!empty('".$_POST["pick".$i]."')
The ' characters are encasing a string, so it will look for a string beginning with ".
You need it to just look for your variable, so use:

Code: Select all

if(!empty($_POST["pick".$i])
This may give you the result you expect. But if not, please could you provide details of what exactly you are expecting, compared against what you are getting?
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: The if statement and checking if empty

Post by Brad7928 »

I have the following code that displays a table and allows users to enter data into the database. Problem is that when it enters the data if some rows in the table are blank, it enters them as well.

Code: Select all

<?php 
//revalidate the page script start
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 23 Jun 1986 12:00:00 GMT"); // This date is in the past.
header("Pragma: no-cache");
//revalidate the page script stop
// Connects to your Database 
mysql_connect("localhost", "username", "password") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 
 
//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
{ 
 
//if the cookie has the wrong password, they are taken to the login page 
if ($pass != $info['password']) 
{ header("Location: index.php"); 
} 
 
 
//otherwise they are shown the admin area 
else 
include("header.inc.php"); 
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center" width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
    <td align="center"><strong>Pick Up From</strong></td>
    <td align="center"><strong>Address</strong></td>
    <td align="center"><strong>Destination</strong></td>
    <td align="center"><strong>Tonnes</strong></td>
    <td align="center"><strong>Driver</strong></td>
    <td align="center"><strong>Ryans Inv No.</strong></td>
    <td align="center"><strong>Sub Cont.</strong></td>
    <td align="center"><strong>Spa No.</strong></td>
  </tr>
  <tr>
    <td align="right"><strong>1.</strong></td>
    <td align="center"><input type="text" name="pick1" /></td>
    <td align="center"><input type="text" name="addr1" /></td>
    <td align="center"><input type="text" name="dest1" /></td>
    <td align="center"><input type="text" name="tonn1" /></td>
    <td align="center"><input type="text" name="driv1" /></td>
    <td align="center"><input type="text" name="invn1" /></td>
    <td align="center"><input type="text" name="subc1" /></td>
    <td align="center"><input type="text" name="span1" /></td>
  </tr>
  <tr>
    <td align="right"><strong>2.</strong></td>
    <td align="center"><input type="text" name="pick2" /></td>
    <td align="center"><input type="text" name="addr2" /></td>
    <td align="center"><input type="text" name="dest2" /></td>
    <td align="center"><input type="text" name="tonn2" /></td>
    <td align="center"><input type="text" name="driv2" /></td>
    <td align="center"><input type="text" name="invn2" /></td>
    <td align="center"><input type="text" name="subc2" /></td>
    <td align="center"><input type="text" name="span2" /></td>
  </tr>
...
...
...
    <tr>
    <td align="right"><strong>9.</strong></td>
    <td align="center"><input type="text" name="pick9" /></td>
    <td align="center"><input type="text" name="addr9" /></td>
    <td align="center"><input type="text" name="dest9" /></td>
    <td align="center"><input type="text" name="tonn9" /></td>
    <td align="center"><input type="text" name="driv9" /></td>
    <td align="center"><input type="text" name="invn9" /></td>
    <td align="center"><input type="text" name="subc9" /></td>
    <td align="center"><input type="text" name="span9" /></td>
  </tr>
  <tr>
    <td align="right"><strong>10.</strong></td>
    <td align="center"><input type="text" name="pick10" /></td>
    <td align="center"><input type="text" name="addr10" /></td>
    <td align="center"><input type="text" name="dest10" /></td>
    <td align="center"><input type="text" name="tonn10" /></td>
    <td align="center"><input type="text" name="driv10" /></td>
    <td align="center"><input type="text" name="invn10" /></td>
    <td align="center"><input type="text" name="subc10" /></td>
    <td align="center"><input type="text" name="span10" /></td>
  </tr>
</table>
<hr />
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><input type="hidden" name="date" value="<? echo date("j-M"); ?>" /></td>
  </tr>
  <tr>
    <td align="center"><input type="submit" name="submit" value="Submit Jobs" default="default" />
    <input type="reset" onClick="return confirm('Are you sure you want to clear the form?')" />
    </td>
  </tr>
</table>
</form>
 
<?
$hostname ="localhost";
$user = "username";
$pass = "password";
$database = "database";
 
// Connects to your Database 
$connect = mysql_connect($hostname, $user, $pass); 
 
mysql_select_db($database, $connect); 
 
//echo ("<br>Connected<br>");
 
if (isset($_POST['submit'])) { 
 
$i=1;
 
while($i<=10) {
 
 
$fld = 'pick'.$i;
$fld1 = 'addr'.$i;
$fld2 = 'dest'.$i;
$fld3 = 'tonn'.$i;
$fld4 = 'driv'.$i;
$fld5 = 'invn'.$i;
$fld6 = 'subc'.$i;
$fld7 = 'span'.$i;
 
if(isset($_POST[$fld])) {
   $sql = "INSERT INTO `jobs` (`ts`, `pick`, `addr`, `dest`, `tonn`, `driv`, `invn`, `subc`, `span`, `completed`) VALUES ('$_POST[date]', '".$_POST["pick".$i]."', '".$_POST["addr".$i]."', '".$_POST["dest".$i]."', '".$_POST["tonn".$i]."', '".$_POST["driv".$i]."', '".$_POST["invn".$i]."', '".$_POST["subc".$i]."', '".$_POST["span".$i]."', 'no')";
 
//echo $sql ."<br>";
 
mysql_query($sql) or die(mysql_error());
   //echo ("<br>Inserted<br>");
}
 
$i++;
}
echo ("<br /><h4 align='center'>Inserted</h4>");
}
 
include("footer.inc.php");
} 
} 
else 
 
//if the cookie does not exist, they are taken to the login screen 
{ 
header("Location: index.php"); 
} 
?>
I'm looking to add an else statement into the INSERT part that says if the field IS empty add 1 to i, if not insert into database then repeat the while loop. What am i missing?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: The if statement and checking if empty

Post by Eric! »

This is just a quick guess, but try this on line 133 instead of isset()

Code: Select all

if(!empty($_POST["pick".$i])) {
Why do you define all the $fld variables and then use $_POST in your query? Just experimenting with it? If you keep the $fld you could use your variable if you want

Code: Select all

if(!empty($fld)) {
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: The if statement and checking if empty

Post by Brad7928 »

Well that was a lot easier than i was making it!! I changed isset to !empty and it works :) Thanks.

With the $fld's i was originally using them in my query but then all of a sudden it didn't work so i changed to $_POST's, since it's working i kinda don't want to change it back in case it doesn't work, if it does happen to play up again though i will use the $fld's, they are just there to remind me... (they are commented out now...)

Thanks for your help :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: The if statement and checking if empty

Post by Ollie Saunders »

Also consider using trim() to make things like " " into "" so that they will be considered empty.
Post Reply