Jump to content


Photo

Writing to db my slashes disappear!


  • Please log in to reply
3 replies to this topic

#1 renno

renno
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 13 September 2006 - 04:02 PM

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


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 September 2006 - 04:27 PM

Can you post your code?

Info: PHP Manual


#3 renno

renno
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 13 September 2006 - 04:40 PM

THE UPLOAD 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>";
		
	}

}
?>

view path/image 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();
?>

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


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 September 2006 - 07:40 PM

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
<?php $str = "C:\\new\\folder\\filename.ext" ?>
or use forward slashes, which work just as well:
<?php $str = "C:/new/folder/filename.ext" ?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users