error in my IF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joecrack
Forum Commoner
Posts: 99
Joined: Mon Oct 31, 2005 9:17 pm

error in my IF

Post 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!!!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post 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.
Post Reply