Jump to content

Writing to db my slashes disappear!


renno

Recommended Posts

Hi,

I've written a file path to a database but when I look at the records in the database it has erased the slashes indicating the change of folders... for example...

C:\new\folder\filename.ext

becomes

c:newfolderfilename

Do I have to use addslashes() in some way?  I have tried it but nothing seems to be changing.

Also, when I use php to retrieve the database information instead of returning 'c:newfolderfilename' it displays 'Resource id #3'.

Any help would be very much appriciated...

Cheers
Link to comment
Share on other sites

[b]THE UPLOAD CODE:[/b][code]<?php

$uploadDir = 'C:\Program Files\xampp\htdocs\Sites\Test\uploads\#';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);

if (!$result)
{
echo "Error uploading file";
exit;
}

print "File uploaded to $filePath";

if ($dbc = mysql_connect ('localhost', 'us', 'pw))
{

print '<p>Successfully connected to MySQL.</p>';

if (@mysql_select_db ('client_db'))
{
print '<p>The database has been selected.</p>';
}
else
{
die ('<p>Could not select the database because: <b>' . mysql_error() .'</b></p>');
}

if(!get_magic_quotes_gpc())
{
//$fileName = addslashes($fileName);
//$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

mysql_close();

echo "<br>Files uploaded<br>";

}

}
?>[/code]

[b]view path/image code:[/b][code]<?php
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);

//Connect and select----------------------------------------------------------

if ($dbc = mysql_connect ('localhost', 'tom', 'ickellious'))
{
if (!@mysql_select_db ('client_db'))
{
die ('<p>Could not select the database because: <b>'. mysql_error().'</b></p>');
}
}
else
{
die ('<p>Could not connect to MySQL because: <b>'. mysql_error().'</b></p>');
}
//----------------------------------------------------------------------------

$query = "SELECT path FROM upload2";
$result = mysql_query ($query);
print "$result";

/*$query = 'SELECT * FROM upload2 ';//ORDER BY entry_ID DESC

if ($r = mysql_query ($query))
{
while ($row = mysql_fetch_array ($r))
{
print "<p>{$row['id']}<br/>
{$row['name']}<br/>
{$row['type']}<br/>
{$row['size']}<br/>
{$row['path']}<br/>
</p>";
}
}*/



mysql_close();
?>[/code]

[b]I have commented out some stuff I've tried but the query ran from the sql console returns the correct value but when it is ran through php, the wrong stuff is output...

Cheers[/b]
Link to comment
Share on other sites

Your backslash problem occurs because the backslash is the escape character in PHP & MySQL and when you're string is inserted into the DB, MySQL sees the backslash and removes it. You can either double the backslashes in the original string
[code]<?php $str = "C:\\new\\folder\\filename.ext" ?>[/code]
or use forward slashes, which work just as well:
[code]<?php $str = "C:/new/folder/filename.ext" ?>[/code]

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.