在mysql中上传图像给出null

时间:2017-07-18 09:00:13

标签: php mysql database image

无法upload.getting错误上传图片时出错。

    echo "That's not an image.";
else
{ 
if (!$insert = mysqli_query($db  ,"INSERT INTO uploadimages (`Name`,`image`,`user_id` )  VALUES ('$image_name','$image',$user_id)"))
echo "Problem uploading image.";
else

http://jsfiddle.net/amibhop/ja9fpo5b/  More info on this link here-please click

=============================================== ==

     http://jsfiddle.net/amibhop/ja9fpo5b/
 <form id="form" action="" method="post" enctype="multipart/form-data" name="a">


                         <table width="65%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#EFEFEF">
     <input type= "file" name="image"><p>



       <td height="243" colspan=3 valign="top"> 
         <table width="100%" border=0 cellpadding="3">
           <tr>
             <td width="100%" align="left" class="desc"><span class="style5">
         <input type="button" name="Add" value="Add" onclick="addRow();"/>
         <input name="submit" type="button" id="submit" value="Delete" onclick="deleteRow1('tblSample1');" />
       </span></td>
           </tr>
           <tr>
             <td align="left" class="desc"> <table id = "tblSample1" >                    
                              </table>
                               <input type="hidden" name="tblid" /><input type="hidden" name="cid" value="<?php echo $_REQUEST['id']; ?>" /></td>
             </tr>
                  <tr bgcolor="white">
                    <td height="28" colspan="3" align="center"><div align="center">
                      <input type="submit" name="submit2" value="Save" class="button1" />
                      </div></td>
 </tr>
              </table></td></tr>
                 </table>

                   </form>
    </body>
   <?php
 //connect to database
 include('1.php');
 include("db.php");
 if(isset($_POST['submit2']))
 {
 $user_id=$_POST['cid'];
 // file properties
 $file = $_FILES["image"]["tmp_name"];
 if (!isset($file))
 echo "Please select an image.";
 else
 {
 $image =addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
 $image_name = addslashes($_FILES["image"]["name"]);
 $image_size = getimagesize($_FILES["image"]["tmp_name"]);
 if ($image_size==FALSE)
 echo "That's not an image.";
 else
 {

 if (!$insert = mysqli_query($db  ,"INSERT INTO uploadimages (`Name`,`image`,`user_id` ) VALUES ('".$image_name."','".$image."','".$user_id.")"))
 echo "Problem uploading image.";
 else
 {
 $lastID= mysqli_query($db  ,'select * from uploadimages ORDER BY ID DESC LIMIT 1');
 $las=mysqli_fetch_array($lastID) ;
 $rea=$las['ID'];
 echo "Image uploaded <p> Your image:</p>echo <img src='uploadphoto1.php'?ID='$rea'>";
 }
 }
 }
 }
 ?>

 </html>

aDDING更多代码以澄清

1 个答案:

答案 0 :(得分:1)

由于代码不完整,所以我将向您介绍该过程。

收到请求后,取用文件名并将其存储在变量中。 将文件移动到该文件夹​​,然后将图像路径存储在DB

如果您收到mysql错误,则可能存在多个问题。 1.表中有一个强制字段,表示您没有任何值。 2.列名不匹配或表名错误。 3.数据库连接也可能是一个问题。

请使用mysqli_error($db)查看导致问题的原因。首先在解决之前指出错误。

您可以通过这种方法解决您遇到的问题。

$imgname=$_FILES['image']['name'];
if($_FILES['image']['error']==0)
{
   $uploadFile=move_uploaded_file($_FILES['image']['tmp_name'],"/images/$imgname");
   if($uploadFile)
   {
    if (!$insert = mysqli_query($db  ,"INSERT INTO uploadimages 
           (`Name`,`image`,`user_id` ) VALUES 
            ('".$imgname."','".$imgname."','".$user_id.")"))
            echo "Problem uploading image.";
   else
   {
      $lastID= mysqli_query($db  ,'select * from uploadimages ORDER BY ID DESC LIMIT 1');
      $las=mysqli_fetch_array($lastID) ;
      $rea=$las['ID'];
      echo "Image uploaded <p> Your image:</p>echo <img src='uploadphoto1.php'?ID='$rea'>";
      }               
   }