Back to Top
The nuts and bolts for
Creative web development

How Can I Update Multiple Records Using Repeat Region (PHP/MYSQL)

I have a site where folks post their resumes and photos. I want to be able to let those folks remove certain photos and change the order that they are displayed in their profiles.

I have a site where folks post their resumes and photos. I want to be able to let those folks remove certain photos and change the order that they are displayed in their profiles.

I created a page that list all of their photos in a form based on their username once they log in. Under each photo there is also a checkbox to "remove" the photo if they wish (it doesn't actually delete it, it just changes the status from "Approved" to "Not Approved" and only "Approved" photos are displayed).

I added a repeat region to that area of the page, but the submit button is only shown once. I want the people to go through the photos and pick the ones they want to remove, hit submit, and all of those records will update.

I did all of that and it only updates the first record in the list no matter which record in the repeated regions I change.

Here's The Code I am Referring To: (There is also a seperate form to allow them upload a new photo on the page and that works just fine)

// <?php
// *** Restrict Access To Page: Grant or deny access to this page
$FF_authorizedUsers=" ";
$FF_authFailedURL="../../index.php";
$FF_grantAccess=0;
session_start();
if (isset($HTTP_SESSION_VARS["MM_Username"])) {
  if (true || !(isset($HTTP_SESSION_VARS["MM_UserAuthorization"])) || $HTTP_SESSION_VARS["MM_UserAuthorization"]=="" || strpos($FF_authorizedUsers, $HTTP_SESSION_VARS["MM_UserAuthorization"])) {
    $FF_grantAccess = 1;
  }
}
if (!$FF_grantAccess) {
  $FF_qsChar = "?";
  if (strpos($FF_authFailedURL, "?")) $FF_qsChar = "&";
  $FF_referrer = $HTTP_SERVER_VARS['PHP_SELF'];
  if (isset($HTTP_SERVER_VARS['QUERY_STRING']) && strlen($HTTP_SERVER_VARS['QUERY_STRING']) > 0) $FF_referrer .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
  $FF_authFailedURL = $FF_authFailedURL . $FF_qsChar . "accessdenied=" . urlencode($FF_referrer);
  header("Location: $FF_authFailedURL");
  exit;
}

// Pure PHP Upload 2.1.10
$ppu = new pureFileUpload();
$ppu->path = "../../photos/";
$ppu->extensions = "GIF,JPG,JPEG,BMP,PNG";
$ppu->formName = "new_photo";
$ppu->storeType = "file";
$ppu->sizeLimit = "";
$ppu->nameConflict = "uniq";
$ppu->requireUpload = "true";
$ppu->minWidth = "";
$ppu->minHeight = "";
$ppu->maxWidth = "";
$ppu->maxHeight = "";
$ppu->saveWidth = "";
$ppu->saveHeight = "";
$ppu->timeout = "600";
$ppu->progressBar = "";
$ppu->progressWidth = "";
$ppu->progressHeight = "";
$ppu->redirectURL = "";
$ppu->checkVersion("2.1.10");
$ppu->doUpload();

// Smart Image Processor 1.0.7
if (isset($HTTP_GET_VARS['GP_upload'])) {
  $sip = new resizeUploadedFiles($ppu);
  $sip->component = "ImageMagick";
  $sip->resizeImages = "true";
  $sip->aspectImages = "true";
  $sip->maxWidth = "600";
  $sip->maxHeight = "600";
  $sip->quality = "80";
  $sip->makeThumb = "true";
  $sip->pathThumb = "../../photos/thumbs/";
  $sip->aspectThumb = "true";
  $sip->naming = "prefix";
  $sip->suffix = "thumb_";
  $sip->maxWidthThumb = "300";
  $sip->maxHeightThumb = "";
  $sip->qualityThumb = "70";
  $sip->checkVersion("1.0.7");
  $sip->doResize();
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (isset($editFormAction)) {
  if (isset($_SERVER['QUERY_STRING'])) {
   if (!eregi("GP_upload=true", $_SERVER['QUERY_STRING'])) {
     $editFormAction .= "&GP_upload=true";
  }
  } else {
    $editFormAction .= "?GP_upload=true";
  }
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "new_photo")) {
  $insertSQL = sprintf("INSERT INTO Photos (Photo_Name, Photo_Approved, UniqueID, User_Email, User_FirstName, User_LastName, Photo_Order) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Photo_Name'], "text"),
                       GetSQLValueString($_POST['Photo_Approved2'], "text"),
                       GetSQLValueString($_POST['UniqueID'], "text"),
                       GetSQLValueString($_POST['User_Email'], "text"),
                       GetSQLValueString($_POST['User_FirstName'], "text"),
                       GetSQLValueString($_POST['User_LastName'], "text"),
                       GetSQLValueString($_POST['Photo_Order'], "int"));

  mysql_select_db($database_Resumes_DB, $Resumes_DB);
  $Result1 = mysql_query($insertSQL, $Resumes_DB) or die(mysql_error());
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "Edit_Photos")) {
  $updateSQL = sprintf("UPDATE Photos SET photo_approved="%s," top_photo="%s" WHERE photo_id="%s"",
                       GetSQLValueString(isset($_POST['Photo_Approved']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['Top_Photo'], "text"),
                       GetSQLValueString($_POST['Photo_ID'], "text"));

  mysql_select_db($database_Resumes_DB, $Resumes_DB);
  $Result1 = mysql_query($updateSQL, $Resumes_DB) or die(mysql_error());
}

$colname_rsPhotos = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsPhotos = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_Resumes_DB, $Resumes_DB);
$query_rsPhotos = sprintf("SELECT * FROM Photos WHERE User_Email = '%s' AND Photo_Approved = 'Y' ORDER BY Photo_Order ASC", $colname_rsPhotos);
$rsPhotos = mysql_query($query_rsPhotos, $Resumes_DB) or die(mysql_error());
$row_rsPhotos = mysql_fetch_assoc($rsPhotos);
$totalRows_rsPhotos = mysql_num_rows($rsPhotos);

