Chenelle
Chenelle

Reputation: 187

How to insert a file path into a MySQL table using php?

I'm trying to insert a file path into an empty MySQL table with PHP 5 but slashes and other characters are removed so I can not use to the string when calling it from the table. What function do I use to preserve the file path without losing characters?

$rawpath    = "F:\Business\test.htm";
$url        = "Business";

$query      = "INSERT IGNORE INTO `sites` (`url`,`base`)".
              "VALUES ('$rawpath','$url');";
echo $query;

Outputs: INSERT IGNORE INTO sites (url,base) VALUES ('F:\Business\test.htm','Business');

$query      = "SELECT * FROM `sites` WHERE `base`='Business';";
$row        = mysql_query($query);
$url        = mysql_result($row,0,"url");

echo $url;

Outputs: F:Businesstest.htm

Upvotes: 1

Views: 15807

Answers (3)

Shonkho
Shonkho

Reputation: 51

You can just do: quotemeta($rawpath) before the insert statement. All the backslashes will be correctly escaped after that.

Upvotes: 1

Carlos Campderrós
Carlos Campderrós

Reputation: 22972

Your problem is with backslashes \, which are used often to escape certain characters. You need to escape this backslashes (with another backslash), to make them appear. So, if you use double quotes, write \\ instead of just \, or use addslashes($url).

I'm not quite sure that you need to escape them if you use just single quotes.

Upvotes: 4

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

You need to escape your back slashes. Try this

$rawpath    = "F:\\Business\\test.htm";

Hope this helps.

Upvotes: 2

Related Questions