28

i've been staringly blanky at this error and can't seem to know what the problem is.When i run the query i get this error:

unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING at this line:

$sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user=$rows['user'] ";
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
che
  • 319
  • 1
  • 3
  • 8
  • `$sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user=$rows[user]";` – Gordon Mar 06 '12 at 11:49
  • See the [chapter on variable parsing in strings](http://php.net/manual/en/language.types.string.php#language.types.string.parsing). And let's hope you sanitized these values before interpolating them into your query instead of using prepared statements. – Gordon Mar 06 '12 at 11:52
  • In my case, I had the query in [HEREDOC](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) string syntax, and indented the closing tag. It's allowed now in PHP 8+, but the project was on PHP 7.2. (Fetch API returned 500. It's nice to have log to at least Google things.) – s3c Feb 10 '21 at 21:17

6 Answers6

46

try this

echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user='".$rows['user']."' ";
Toto
  • 89,455
  • 62
  • 89
  • 125
Ullas Prabhakar
  • 3,546
  • 3
  • 28
  • 27
  • thanks,it worked.but now let's say i want to updat more than one row how do i do that because this code doesn't seem to work: echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty AND name=$name WHERE user='".$rows['user']."' "; – che Mar 09 '12 at 12:40
14

Try

$sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user={$rows['user']} ";

You need curly brackets for array access in double quoted strings.

8

Use { before $ sign. And also add addslashes function to escape special characters.

$sqlupdate1 = "UPDATE table SET commodity_quantity=".$qty."WHERE user=".addslashes($rows['user'])."'";
Toto
  • 89,455
  • 62
  • 89
  • 125
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • Queries params must be escaped with appropriate funcs like PDO::quote() or mysqli_real_esape_string(), not addslashes() – JCH77 Mar 13 '23 at 07:35
2

In my case, heredoc caused the issue. There is no problem with PHP version 7.3 up. Howerver, it error with PHP 7.0.33 if you use heredoc with space.

My example code

$rexpenditure = <<<Expenditure
                  <tr>
                      <td>$row->payment_referencenumber</td>
                      <td>$row->payment_requestdate</td>
                      <td>$row->payment_description</td>
                      <td>$row->payment_fundingsource</td>
                      <td>$row->payment_agencyulo</td>
                      <td>$row->payment_agencyproject</td>
                      <td>$$row->payment_disbustment</td>
                      <td>$row->payment_payeename</td>
                      <td>$row->payment_processpayment</td>
                  </tr>
Expenditure;

It will error if there is a space on PHP 7.0.33.

Hoeun ANN
  • 31
  • 3
1

Change your code to.

<?php
$sqlupdate1 = "UPDATE table SET commodity_quantity=".$qty."WHERE user=".$rows['user'];
?>

There was syntax error in your query.

Milap
  • 6,915
  • 8
  • 26
  • 46
  • thanks,it worked.but now let's say i want to update more than one row how do i do that because this code doesn't seem to work: echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty AND name=$name WHERE user='".$rows['user']."' "; – che Mar 09 '12 at 12:44
  • For that see this. http://stackoverflow.com/questions/3432/multiple-updates-in-mysql and http://www.tizag.com/sqlTutorial/sqlupdate.php – Milap Mar 09 '12 at 12:57
-1

My issue was also within the heredoc. I had it within an if/then statement and the closing bracket directly after the semicolon. So I moved the closing bracket to its own line and issue was fixed.

I changed:

... ;}

to:

... ;
}
navid
  • 1,022
  • 9
  • 20