-3

I wrote a php code to replace SQL row values. But it is not working as expected

ERROR : Parse error: syntax error, unexpected T_STRING in ...... on line 3

I think i should not use these quotes " " right ? But, when i ran this without " " got this error

Error updating comment table: 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 '1' at line 1

Here is my Code

<?php    
include_once("db.php");    
$sql1 = "update table1 set marks = replace(c2,"NK",'NOKIA')";
$sql2 = "update table1 set marks = replace(c2,"SM",'SAMSUNG')";
$sql3 = "update table1 set marks = replace(c2,"A",'APPLE')";
$sql4 = "update table1 set marks = replace(c2,"CH",'CHINAPECE')";
$sql5 = "update table1 set marks = replace(c2,"WO",'WORLDCLASS')";
if (mysql_query($sql1 && $sql2 && $sql3 && $sql4 && $sql5))
  {
  echo "Replaced.";
  }
else
  {
  echo "Error in replacing: " . mysql_error();
  }
?> 

Thank you!


UPDATE (updated php code with sujjestions sugested by Trinimon and beiller)

ERROR : Parse error: syntax error, unexpected ';' in .... on line 8

<?php    
    include_once("db.php");
    $sql1 = "update table1 set marks = replace(c2,"NK",'NOKIA')";
    $sql2 = "update table1 set marks = replace(c2,"SM",'SAMSUNG')";
    $sql3 = "update table1 set marks = replace(c2,"A",'APPLE')";
    $sql4 = "update table1 set marks = replace(c2,"CH",'CHINAPECE')";
    $sql5 = "update table1 set marks = replace(c2,"WO",'WORLDCLASS')";
    if (mysql_query($sql1);
        mysql_query($sql2);
        mysql_query($sql3);
        mysql_query($sql4);
        mysql_query($sql5);)
  {
      echo "Replaced.";
      }
    else
      {
      echo "Error in replacing: " . mysql_error();
      }
    ?> 

ERROR : Parse error: syntax error, unexpected ';' in .... on line 8

Starx
  • 77,474
  • 47
  • 185
  • 261
user2373405
  • 91
  • 1
  • 1
  • 10
  • 6
    Quotes, quotes, quotes. A basic syntax highlighter will catch this for you. – John Conde May 16 '13 at 19:00
  • possible duplicate of [Escaping quotation marks in PHP](http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – qJake May 16 '13 at 19:03
  • I misunderstood the question, my answer is deleted now. Look at the other one, he's right. Just remember that '\' is an escape character for these kinda of things. If you need to actually use '|' you should use '\\' which will result in only one backslash. –  May 16 '13 at 19:06
  • @JohnConde even the SO catches it. – Ryan B May 16 '13 at 19:11
  • It is localized but this question can be improved. – Starx May 16 '13 at 19:37
  • @Starx And what would be the use of that? – PeeHaa May 16 '13 at 19:39
  • @PeeHaa埽, Such mistakes are very abundant, if it can be addressed in more than one way, isn't that something good? – Starx May 16 '13 at 19:41
  • @Starx I disagree. Parse error questions are not helpful, except for the one posting the question. The goal of Stackoverflow questions is to help future visitors. – Jocelyn May 16 '13 at 19:42
  • @Starx No I wouldn't call this question something good no – PeeHaa May 16 '13 at 19:44
  • 1
    @Jocelyn, Look at your right side, I know what you mean. So many questions like this, but parse error are also error in programming So I believe it is a valid question. – Starx May 16 '13 at 19:47
  • @starx thanks a ton for giving importance to my question. – user2373405 May 16 '13 at 19:49

4 Answers4

3
 mysql_query($sql1 && $sql2 && $sql3 && $sql4 && $sql5);

This is incorrect. One mysql_query per $sql.

mysql_query($sql1);
mysql_query($sql2);
mysql_query($sql3);
mysql_query($sql4);
mysql_query($sql5);

As your error message shows $sql1 && $sql2 == TRUE which turns into "1" which is an invalid query.

beiller
  • 3,105
  • 1
  • 11
  • 19
2

Your problem is due to unescaped quotes. Use Single Quotes to define the values in a query string. It is less confusing that way.

Also there is a problem with the statements. Since you are combining statements they have to terminated using delimeter ;

$sql = "update table1 set marks = replace(c2,'NK','NOKIA');";
$sql .= "update table1 set marks = replace(c2,'SM','SAMSUNG');";
$sql .= "update table1 set marks = replace(c2,'A','APPLE');";
$sql .= "update table1 set marks = replace(c2,'CH','CHINAPECE');";
$sql .= "update table1 set marks = replace(c2,'WO','WORLDCLASS');";

And query like this:

mysql_query($sql);

NOTE: Please dont use MYSQL_* API Functions they are deprecated

Starx
  • 77,474
  • 47
  • 185
  • 261
1

Alternatively use this

$sql1 = "update table1 set marks = replace(c2,\"NK\",'NOKIA')";
$sql2 = "update table1 set marks = replace(c2,\"SM\",'SAMSUNG')";
$sql3 = "update table1 set marks = replace(c2,\"A\",'APPLE')";
$sql4 = "update table1 set marks = replace(c2,\"CH\",'CHINAPECE')";
$sql5 = "update table1 set marks = replace(c2,\"WO\",'WORLDCLASS')";

Se this post too.

p.s.: in addition I'm not so sure about this command:

if (mysql_query($sql1 && $sql2 && $sql3 && $sql4 && $sql5))

Check out this documentation (its deprecated and says "multiple queries are not supported"). I think you should use ...

$con=mysqli_connect("host","user","password","database");
mysqli_query($con,$sql1);
mysqli_query($con,$sql2);
...

Cheers!

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
1
if (mysql_query($sql1);
        mysql_query($sql2);
        mysql_query($sql3);
        mysql_query($sql4);
        mysql_query($sql5);)

is wrong. You should use a logical operator between each statement like && (AND), || (OR)

Pyrech
  • 593
  • 1
  • 4
  • 14