Images Not Being Inserted into MySQL database
I am trying to use the form below to insert images into a MySQL database. However, nothing is being put into the MySQL table. Any idea why it's not working?
Thanks in advance,
John
EDIT: The image is $image1
.
The form:
echo '<form enctype="multipart/form-data" action="http://www...com/.../submit2.php" method="post">
<input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
<开发者_运维知识库;div class="submissiontitle"><label for="title">Blog Post Title:</label></div>
<div class="submissionfield"><input class="checkMax3" name="title" type="title" id="title" maxlength="80"></div>
<div class="texttitle"><label for="text1">Blog Post Text 1:</label></div>
<div class="textfield"><textarea class="checkMax" name="text1" type="comment" id="text1" maxlength="10000"></textarea></div>
<div class="imagetitle"><label for="image1">Image 1:</label></div>
<div class="imagefield"><input type="file" name="image1" /></div>
<div class="hyperlinktitle"><label for="url1">Hyperlink 1:</label></div>
<div class="hyperlinkfield "><input name="url1" type="title" id="url1" maxlength="200"></div>
<div class="submissionbutton"><input name="submit" type="submit" value="Submit"></div>
</form>
';
The query on submit2.php:
$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$uid = $_POST['uid'];
$title = $_POST['title'];
$text1 = $_POST['text1'];
$image1 = $_POST['image1'];
$image1 = $_FILES['image1'];
$url1 = $_POST['url1'];
$submissionid = mysql_insert_id();
$info1 = getImageSize($image1['tmp_name']);
$queryimage1 = sprintf(
"insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
values ('%s', '%s', '%s', %d, '%s')",
$submissionid,
mysql_real_escape_string($image1['name']),
mysql_real_escape_string($info1['mime']),
$image1['size'],
mysql_real_escape_string(
file_get_contents($image1['tmp_name'])
)
)or die(mysql_error());
mysql_query($queryimage1);
Did you check if the file upload succeeded?
if ($_FILES['image1']['error'] !== UPLOAD_ERR_OK) {
die("File didn't upload, error code is {$_FILES['image1']['error']");
}
Did you check if the query succeeded?
$result = mysql_query($queryimage1);
if ($result === FALSE) {
die("Query failed: " . mysql_error());
}
Honestly... why are there so many posts here without even the most basic simplest of error handling/checking? It's as if people expect code to magically work regardless of any errors, so there's no point in putting in error handling.
Depending on how large the images are, you may to have change MySQL's max_allowed_packet value. The default is 1MB, any larger images will result in an error and no data stored.
i feel you should use
$image1 = $_FILES['image1'];
$newImage = file_get_contents($image1);
and use $newImage in your Mysql query
sorry it was not complete
update
$image1 = $_FILES['image1']['tmp_name'];
$newImage = file_get_contents($image1);
精彩评论