I believe that switching %s to %d was important...but the problem I described still exists. And please let me clarify, I tried using settype to convert $_POST['new_picorder'.$i] to see what value it returned before it was inserted into the sprintf function. Sorry that wasn't clear in my first post.
I am stumped. I am passing old_picorder and propertyid as hidden form fields. propertyid originally comes from a URL parameter and old_picorder comes from the table that I am updating. I know that these two fields are being passed correctly in the query since only the rows where 'propertyid' = 36 are being updated (and all the rows--not just those where 'picorder' = 1--are being updated).
When I print $_POST['new_picorder'.$i] to the screen before settype, it prints correctly. After it has been converted to an integer (regardless of the method), it always becomes '1.'
I'm posting both the form and the php script below (The HTML may appear incomplete as I omitted the uneditable template info). Thank you for your help.
<?php require_once('../Connections/connProperties.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_rsImages = "-1";
if (isset($_GET['propertyid'])) {
$colname_rsImages = $_GET['propertyid'];
}
mysql_select_db($database_connProperties, $connProperties);
$query_rsImages = sprintf("SELECT * FROM propertiesimages WHERE propertyid = %s ORDER BY picorder", GetSQLValueString($colname_rsImages, "int"));
$rsImages = mysql_query($query_rsImages, $connProperties) or die(mysql_error());
$row_rsImages = mysql_fetch_assoc($rsImages);
$totalRows_rsImages = mysql_num_rows($rsImages);
?>
<form method="post" action="update_images_process.php" name="update_image" method="post">
<table width="100%" border="0">
<tr>
<td width="35%"> </td>
<td width="65%">Order to view images (first 5 are the main images):</td>
</tr> </table>
<?php do { ?>
<table width="100%" border="0">
<tr>
<td width="35%"><img src="<?php echo $row_rsImages['path']; ?>" width="200" height="150" /></td>
<td width="65%"><label>
<?php echo "<input name=\"new_picorder" . $row_rsImages['picorder'] . "\" type=\"text\" id=\"new_picorder" . $row_rsImages['picorder'] . "\" size=\"2\" />"?>
<?php echo "<input name=\"old_picorder". $row_rsImages['picorder'] . "\" type=\"hidden\" id=\"old_picorder". $row_rsImages['picorder'] . "\" value=\"". $row_rsImages['picorder']."\">" ?>
</label><?php echo $row_rsImages['picorder'];
$total_images = $row_rsImages['picorder'];?></td>
</tr>
</table>
<?php } while ($row_rsImages = mysql_fetch_assoc($rsImages)); ?>
<?php echo "<input name=\"total_images\" type=\"hidden\" id=\"total_images\" value=\"$total_images\">
<input name=\"propertyid\" type=\"hidden\" id=\"propertyid\" value=\"$colname_rsImages\">
<input type=\"submit\" name=\"update_images_now\" value=\"Update Now!\">" ?>
</form>
<?php require_once('../Connections/connProperties.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$num_files = $_POST['total_images'];
$error = 0;
for ($i = 1; $i <= $num_files; $i++) {
for ($num = 1; $num <= $num_files; $num++) {
if ($_POST['new_picorder'.$num] == $_POST['new_picorder'.$i]) {
$error++;
}
}
}
for ($i = 1; $i <= $num_files; $i++) {
if ($error == $num_files) {
$updateSQL = sprintf("UPDATE propertiesimages SET picorder = %d WHERE propertyid=%d AND picorder = %d",
GetSQLValueString($_POST['new_picorder'.$i], "int"),
GetSQLValueString($_POST['propertyid'], "int"),
GetSQLValueString($_POST['old_picorder'.$i], "int"));
mysql_select_db($database_connProperties, $connProperties);
$Result1 = mysql_query($updateSQL, $connProperties) or die(mysql_error());
} else {
echo "You cannot have images with the same order number. Please click the back button and adjust";
break;
}
}
?>