Problem with an insert and update on same page
I have got an insert new record which is working great, and I have an update which is not. They are on the same form and both trigger when the button 'Insert' is pressed.
Both of them are entering into the same table, but the update is obviously updating an existing record. The update is working to the point that it is entering content into the correct record, but I am not getting the correct values entered; I am only getting '0' and '0000-00-00 00:00:00'... an INT column and DATETIME column.
For the INT I am trying to use fld_fREPLYCOUNT = fld_fREPLYCOUNT + 1 to increment each time the record is updated, and for the DATETIME I am simply using NOW() to record when it was updated. I can't use timestamp to update automatically as this will have an affect on ordering elsewhere.
Is it possible to update and insert to the same table from the same form at once?
Anyway, the insert is created with the DataAssist wizard, and then I have used the Update Single Record behaviour for the update. Here's the code that has been produced by the WA wizards:
<?php
// WA DataAssist Update
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $conn_mrs;
$WA_table = "tbl_forumPOSTS";
$WA_redirectURL = "";
if (function_exists("rel2abs")) $WA_redirectURL = $WA_redirectURL?rel2abs($WA_redirectURL,dirname(__FILE__)):"";
$WA_keepQueryString = false;
$WA_indexField = "fld_fID";
$WA_fieldNamesStr = "fld_fRELPYCOUNT|fld_fREPLYDATE";
$WA_fieldValuesStr = "fld_fREPLYCOUNT = fld_fREPLYCOUNT + 1" . $WA_AB_Split . "NOW()";
$WA_columnTypesStr = "none,none,NULL|',none,NULL";
$WA_comparisonStr = "=|=";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode($WA_AB_Split, $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_where_fieldValuesStr = "".$_SESSION['USES_threadID'] ."";
$WA_where_columnTypesStr = "none,none,NULL";
$WA_where_comparisonStr = "=";
$WA_where_fieldNames = explode("|", $WA_indexField);
$WA_where_fieldValues = explode($WA_AB_Split, $WA_where_fieldValuesStr);
$WA_where_columns = explode("|", $WA_where_columnTypesStr);
$WA_where_comparisons = explode("|", $WA_where_comparisonStr);
$WA_connectionDB = $database_conn_mrs;
mysql_select_db($WA_connectionDB, $WA_connection);
@session_start();
$updateParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WhereObj = WA_AB_generateWhereClause($WA_where_fieldNames, $WA_where_columns, $WA_where_fieldValues, $WA_where_comparisons );
$WA_Sql = "UPDATE `" . $WA_table . "` SET " . $updateParamsObj->WA_setValues . " WHERE " . $WhereObj->sqlWhereClause . "";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
And I have also tried the following:
<?php
// WA DataAssist Update
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $conn_mrs;
$WA_table = "tbl_forumPOSTS";
$WA_redirectURL = "";
if (function_exists("rel2abs")) $WA_redirectURL = $WA_redirectURL?rel2abs($WA_redirectURL,dirname(__FILE__)):"";
$WA_keepQueryString = false;
$WA_indexField = "fld_fID";
$WA_fieldNamesStr = "fld_fRELPYCOUNT|fld_fREPLYDATE";
$WA_fieldValuesStr = "".((isset($_POST["fld_fREPLYCOUNT"]))?$_POST["fld_fREPLYCOUNT"]:"") ."" . $WA_AB_Split . "".((isset($_POST["fld_fREPLYDATE"]))?$_POST["fld_fREPLYDATE"]:"") ."";
$WA_columnTypesStr = "none,none,NULL|',none,NULL";
$WA_comparisonStr = "=|=";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode($WA_AB_Split, $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_where_fieldValuesStr = "".$_SESSION['USES_threadID'] ."";
$WA_where_columnTypesStr = "none,none,NULL";
$WA_where_comparisonStr = "=";
$WA_where_fieldNames = explode("|", $WA_indexField);
$WA_where_fieldValues = explode($WA_AB_Split, $WA_where_fieldValuesStr);
$WA_where_columns = explode("|", $WA_where_columnTypesStr);
$WA_where_comparisons = explode("|", $WA_where_comparisonStr);
$WA_connectionDB = $database_conn_mrs;
mysql_select_db($WA_connectionDB, $WA_connection);
@session_start();
$updateParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WhereObj = WA_AB_generateWhereClause($WA_where_fieldNames, $WA_where_columns, $WA_where_fieldValues, $WA_where_comparisons );
$WA_Sql = "UPDATE `" . $WA_table . "` SET " . $updateParamsObj->WA_setValues . " WHERE " . $WhereObj->sqlWhereClause . "";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
With these hidden fields:
<input id="fld_fREPLYCOUNT" name="fld_fREPLYCOUNT" type="hidden" value="fld_fREPLYCOUNT = fld_fREPLYCOUNT + 1">
<input id="fld_fREPLYDATE" name="fld_fREPLYDATE" type="hidden" value="NOW()">
I am certain that the issue lies in what I am entering into the Value field of the bindings tab in the Update Single Record wizard... I am literally entering fld_fREPLYCOUNT = fld_fREPLYCOUNT + 1 and NOW(). Page in question is attached.
What am I doing wrong?
Thanks.