$colname_rsPhotoCount = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsPhotoCount = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_Resumes_DB, $Resumes_DB);
$query_rsPhotoCount = sprintf("SELECT * FROM Photos WHERE User_Email = '%s' ORDER BY Posted_Date ASC", $colname_rsPhotoCount);
$rsPhotoCount = mysql_query($query_rsPhotoCount, $Resumes_DB) or die(mysql_error());
$row_rsPhotoCount = mysql_fetch_assoc($rsPhotoCount);
$totalRows_rsPhotoCount = mysql_num_rows($rsPhotoCount);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1"" />
<title>Untitled Document</title>
<link href="../../stylenew.css" rel="stylesheet" type="text/css" />
<script language="'javascript'" src="'../../ScriptLibrary/incPureUpload.js'></script>"
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0" class="spotlight_photo_border">
  <tr>
    <td><form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="new_photo" id="new_photo" document.MM_returnValue">
      <table width="100%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td><div align="center">You Can Add A New Photo To Your Website Here.</div></td>
        </tr>
        <tr>
          <td><div align="center">
            <label>
            <input name="Photo_Name" type="file" class="Register_Forms" id="Photo_Name" />
            </label>
            <label>
            <input name="Submit" type="submit" class="Register_Forms" value="Submit" />
            </label>
            <input name="UniqueID" type="hidden" id="UniqueID" value="HIDDEN" />
            <input name="User_FirstName" type="hidden" id="User_FirstName" value="HIDDEN" />
            <input name="User_LastName" type="hidden" id="User_LastName" value="HIDDEN" />
            <input name="Photo_Approved2" type="hidden" id="Photo_Approved2" value="Y" />
            <input name="User_Email" type="hidden" id="User_Email" value="HIDDEN" />
            <input name="Photo_Order" type="hidden" id="Photo_Order" value="<?php echo $row_rsPhotoCount['Photo_Order']+1; ?>" />
          </div></td>
        </tr>
      </table>
            <input type="hidden" name="MM_insert" value="new_photo">
    </form>
    </td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="2" cellpadding="2">
      <tr>
        <td height="30" valign="bottom"><div align="center">You Can Remove Your Photos Here. You Can Also Choose/Change The 1st Photo. </div></td>
      </tr>
      <tr>
        <td><form action="<?php echo $editFormAction; ?>" id="Edit_Photos" name="Edit_Photos" method="POST">
          <div align="center">
                <?php do { ?>
                  <table width="300" border="0" align="center" cellpadding="2" cellspacing="2" class="spotlight_photo_border">
                    <tr>
                      <td colspan="2"><div align="center"><img src="../../photos/thumbs/thumb_<?php echo $row_rsPhotos['Photo_Name']; ?>" width="300" /><br />
                        </div></td>
                        </tr>
                    <tr>
                      <td width="148" valign="middle"></td>
                        <td width="146" valign="middle"></td>
                        </tr>
                    <tr>
                      <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td width="37%" valign="middle">Remove This Photo </td>
                              <td width="12%" valign="middle">
                                <input name="Photo_Approved" type="checkbox" class="Register_Forms" id="Photo_Approved" value="N" />
                                <input name="Photo_ID" type="hidden" id="Photo_ID" value="<?php echo $row_rsPhotos['Photo_ID']; ?>" /></td>
                              <td width="31%" valign="middle"><div align="center">Make 1st Photo  </div></td>
                              <td width="20%" valign="middle">
                                <div align="center">
                                  <input <?php if (!(strcmp($row_rsPhotos['Top_Photo'],"Y"))) {echo "checked=\"checked\"";} ?> name="Top_Photo" type="radio" class="Register_Forms" value="Y" />
                              </div>    </td>
                              </tr>
                        </table></td>
                        </tr>
                      </table>
                  <?php } while ($row_rsPhotos = mysql_fetch_assoc($rsPhotos)); ?><input name="Submit2" type="submit" class="Register_Forms" value="Submit" />
          </div>
          <input type="hidden" name="MM_update" value="Edit_Photos">
        </form>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($rsPhotos);
mysql_free_result($rsPhotoCount);
?>
//

 

Share this Article