Page 1 of 1

error in my IF

Posted: Thu Nov 17, 2005 2:38 am
by joecrack
my code is supposed to unly change a field if there is a value in it.
i dont know what the error is ... i get the parse: T_CONSTANT_ENCAPSED_STRING
for the line: $sql = "WHERE eqdes ='" .$_POST['eqdes']."'";

Code: Select all

$params = array();
if ($_POST['groupp'] != '') {
    $params['groupp'] = $_POST['groupp'];
}
if ($_POST['beschreibung'] != ''){
    $params['beschreibung'] = $_POST['beschreibung'];
}
if (count($params)) {
   $sql = "UPDATE sam_artikel SET ";
   foreach ($params as $key=>$val) {
      $sql = "$key = '''.$val."', ";
   }
   $sql = substr($sql, 0, -2);
   $sql = "WHERE eqdes ='" .$_POST['eqdes']."'";
   mysql_query($sql);
}
I changed it over and over ...
plz help i dont know what to do!!!

Posted: Thu Nov 17, 2005 2:47 am
by twigletmac
The actual error was a few lines up (line 13), you have:

Code: Select all

$sql = "$key = '''.$val."', ";
but what you should see (and this is shown also by the syntax highlighting) is that before $val you have three single quotes instead of one single quote and a double quote. So you'd need to change it to look like this:

Code: Select all

$sql = "$key = '".$val."', ";
Note the difference in the syntax highlighting of the whole snippet now:

Code: Select all

$params = array();
if ($_POST['groupp'] != '') {
    $params['groupp'] = $_POST['groupp'];
}
if ($_POST['beschreibung'] != ''){
    $params['beschreibung'] = $_POST['beschreibung'];
}
if (count($params)) {
   $sql = "UPDATE sam_artikel SET ";
   foreach ($params as $key=>$val) {
      $sql = "$key = '".$val."', ";
   }
   $sql = substr($sql, 0, -2);
   $sql = "WHERE eqdes ='" .$_POST['eqdes']."'";
   mysql_query($sql);
}
Basically, the line reported by PHP was the line at which it could no longer process the file, often the actual syntax error occurs a few lines up.

Mac

Posted: Thu Nov 17, 2005 2:48 am
by ody
You problem is in this line:

Code: Select all

$sql = "$key = '''.$val."', ";
Notice your use of two single quotes instead of one?

Also I think you would rather be using $sql .= instead of just = after you have assigned an initial value to $sql, .= adds (concatenates) to the variable.