Hi,
I am having a problem inserting an image in a MySQL database using PHP as binary data. Basically, I have a script to upload the image into a folder and insert a random image name into the database (this all works fine) but I also need to insert it in to the database also as binary data. Everything else is working (ie: image is uploading to the folder,etc.) but the image just isn't going into the database.
Heres the code:
<?php
require_once '../library/config.php';
require_once '../library/functions.php';
if(isset($_POST['txtName']))
{
$albumName = $_POST['txtName'];
$albumDesc = $_POST['mtxDesc'];
$newsDesc = $_POST['newsDesc'];
$imgName = $_FILES['fleImage']['name'];
$tmpName = $_FILES['fleImage']['tmp_name'];
/*-- Code for adding the newsletter image--*/
$imgName2 = $_FILES['fleImage2']['name'];
$tmpName2 = $_FILES['fleImage2']['tmp_name'];
// we need to rename the image name just to avoid
// duplicate file names
// first get the file extension
$ext = strrchr($imgName, ".");
$ext2 = strrchr($imgName2, ".");
// then create a new random name
$newName = md5(rand() * time()) . $ext;
$newName2 = md5(rand() * time()) . $ext2;
// the album image will be saved here
$imgPath = ALBUM_IMG_DIR . $newName;
// the flyer image will be saved here
$imgPath2 = FLYER_IMG_DIR . $newName2;
// resize all album image
$result = createThumbnail($tmpName, $imgPath,ALBUM_THUMBNAIL_WIDTH, ALBUM_THUMBNAIL_HEIGHT);
// resize all flyer image
$result2 = createThumbnail($tmpName2, $imgPath2, FLYER_THUMBNAIL_WIDTH, FLYER_THUMBNAIL_HEIGHT);
if (!$result) {
echo "Error uploading file - NB: NO FIELDS MUST BE LEFT BLANK!";
exit;
}
if (!$result2) {
echo "Error uploading file - NB: NO FIELDS MUST BE LEFT BLANK!";
exit;
}
if (!get_magic_quotes_gpc()) {
$albumName = addslashes($albumName);
$albumDesc = addslashes($albumDesc);
//$newsDesc = addslashes($newsDesc);
}
/*$query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date)
VALUES ('$albumName', '$albumDesc', '$newName', NOW())";
$query2 = "INSERT INTO tbl_newsletter (news_description) VALUES ('$newsDesc')";
mysql_query($query) or die('Error, add album failed : ' . mysql_error());
mysql_query($query2) or die('Error, add album failed : ' . mysql_error());*/
// Inserting the data into users
// -----------------------------
$data = $_FILES['fleImage'];
$sql = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date, bin_data,filename,filesize,filetype)
VALUES ('$albumName', '$albumDesc', '$newName', NOW(), '$data','$form_data_name','$form_data_size','$form_data_type')";
$result = mysql_query( $sql,$conn );//CHECK THIS VARIABLE!!!
if(isset($_POST['txtName']))
{
$user_id = mysql_insert_id( $conn );
$sql = "INSERT INTO tbl_newsletter (news_album_id, news_image) VALUES ($user_id, '$newName2')";
$result = mysql_query($sql, $conn);
}
mysql_free_result( $result );
// -----------------------------
// the album is saved, go to the album list
echo "<script>window.location.href='index.php?page=list-album';</script>";
exit;
}
?>
<form action="" method="post" enctype="multipart/form-data" name="frmAlbum" id="frmAlbum">
<table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_grey">
<tr>
<th width="150">Album Name</th>
<td width="80" bgcolor="#FFFFFF"> <input name="txtName" type="text" id="txtName"></td>
</tr>
<tr>
<th width="150">Description</th>
<td bgcolor="#FFFFFF"> <textarea name="mtxDesc" cols="50" rows="4" id="mtxDesc"></textarea> </td>
</tr>
<tr>
<th width="150">Club Flyer</th>
<td bgcolor="#FFFFFF"> <input name="fleImage" type="file" class="box" id="fleImage"><input type="hidden" name="MAX_FILE_SIZE" value="1000000"></td>
</tr>
<tr>
<th width="150">Newsletter Image</th>
<td bgcolor="#FFFFFF"><input name="fleImage2" type="file" class="box" id="fleImage2" /></td>
</tr>
<tr>
<td width="150" bgcolor="#FFFFFF"> </td>
<td bgcolor="#FFFFFF"> <input name="btnAdd" type="submit" id="btnAdd" value="Add Album">
<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();"></td>
</tr>
</table>
</form>
Thanks!