Update a record it does not tell me that one already exists when I am only editing
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.
