-1

I'm having a problem when trying to add a URL to a mySQL database.

The string is a URL:

http://pbs.twimg.com/profile_images/1708867059/405000_10150426314376065_707061064_8645107_703731598_n_normal.jpg

The error I get is:

Error description: 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 '://pbs.twimg.com/profile_images/1708867059/405000_10150426314376065_707061064_86' at line 1

It seems as though it won't allow me to add a URL, I presume there is something wrong with some of the characters but I don't know what?

My SQL is:

INSERT INTO accounts (name,consumerkey,consumersecret,pic_url) VALUES ($twitterID,$consumerkey,$consumersecret,$picture_url)"
Francesca
  • 26,842
  • 28
  • 90
  • 153

2 Answers2

0

You cannot truly solve this kind of problem by adding a few characters (like ' or ") to your bespoke sql string!

Instead, get to know the real way to write sql in php (it's like a very badly kept secret), which is to use PDO statements. This will allow you to use placehoders like (:twitterID, :consumerKey, :consumerSecret, :pictureUrl) which will accept complex variables such as urls and any of the crap users send in much more gracefully.

In the long run, this will save you a lot of trouble and time.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • Can you offer any help when writing prepared statements for a simple select query like this? I've been trying all morning but all the examples have loads of extra stuff in and I don't know what I need to omit. – Francesca Nov 14 '14 at 10:03
  • Well, you can try using these database/pdo wrappers: http://pastebin.com/BQAfiFmT Obviously they'll need some modification for your particular case, but being able to go `query_item('select count(*) from users where firstname = :name', array(':name'=>$bob));` is quite useful. – Kzqai Nov 14 '14 at 16:28
0

You need to quote string values and any other character that SQL will complain about, in this case it's the colon; see further down below.

($twitterID,$consumerkey,$consumersecret,'$picture_url')

or

('".$twitterID."','".$consumerkey."','".$consumersecret."','".$picture_url."')

if you wish to quote all the values.

Sidenote: You can remove the quotes around the variables that are integers.

I.e.:

This based on, and without seeing how the rest of your code looks like:

$picture_url = "http://pbs.twimg.com/profile_images/1708867059/405000_10150426314376065_707061064_8645107_703731598_n_normal.jpg";
  • The error states that it is near : - near being just that, the colon.
...right syntax to use near '://pbs.twimg.com
                             ^ right there

You can also use:

VALUES ($twitterID, $consumerkey, $consumersecret, '" .$dbcon->real_escape_string($picture_url) . "')";

$dbcon is an example of a DB connection variable and based on mysqli_ syntax.

Something you haven't stated as to which MySQL API you are using.


Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141