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> </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?