My first script looks like this:
<?php
# Now fetch the products for this transaction.
# odbc_num_rows() doesn't work with the MS Access driver when performing SELECT
# queries so yet more trickery is needed to obtain the correct number of rows
# that were returned from the $proddata query.
function num_rows($proddata) {
ob_start();
(int)$number=odbc_result_all($proddata);
ob_clean();
return $number;
}
session_start();
# Now perform the products loop.
$num = num_rows($proddata);
$_SESSION['productcount'] = $num;
$_SESSION['trackingid'] = $trackingid;
$i = 0;
while ($i < $num) {
$row = odbc_fetch_row($proddata,$i);
$PLU = odbc_result($proddata,'PLU');
$Description = odbc_result($proddata,'Description');
$Retail = odbc_result($proddata,'Retail');
$tracksalesnumber = odbc_result($proddata,'SalesNumber');
$ordertrackid = $trackingid;
if(mysql_num_rows($detaildata) == 0) {
$Delivery = "<input type='text' name='delivery$i' size=10 value='dd/mm/yyyy' />";
$Status = "<select name='status".$i."'>
<option value='0'>Please select...</option>
<option value='1'>In progress</option>
<option value='2'>Awaiting stock</option>
<option value='3'>In stock</option>
<option value='4'>Delivery arranged</option>
<option value='5'>Delivered</option>
<option value='6'>Delayed</option>
</select>";
} else {
$DeliveryDate = mysql_result($detaildata,$i,'DeliveryDate');
$StatusText = mysql_result($detaildata,$i,'Status');
$Delivery = "<input type='text' name='delivery$i' size='10' value='$DeliveryDate' />";
$trackid = mysql_result($trackdata,$i,'StatusInternal');
$Status = "<select name='status".$i."'>
<option value='$i' selected='selected'>$trackid</option>
<option value='1'>In progress</option>
<option value='2'>Awaiting stock</option>
<option value='3'>In stock</option>
<option value='4'>Delivery arranged</option>
<option value='5'>Delivered</option>
<option value='6'>Delayed</option>
</select>";
}
echo "<tr>
<td width='800' bgcolor='#000000' height='1' colspan='7'></td>
</tr>
<tr>
<td width='800' bgcolor='#ffffcc' height='3' colspan='7'></td>
</tr>
<tr>
<td width='10' bgcolor='#ffffcc'></td>
<td width='60' align='center' bgcolor='#ffffff'><font face='arial' size='2'>$PLU</font></td>
<td width='300' align='center' bgcolor='#ffffff'><font face='arial' size='2'>$Description</font></td>
<td width='60' align='center' bgcolor='#ffffff'><font face='arial' size='2'>£".sprintf('%01.2f',$Retail)."</font></td>
<td width='90' align='center' bgcolor='#ffffff'>$Delivery</td>
<td width='270' align='center' bgcolor='#ffffff'>$Status</td>
<td width='10' bgcolor='#ffffcc'></td>
</tr>
<tr>
<td width='800' bgcolor='#ffffcc' height='3' colspan='7'></td>
</tr>";
$_SESSION["Description$i"] = $Description;
$_SESSION["tracksalesnumber$i"] = $tracksalesnumber;
$_SESSION["ordertrackid$i"] = $ordertrackid;
$_SESSION["PLU$i"] = $PLU;
$_SESSION["orderdate$i"] = $EposTime;
$_SESSION["num$i"] = $i;
$i++;
}
?>
The idea being that which each iteration of the loop, I will end up with values like:
1st loop:
$SESSION["Description1"] = $Description;
$SESSION["tracksalesnumber1"] = $tracksalesnumber;
$SESSION["ordertrackid1"] = $ordertrackid;
$SESSION["PLU1"] = $PLU;
$SESSION["orderdate1"] = $EposTime;
$SESSION["num1"] = $i;
2nd loop:
$SESSION["Description2"] = $Description;
$SESSION["tracksalesnumber2"] = $tracksalesnumber;
$SESSION["ordertrackid2"] = $ordertrackid;
$SESSION["PLU2"] = $PLU;
$SESSION["orderdate2"] = $EposTime;
$SESSION["num2"] = $i;
and so on.
My second script looks like this:
<?php
session_start();
$trackingid = $_SESSION['trackingid'];
$query = "SELECT TrackingID FROM tbldetails WHERE TrackingID = '$trackingid'";
$select = mysql_query($query,$conn);
$productcount = $_SESSION['productcount'];
$count = $productcount;
$i = 0;
while($i < $count) {
$tracksalesnumber = $_SESSION["tracksalesnumber$i"];
$ordertrackid = $_SESSION["ordertrackid$i"];
$deliverydate = $_POST["delivery$i"];
$status = $_POST["staus$i"];
$description = $_SESSION["Description$i"];
$orderdate = $_SESSION["orderdate$i"];
if(mysql_num_rows($select) == 0) {
$data = "INSERT INTO tbldetails VALUES $tracksalesnumber, '$ordertrackid', '$deliverydate', $status, '$description', '$orderdate'";
} else {
$data = "UPDATE tbldetails SET DeliveryDate = '$deliverydate', Status = $status WHERE SalesNumber = $tracksalesnumber";
}
mysql_query($data,$conn);
$i++;
}
?>
The idea is to use the number of rows returned in the odbc query to set the iteration limit for the loop (there is always at least one product returned), then either insert rows in the mysql database if none exist with a matching transactionnumber or update matching rows if they do exist.
Trouble is, nothing is being updated or inserted in the mysql database. It seems the while loop in the second script isn't being executed. PHP does not output any errors so I'm wondering what the problem may be?