-4

Not able to upload.getting error Problem uploading image.

    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 up more codes for clarification

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Amit
  • 81
  • 1
  • 8
  • 2
    Possible duplicate of [How to get mysqli error in different environments?](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments) – Masivuye Cokile Jul 18 '17 at 09:09

1 Answers1

1

As the code is not complete, So i will give you a walk through of the process.

After receiving the request, Take the filename and store it in a variable. Move the file to the folder and then store the image path in the DB

If you are getting mysql error then there can be multiple issue. 1. There is a mandatory field in the table that you are not putting any Value. 2. The column name is not matched or table name is wrong. 3. The database connection can also be an issue.

Please use mysqli_error($db) to see what is causing the issue. First point out the error before you solve it.

The issue you are facing can be solved by this approach.

$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'>";
      }               
   }
  • i provided link http://jsfiddle.net/amibhop/ja9fpo5b/ – Amit Jul 18 '17 at 09:25
  • The link you provided only has the html and css. And what you are facing is a database and PHP issue. Edit your Question and add the relevant part of your code for better assistance. – Shashank Shekhar Jul 18 '17 at 09:29
  • its added up you can see in js bottom – Amit Jul 18 '17 at 09:35
  • you can say it as many times as you want, but i don't see anything php or sql related in the link. Add the code here if you can or try to convince everyone to see the invisible code there. I already have given you the solution according to your input. – Shashank Shekhar Jul 18 '17 at 10:04
  • chk pl added few more lines – Amit Jul 18 '17 at 10:18
  • you are getting the content of the file and storing that in the database. Worst approach. move the file in the file system and then store the url in the database. It is very simple thing, you should try it on your own, otherwise i would have given you the code snippet. – Shashank Shekhar Jul 18 '17 at 11:08
  • if u can as i am doing this way – Amit Jul 18 '17 at 11:23
  • added the code for help. I left some minor mistakes. You have to find them and fix it here. This is to ensure that you learn something from here. SO is for error solving – Shashank Shekhar Jul 18 '17 at 12:02