Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get form to redirect properly

Status
Not open for further replies.

BadChough

Programmer
Dec 20, 2007
137
GB
I've got stuck again, so would be grateful for some assistance.
I'm updating a short text from a form to two msql tables simultaineously, and then moving on to another page, which is where the problem lies. I want to pass on a Primary Key to filter the recordset in the nest page, but whatever I try fails. I tend to get the error message:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
and I notice in the address bar I get more than the single variable that I want being transmitted.
I am using DWmx to write the code. As there are two pages invovled I'm unsure which to include, but this is from Page One
Code:
<?php require_once('../../Connections/tormented3.php'); ?>
<?php
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($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE cel_contents SET cel_org=%s WHERE cel_pk=%s",
                       GetSQLValueString($_POST['textfield'], "text"),
                       GetSQLValueString($_POST['contentField'], "int"));

  mysql_select_db($database_tormented3, $tormented3);
  $Result1 = mysql_query($updateSQL, $tormented3) or die(mysql_error());
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE chk_sixmonth SET ck_organisation=%s WHERE ck_pk=%s",
                       GetSQLValueString($_POST['textfield'], "text"),
                       GetSQLValueString($_POST['contactField'], "int"));

  mysql_select_db($database_tormented3, $tormented3);
  $Result1 = mysql_query($updateSQL, $tormented3) or die(mysql_error());

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

$colname_rstSix = "1";
if (isset($_GET['organ'])) {
  $colname_rstSix = (get_magic_quotes_gpc()) ? $_GET['organ'] : addslashes($_GET['organ']);
}
mysql_select_db($database_tormented3, $tormented3);
$query_rstSix = sprintf("SELECT ck_pk, ck_organisation FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix);
$rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error());
$row_rstSix = mysql_fetch_assoc($rstSix);
$totalRows_rstSix = mysql_num_rows($rstSix);

mysql_select_db($database_tormented3, $tormented3);
$query_rstCont = "SELECT cel_pk, cel_org FROM cel_contents";
$rstCont = mysql_query($query_rstCont, $tormented3) or die(mysql_error());
$row_rstCont = mysql_fetch_assoc($rstCont);
$totalRows_rstCont = mysql_num_rows($rstCont);
?>

<!DOCTYPE HTML PUBLIC "-//W3C/etc...

Sorry there's so much, but I don't know where the problem lies.
 
Well this:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

This tends to indicate a problem with your actual query, and not with PHP redirecting or anything like that.

Since I'm not sure which of your 2 queries is generating that error its hard to offer a solution.

But for starters try echoing out your queries before attempting to use them.

So add this in:

Code:
mysql_select_db($database_tormented3, $tormented3);
$query_rstSix = sprintf("SELECT ck_pk, ck_organisation FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix);
[red]echo "The query for rstSix is: " . $query_rstSix;[/red]
$rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error());
$row_rstSix = mysql_fetch_assoc($rstSix);
$totalRows_rstSix = mysql_num_rows($rstSix);

mysql_select_db($database_tormented3, $tormented3);
$query_rstCont = "SELECT cel_pk, cel_org FROM cel_contents";
[red]echo "The query for rstCon is: " . $query_rstCont;[/red]
$rstCont = mysql_query($query_rstCont, $tormented3) or die(mysql_error());
$row_rstCont = mysql_fetch_assoc($rstCont);
$totalRows_rstCont = mysql_num_rows($rstCont);
?>

