Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Update a record it does not tell me that one already exists when I am only editing

Guest
Sep 13, 2010 Sep 13, 2010

I am having a difficult time getting my code to search if my record has duplicated.  I was able to get it where if it searches my database and there is a repeating record it alerts me that there is a record that exists.  However, what my issue is, if I want to update that record lets say I want to change the order that it lists, then I get an error stating that the same record exists.  I know the same record exists, I just want to update that record.  I find it that the only way to go around it is name it something else, post it, then go back into it and edit it as to what I want.  How do I go around it where if I want to update a record it does not tell me that one already exists when I am only editing.

Here is the following code:

Here are the recordsets

$colname_getLiturgy = "-1";
if (isset($_GET['liturgyid'])) {
  $colname_getLiturgy = $_GET['liturgyid'];
}
mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getLiturgy = sprintf("SELECT * FROM liturgy WHERE liturgyid = %s", GetSQLValueString($colname_getLiturgy, "text"));
$getLiturgy = mysql_query($query_getLiturgy, $conOnDemand) or die(mysql_error());
$row_getLiturgy = mysql_fetch_assoc($getLiturgy);
$totalRows_getLiturgy = mysql_num_rows($getLiturgy);

mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getLiturgyHeading = "SELECT * FROM liturgyheadings ORDER BY liturgyheadorder ASC";
$getLiturgyHeading = mysql_query($query_getLiturgyHeading, $conOnDemand) or die(mysql_error());
$row_getLiturgyHeading = mysql_fetch_assoc($getLiturgyHeading);
$totalRows_getLiturgyHeading = mysql_num_rows($getLiturgyHeading);

mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getLiturgyPieces = "SELECT * FROM liturgypieces ORDER BY liturgypiecesorder ASC";
$getLiturgyPieces = mysql_query($query_getLiturgyPieces, $conOnDemand) or die(mysql_error());
$row_getLiturgyPieces = mysql_fetch_assoc($getLiturgyPieces);
$totalRows_getLiturgyPieces = mysql_num_rows($getLiturgyPieces);

$colname_getDupLitHeadID = "-1";
if (isset($_POST['liturgyheadid'])) {
  $colname_getDupLitHeadID = $_POST['liturgyheadid'];
}
$litTitleBatch_getDupLitHeadID = "-1";
if (isset($_POST['liturgyid'])) {
  $litTitleBatch_getDupLitHeadID = $_POST['liturgyid'];
}
mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getDupLitHeadID = sprintf("SELECT * FROM liturgy WHERE liturgy.liturgyheadid = %s AND liturgy.liturgyid !=%s", GetSQLValueString($colname_getDupLitHeadID, "text"),GetSQLValueString($litTitleBatch_getDupLitHeadID, "int"));
$getDupLitHeadID = mysql_query($query_getDupLitHeadID, $conOnDemand) or die(mysql_error());
$row_getDupLitHeadID = mysql_fetch_assoc($getDupLitHeadID);
$totalRows_getDupLitHeadID = mysql_num_rows($getDupLitHeadID);

$colname_getDupLitPiecesID = "-1";
if (isset($_POST['liturgypiecesid'])) {
  $colname_getDupLitPiecesID = $_POST['liturgypiecesid'];
}
$litPiecesBatch_getDupLitPiecesID = "-1";
if (isset($_POST['liturgyid'])) {
  $litPiecesBatch_getDupLitPiecesID = $_POST['liturgyid'];
}
mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getDupLitPiecesID = sprintf("SELECT * FROM liturgy WHERE liturgy.liturgypiecesid= %s   AND liturgy.liturgyid != %s", GetSQLValueString($colname_getDupLitPiecesID, "text"),GetSQLValueString($litPiecesBatch_getDupLitPiecesID, "int"));
$getDupLitPiecesID = mysql_query($query_getDupLitPiecesID, $conOnDemand) or die(mysql_error());
$row_getDupLitPiecesID = mysql_fetch_assoc($getDupLitPiecesID);
$totalRows_getDupLitPiecesID = mysql_num_rows($getDupLitPiecesID);

mysql_select_db($database_conOnDemand, $conOnDemand);
$query_getLiturgy = sprintf("SELECT * FROM liturgy WHERE liturgyid = %s", GetSQLValueString($colname_getLiturgy, "text"));
$getLiturgy = mysql_query($query_getLiturgy, $conOnDemand) or die(mysql_error());
$row_getLiturgy = mysql_fetch_assoc($getLiturgy);

Here is the "if" statement along with the coding:

$error = array();
// Validate form input
$MM_flag="MM_update";
if (isset($_POST[$MM_flag])) {

} // end of if statement


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
    if (($totalRows_getDupLitHeadID > 0) &&
        ($totalRows_getDupLitPiecesID > 0)) {
    $error['titleDuplicate'] = 'The liturgy piece title you had entered is already taken';
    }
   
if(!$error){
  $updateSQL = sprintf("UPDATE liturgy SET liturgyheadid=%s, liturgypiecesid=%s, liturgyorder=%s, liturgyonline=%s WHERE liturgyid=%s",
                       GetSQLValueString($_POST['liturgyheadid'], "text"),
                       GetSQLValueString($_POST['liturgypiecesid'], "text"),
                       GetSQLValueString($_POST['liturgyorder'], "int"),
                       GetSQLValueString(isset($_POST['liturgyonline']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['liturgyid'], "text"));

  mysql_select_db($database_conOnDemand, $conOnDemand);
  $Result1 = mysql_query($updateSQL, $conOnDemand) or die(mysql_error());

  $updateGoTo = "liturgy.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

}

-------

This is the same sample code and somewhat structure with just a little tweaking that is in the "The Essential Guide to Dreamweaver CS3 with CSS, Ajax,and PHP" by David Powers.

Please help me.

TOPICS
Server side applications
519
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 13, 2010 Sep 13, 2010
LATEST

As your first fail-safe, set the fields you wish to protect in your database to not allow duplicates. Then as a last resort your app will throw an error and prevent a duplicate.

Now, the way to keep errors from happening all the time is, once a form is submitted, do a query of your database to check for duplicate fields, for the fields you want to protect, such as email  (  WHERE emailfield = #FORM.email#  )  - not this is a CF version, that's what I know, but the principle is the same.  If the Query has a record count of greater than 0, you know you have a duplicate and you can handle this by showing a error and telling you user to submit different data before sending it to the database.

Between, Form Validation, Setting the database to not allow duplicates, and screening your data before sending it to the database, you'll be 100% sure that your database ends up with good, clean, unique data.

Hope this helps.

--
Lawrence Cramer - *Adobe Community Professional*
http://www.Cartweaver.com
Shopping Cart for Adobe Dreamweaver
available in PHP, ColdFusion, and ASP

Stay updated - http://blog.cartweaver.com

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines