mardi 24 février 2015

Uploading an IMAGE to MySQL via PHP form

I'm trying to upload a single png/jpeg image and store in MySQL as BLOB The Image is "inserted" along with other form fields such as title,description etc.


Form method is set to post and enctype="multipart/formdata"> and My image column is set to blob type


The upload process/script is just not running , no error messages nor success ... but the script for uploading the other form inputs runs great, How can I add the two scripts to function correctly ?


PHP



<?php

$user = 'root';
$password = 'root';
$db = 'db_1';
$host = 'localhost';
$port = 888;

$link = mysqli_init();
$success = mysqli_real_connect(
$link,
$host,
$user,
$password,
$db,
$port
);

$title = mysqli_real_escape_string($link, $_POST['booktitle']);

$description = mysqli_real_escape_string($link, $_POST['bookdesc']);

$burl = mysqli_real_escape_string($link, $_POST['bookurl']);

$smurl = mysqli_real_escape_string($link, $_POST['smurl']);

$tag = mysqli_real_escape_string($link, $_POST['tag']);

$tagurl = mysqli_real_escape_string($link, $_POST['tagurl']);

$tag2 = mysqli_real_escape_string($link, $_POST['tag']);

$tag2url = mysqli_real_escape_string($link, $_POST['tagurl']);


$errors = true;

$query = "INSERT INTO second (title, description, burl, smurl, tag, tagurl, tag2, tag2url, image) VALUES ('$title', '$description', '$burl', '$smurl', '$tag', '$tagurl', '$tag2', '$tag2url', '$image')";
if (!mysqli_query($link,$query))
{
die('Error: ' . mysqli_error($link));
}
else
{
echo " <strong> Welcome Mohammed Azizi! </strong>
<a href=\"books2.php\"> Home </a> | <a href=\"admin-books.php\"> Admin Panel </a> |<hr /><br />
<center>
<table border=\"2\" width=\"50%\">
<tr >
<td style=\"background:#333\">
<img src=\"bg/libicon2.png\">
</td>
<td style=\"background:#999\">
<font size=\"32px\"> ... </font>
</td>
<tr>
<td>Book Title:</td>
<td><i>$title</i></td>
</tr>
<tr style=\"background:#e3e3e3\">
<td>Description</td>
<td><i>$description</i></td>
</tr>
<tr>
<td>Book URL</td>
<td><i>$burl</i></td>
</tr>
<tr style=\"background:#e3e3e3\">
<td>SM URL</td>
<td><i>$smurl</i></td>
</tr>
<tr>
<td>Tag 1</td>
<td><i>$tag</i></td>
</tr>
<tr style=\"background:#e3e3e3\">
<td>Tag 1 URL</td>
<td><i>$tagurl</i></td>
</tr>
<tr>
<td>Tag 2</td>
<td>$tag2</td>
</tr>
<tr style=\"background:#e3e3e3\">
<td>Tag 2 URL</td>
<td><i>$tag2url</i></td>
</tr>

</table>
<br/>
<a href=\"admin-books.php\"> Next>> </a>";


}

mysqli_close($link);


?>

<?php
$user = 'root';
$password = 'root';
$db = 'search_1';
$host = 'localhost';
$port = 8889;

$link2 = mysqli_init();
$success = mysqli_real_connect(
$link,
$host,
$user,
$password,
$db,
$port
);
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$query2 = "INSERT INTO second ";
$query2 .= "(image) VALUES ('$data')";
$results = mysqli_query($query2, $link2) or die(mysqli_error());
print "Success";
}
else {
print "Error";
}
mysqli_close($link2);

?>


HTML


Add Book - First Year Title



Description/Author



Book URL



SM URL



Tag 1



Tag 1 URL



Tag 2



Tag 2 URL


Aucun commentaire:

Enregistrer un commentaire