Save url in mysql

I want to execute mysql query from php.

$sql = insert into q_links values ( 'garment.png', 'imgs\ques\p1\garment.png' ); 

I could not store the url as it is, rather it is stored as follows: imgsquesp1garment.png . But I want to save the url as: imgs\ques\p1\garment.png . So I tried this:

 $sql = mysql_real_escape_string($sql); 

But this way my $sql looks like this:

 insert into q_links values ( \'garment.png\', \'imgs\\ques\\p1\\garment.png\' ); 

which do not work in mysql database.

I need to paste this url into the database for later use. Url imgs\ques\p1\garment.png . How can I achieve this?

Update: And I tried with the first comment that worked for me.

So the solution is:

 $sql = "insert into q_links values ( 'garment.png', '".mysql_real_escape_string( 'imgs\ques\p1\garment.png' )."' );"; 
+6
source share
7 answers

Add escape only for the img field:

 $sql = "insert into q_links values ( 'garment.png', '".mysql_real_escape_string( 'imgs\ques\p1\garment.png' )."' );" 
+2
source
 $url = "imgs\ques\p1\garment.png"; $url = mysql_real_escape_string($url); $sql = "INSERT INTO q_links VALUES ('garment.png', '$url')"; 

As a side note, the mysql_ * functions are deprecated, and you should go to prepared operations using mysqli_ * or PDO .

Example in PDO:

 $pdo = new PDO("mysql:host=localhost;port=3306;dbname=mydb", "user", "password"); $stmt = $pdo->prepare("INSERT INTO q_links VALUES (?, ?)"); $stmt->execute(array("garment.png", "imgs\ques\p1\garment.png")); $stmt->closeCursor(); 
+4
source

do not escape a single quote, only \

 $var = "insert into q_links values ( 'garment.png', 'imgs\\ques\\p1\\garment.png');" 
+2
source

Why don't you save it with a slash as such?

 $sql = insert into q_links values ( 'garment.png', 'imgs/ques/p1/garment.png' ); 
+2
source
 I can be like this: $img=addslashes("imgs\ques\p1\garment.png"); $sql=insert into q_links values('garment.png',$img); and while retriving you can use stripslashe(); 
+1
source

You can use this code to enter the image URL in the database

 $url =mysql_real_escape_string('imgs\ques\p1\garment.png'); $sql = "insert into q_links values ( 'garment.png', '".$url."' ); 

if you run the query: insert q_links into the values ​​('garment.png', 'imgs \ ques \ p1 \ garment.png');

it will be successfully inserted into the database

+1
source

Use PDO. mysql_ is deprecated anyway.

  $params = array('value_one','value_two') $dbh = new PDO('credentials go here'); $sql = 'insert into q_links values ( ?, ? );'; $stmt = $dbh->prepare($sql); $stmt->execute($params); 

Using PDO, you must prepare your statement, and then call it to execute with the desired variable. That would save all this for you.

0
source

All Articles