Home > Archive > PHP on Windows > April 2005 > BLOBS problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Patrick Roane 2005-02-12, 8:56 pm |
| I am trying to load some jpgs etc. into mysql but my
code does not seem to want to communicate with the
database. I'm not even getting any (database, parse or
syntax) errors.
There has to be a simple explanation for this. Here is
my code.
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 13.1 Opening a Connection to a
Database</title>
</head>
<body>
<?php
if ( ! empty( $_REQUEST['strDesc'] )&&
! empty( $_REQUEST['fileUpload'] ) ){
// check user input here!
$dberror = "";
$ret = add_to_database( $_REQUEST['strDesc'],
$_REQUEST['fileUpload'],
$_REQUEST['fileUpload_type'],
$_REQUEST['fileUpload_name'],
$_REQUEST['fileUpload_size'], $dberror );
if ( ! $ret ) {
print "Error: $dberror<br />\n";
} else {
print "Thank you very much<br />\n";
}
} else {
write_form();
}
function add_to_database( $strDesc, $fileUpload,
$fileUpload_name, $fileUpload_size, $fileUpload_type,
&$dberror ) {
$strDesc = mysql_real_escape_string( $strDesc );
$fileUpload = mysql_real_escape_string(
$fileUpload );
$fileUpload_name = mysql_real_escape_string(
$fileUpload_name );
$fileUpload_size = mysql_real_escape_string(
$fileUpload_size );
$fileUpload_type = mysql_real_escape_string(
$fileUpload_type );
$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileUpload_size);
$fileContent = addslashes($fileContent);
$link = mysql_pconnect( "localhost", "", "" );
if ( ! $link ) {
$dberror = mysql_error();
return false;
}
if ( ! mysql_select_db( "myfiles", $link ) ) {
$dberror = mysql_error();
return false;
}
$query = "INSERT INTO myblobs( strDesc, fileUpload,
fileUpload_name, fileUpload_size, fileUpload_type )
values('$strDesc', '$fileUpload',
'$fileUpload_name', '$fileUpload_size',
'$fileUpload_type' )";
if ( ! mysql_query( $query, $link ) ) {
$dberror = mysql_error();
return false;
}
echo "<h1>File Uploaded</h1>";
echo "The details of the uploaded file are shown
below:<br><br>";
echo "<b>File name:</b> $fileUpload_name <br>";
echo "<b>File type:</b> $fileUpload_type <br>";
echo "<b>File size:</b> $fileUpload_size <br>";
echo "<b>Uploaded to:</b> $fileUpload <br><br>";
echo "<a href='uploadfile.php'>Add Another File</a>";
return true;
}
function write_form() {
print '
<form method="post" enctype="multipart/form-data"
name="frmUploadFile" action="{' . $_SERVER['PHP_SELF']
.. '}">
<p><input type="text" name="strDesc" size="20"
maxlength="50" />
file Description:</p>
<p><input type="file" name="fileUpload" size="20" />
Choose a file</p>
<input type="submit" value="Upload this file"
name="Submit" /></p>
</form>
</FORM> ';
}
?>
</form>
</BODY>
</HTML>
=====
Patrick Roane
Web design and development
www.franklin-band.com
Fox River Grove, Il.
| |
| cchurchouse 2005-04-12, 7:03 am |
| Patrick,
Can't see anywhere where you actually connect to your database. You will something along the lines of
$dbh= mysql_connect("servername","yourusername","") or die("I cannot connect to the database because:".mysql_error());
mysql_select_db("yourdbname");
servername is often 'localhost'
These lines need to be added at the start of every script that accesses your database. It's quite a good idea to put them in a separate file (let's call it 'mysql.php') and the include it in your scripts with the line:
include ('mysql.php');
That way, if you want to change the database details you only have to update one file instead of every file.
HTH. |
|
|
|
|