PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.
I'm trying to use the code below to write an image into a long blob field in my mysql database.
<?php
// Create MySQL login values and
// set them to your login information.
$username = "root";
$password = "*****";
$host = "localhost";
$database = "anml";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select thedatabase
mysql_select_db ($database);
// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
// Create the query and insert
// into the database.
$query = "UPDATE tblhomepage SET image='$data' WHERE homepage_id=4";
$results = mysql_query($query, $link);
// Print results
echo "<br/><br/><br/>";
echo "<center>";
echo "<p>Thank you, your file has been uploaded.</p>";
echo "</center>";
}
else {
echo "<br/><br/><br/>";
echo "<center>";
echo "<p>No image selected/uploaded. <br/> Please click <a href='editHomepageImage.php'>here</a> to try again</p>";
echo "</center>";
}
// Close our MySQL Link
mysql_close($link);
?>
It's returning a message saying that the image has successfully been uploaded, but it's not actually storing in the database..
Any suggestions as to why this might be??
Thanks so much
Dee
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.
$query = "UPDATE tblhomepage SET image='$data' WHERE homepage_id=4;
$results = mysql_query($query, $link);
if (!$results) {
die('Could not connect: image query not working ');
and it returns the 'could not connect: image not working message'... so i assume the prob is with my sql??
Storing images in the database is a bad idea. I really can't think of 1 good reason to do such a thing. I would encourage you to save the images to the file system instead.