See what that looks like.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the interest, vacunita.
Your code produces
Code:
The query for rstSix is: SELECT ck_pk, ck_organisation FROM chk_sixmonth WHERE ck_organisation = 'Age Concern'
The query for rstCon is: SELECT cel_pk, cel_org FROM cel_contentsmysql_select_db($database_tormented3, $tormented3); $query_rstSix = sprintf("SELECT ck_pk, ck_organisation FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix); $rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error()); $row_rstSix = mysql_fetch_assoc($rstSix); $totalRows_rstSix = mysql_num_rows($rstSix); mysql_select_db($database_tormented3, $tormented3); $query_rstCont = "SELECT cel_pk, cel_org FROM cel_contents"; $rstCont = mysql_query($query_rstCont, $tormented3) or die(mysql_error()); $row_rstCont = mysql_fetch_assoc($rstCont); $totalRows_rstCont = mysql_num_rows($rstCont); ?> 
TinyMCE Test

I also notice that there is a little red exclamation mark on the DWeaver "Server Behaviours" list, next to the "Recordset (rstSix), which tends to mean DW is feeling a bit itchy about that element and wants me to scratch that spot, if you know what I mean!
 
vacunita, on re-reading your suggestion it has occured to me that I am looking for the problem on the wrong page. You do realise that the error message is created when I try to launch page 2(chk_organ.php), do you?
The browser quits page 1(rename.php - the page I quoted), presents the error message, and the following appears in the address field:

Thanks again for any help you can give.
 
I did not realize because you did not tell me that the error was coming from another page and not the one you posted code for.

Secondly, it seems PHP is redirecting fine, but again the query it runs in the page it redirects to has a problem.

In that case we would need to see the code for chk_organ.php.

Again I suggest you echo out your queries in that page and make sure they are properly formed.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I apologise for not being clear in my initial question.
The echo from chk_organ.php query is:
Code:
The query for rstCheck is: SELECT * FROM chk_sixmonth WHERE ck_pk = 1
The php code for the page is:

Code:
<?php
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($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE chk_sixmonth SET ck_manager=%s, ck_firstname=%s, ck_lastname=%s, ck_position=%s, ck_tel=%s, ck_email=%s, ck_url=%s, ck_their_notes=%s WHERE ck_pk=%s",
                       GetSQLValueString($_POST['ck_manager'], "text"),
                       GetSQLValueString($_POST['ck_firstname'], "text"),
                       GetSQLValueString($_POST['ck_lastname'], "text"),
                       GetSQLValueString($_POST['ck_position'], "text"),
                       GetSQLValueString($_POST['ck_tel'], "text"),
                       GetSQLValueString($_POST['ck_email'], "text"),
                       GetSQLValueString($_POST['ck_url'], "text"),
                       GetSQLValueString($_POST['ck_their_notes'], "text"),
                       GetSQLValueString($_POST['ck_pk'], "int"));

  mysql_select_db($database_tormented3, $tormented3);
  $Result1 = mysql_query($updateSQL, $tormented3) or die(mysql_error());
}

$colname_rstCheck = "1";
if (isset($_GET['org'])) {
  $colname_rstCheck = (get_magic_quotes_gpc()) ? $_GET['org'] : addslashes($_GET['org']);
}
mysql_select_db($database_tormented3, $tormented3);
$query_rstCheck = sprintf("SELECT * FROM chk_sixmonth WHERE ck_pk = %s", $colname_rstCheck);
$rstCheck = mysql_query($query_rstCheck, $tormented3) or die(mysql_error());
$row_rstCheck = mysql_fetch_assoc($rstCheck);
$totalRows_rstCheck = mysql_num_rows($rstCheck);
?>

Can you see where the problem lies?
Thanks again.
 
Still I can't see any problem.

What about this query:

Code:
$updateSQL = sprintf("UPDATE chk_sixmonth SET ck_manager=%s, ck_firstname=%s, ck_lastname=%s, ck_position=%s, ck_tel=%s, ck_email=%s, ck_url=%s, ck_their_notes=%s WHERE ck_pk=%s",
                       GetSQLValueString($_POST['ck_manager'], "text"),
                       GetSQLValueString($_POST['ck_firstname'], "text"),
                       GetSQLValueString($_POST['ck_lastname'], "text"),
                       GetSQLValueString($_POST['ck_position'], "text"),
                       GetSQLValueString($_POST['ck_tel'], "text"),
                       GetSQLValueString($_POST['ck_email'], "text"),
                       GetSQLValueString($_POST['ck_url'], "text"),
                       GetSQLValueString($_POST['ck_their_notes'], "text"),
                       GetSQLValueString($_POST['ck_pk'], "int"));

Is it being produced correctly after all values have been substituted in?



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The above suggestion provided no positive results, so I ditched the whole lot and started from scratch. Now the link works OK. Not very satisfactory in that I don't know what was wrong, but very satisfactory in that I can now get on with the next problem ......
Many thanks for the help, vacunita
 
one thought for future readers. when debugging mysql use of mysql_error() is invaluable but is not as far as you can go to help yourself. something like this gives even more information

Code:
$result = mysql_query($sql) or bailout(mysql_error() . "<br/>Query was $sql", __LINE__, __FILE__);

Code:
function bailout($msg, $line, $file){
  echo <<<HTML
<h1>Error</h1>
<h3>error logged at line $line in $file<h3>
<p>$msg</p>
HTML;
}

this would tell the OP which query went wrong, what the error was and what the query looked like that threw the error.

if you're brave you can also examine the backtraces.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top