Basically i want to store a path to an image into the database
Example:I want to save 'C:\wampp\www\project\images'
into database
Instead Pdo remove all the backslash and make it into 'C:wamppwwwprojectimages'
Is there a way to make Pdo keep the back开发者_StackOverflowslash?
Update with code:
$sql = "INSERT INTO meal (pic_path) VALUES('C:\wamp\www\project')";
$db = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute();
The reason that this is happening is because you're using double quotes ("
) around your query, and since the back slash (\
) is the escape character, it's being dropped. You can fix this by placing two back slashes together so it will yield one (C:\\wamp\\www\\project
).
However, Passing it to prepare
as an argument would be a better idea, and you can keep the double quotes.
$directory = "C:\wamp\www\project";
$sql = "INSERT INTO meal (pic_path) VALUES(?)";
$db = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute(array($directory));
Read more about prepared statements.
You should use PDO's variable substitution to safely insert values into the database. To edit your code, it would look like this:
$sql = "INSERT INTO meal (pic_path) VALUES(?)";
$db = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute(array('C:\wamp\www\project'));
PDO will replace the ? in your query with the escaped version of the string you pass.
精彩评论