Jump to content

Archived

This topic is now archived and is closed to further replies.

renno

Writing to db my slashes disappear!

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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.