图像未插入MySQL数据库

时间:2011-04-06 01:55:09

标签: php mysql

我正在尝试使用下面的表单将图像插入MySQL数据库。但是,MySQL表中没有任何内容。知道为什么它不起作用吗?

提前致谢,

约翰

编辑:图片为$image1

表格:

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>
';

对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);

3 个答案:

答案 0 :(得分:2)

您是否检查过文件上传是否成功?

if ($_FILES['image1']['error'] !== UPLOAD_ERR_OK) {
   die("File didn't upload, error code is {$_FILES['image1']['error']");
}

您是否检查过查询是否成功?

$result = mysql_query($queryimage1);
if ($result === FALSE) {
   die("Query failed: " . mysql_error());
}

老实说......为什么这里有很多帖子,甚至没有最基本的最简单的错误处理/检查?就好像人们希望代码能够在任何错误中神奇地工作一样,所以没有必要进行错误处理。

答案 1 :(得分:0)

我觉得你应该使用

  

$ image1 = $ _FILES ['image1'];

     

$ newImage =   的file_get_contents($图像1);

并在Mysql查询中使用$ newImage

抱歉,这不完整

更新

  

$ image1 = $ _FILES ['image1'] ['tmp_name'];

     

$ newImage =   的file_get_contents($图像1);

答案 2 :(得分:0)

根据图像的大小,您可能需要更改MySQL的max_allowed_pa​​cket值。默认值为1MB,任何较大的图像都会导致错误并且不存储数据。

相关问题