Can't include an HTML link in PHP script, but works fine as MySQL query

2

I'm using the following as part of a PHP script:

mysqli_query($con,"UPDATE table
SET description='<a href="http://123456.biz/index.php/123/321/456/*/789">text</a>' 
WHERE description IS null;");

The query works fine with MySQL, but I get the following error when running it as part of the PHP script:

PHP Parse error: syntax error, unexpected 'href' (T_STRING)

What do I need to change?

Arthur Walker

Posted 2015-06-26T04:10:19.290

Reputation: 121

Answers

2

Dont have PHP installed, can't test if this is working, please try the following:

   mysqli_query($con,"UPDATE table
    SET description='<a href=\"http://123456.biz/index.php/123/321/456/*/789\">text</a>' 
    WHERE description IS null;");

Bilo

Posted 2015-06-26T04:10:19.290

Reputation: 1 326

2

Your quotes are the issue. You're using double quotes (") twice in the string and the parser is getting confused. Try escaping them instead with a backslash(\):

mysqli_query($con,"UPDATE table
    SET description='<a href=\"http://123456.biz/index.php/123/321/456/*/789\">text</a>' 
    WHERE description IS null;");

Huey

Posted 2015-06-26T04:10:19.290

Reputation: 683

1

Problem

The value you are setting has double quotation marks. You need to escape these by using backslash \

This tells the script to use the characters as part of the value and not part of the script it's self.

Solution

Just a simple mistake in your coding, try...

mysqli_query($con,"UPDATE table SET description='<a href=\"http://123456.biz index.php/123/321/456/*/789\">text</a>' WHERE description IS null;");

Further Reading

PHP AddSlashes

PHP Escape Sequences

PHP Escape Special Characters

Craig Lowe

Posted 2015-06-26T04:10:19.290

Reputation: 163