无法将图像插入数据库

时间:2014-10-15 13:25:54

标签: php

无法将图像上传到数据库。提交表单后,我收到此错误:

  

上传数据时出错,请重试!

我的代码如下,请帮助我。

   <?php
            extract($_POST);
            extract($_GET);
            error_reporting(0);
        ?>
        <html>
        <head>
        <!-- Date Picker Starts -->
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
        <script src="js/script.js"></script>

        <script>
        $(document).ready(function() {
        $(function() {
        $(".datepicker").datepicker({ dateFormat: 'dd-mm-yy' });
        });
        });
        </script>

        <!-- Date Picker Ends -->
        <script> 
              function getccname(val)
        {
             if(val == 'company')
            {
                document.getElementById("colname").disabled= true;
                document.getElementById("course").disabled = true;
                document.getElementById("year").disabled   = true;
             } 
             else if(val == 'college')
             {
                document.getElementById("comname").disabled          = true;
                        document.getElementsByName("doj")[0].disabled= true;
                document.getElementById("colname").disabled          = false;
                    document.getElementById("course").disabled       = false;
                document.getElementById("year").disabled             = false;

             }
             else 
             {
                document.getElementById("comname").disabled          = false;
                        document.getElementsByName("doj")[0].disabled= false;
                document.getElementById("colname").disabled          = false;
                    document.getElementById("course").disabled       = false;
                document.getElementById("year").disabled             = false;

             }

        }

        </script>
        <!-- nice editor -->
            <script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
            <script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
            <!-- nice editor end -->
        </head>
        <body>
        <?php
        include("menu.php");
        ?><br>

        <?php
        include("database.php");
        if(isset($_POST['submit']))
        {
        $uploadpath= 'upload/'; // directory to store the uploaded files
        $max_size  = 2000; // maximum file size, in KiloBytes
        $alwidth   = 900; // maximum allowed width, in pixels
        $alheight  = 800; // maximum allowed height, in pixels
        $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'jpeg', 'png'); // allowed extensions

        if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) 
        {
            $timestamp           = time();
            $uploadpath          = $uploadpath . $timestamp . basename( $_FILES['fileup']['name']); // gets the file name
            $sepext              = explode('.', strtolower($_FILES['fileup']['name']));
            $type                = end($sepext); // gets extension
            list($width, $height)= getimagesize($_FILES['fileup']['tmp_name']); // gets image width and height
            $err                 = ''; // to store the errors
            // Checks if the file has allowed type, size, width and height (for images)
            if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
            if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
            if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;
            // If no errors, upload the image, else, output the errors
            if(is_uploaded_file($_FILES['fileup']['name']))
            {
            move_uploaded_file( $_FILES['fileup']['tmp_name'], $uploadpath) ;

            $file   =$uploadpath;
            $name   =$_POST["name"];
            $fname  =$_POST["fname"];
            $gender =$_POST["gender"];
            $email  =$_POST["email"];
            $date   =$_POST["date"];
            $mn     =$_POST["mn"];
            $pn     =$_POST["pn"];
            $address=$_POST["address"];
            $idnum  =$_POST["idnum"];
            $ccname =$_POST["ccname"];
            $colname=$_POST["colname"];
            $course =$_POST["course"];
            $year   =$_POST["year"];
            $comname=$_POST["comname"];
            $doj    =$_POST["doj"];

            $result=mysql_query("insert into registration(file,name,fname,gender,email,date,mn,pn,address,idnum,ccname,colname,course,year,comname,doj)
            values('$uploadpath','$name','$fname','$gender','$email','$date','$mn','$pn','$address','$idnum','$ccname','$colname','$course','$year','$comname','$doj')");
            print_r($result);

            echo "Inserted Successfully";
            }
        }
        else
        {
        echo "There was an error uploading the data, please try again!";
        }
        }
        ?>

        <form name="form" method="post" action="" onSubmit="submit;">
        <center><table border="1" cellspacing="0" style="width:500px">

        <tr>
        <td><b>Image</td> <td><input type="file" name="fileup" id="fileup" size="25" /></td>
        </tr>

        <tr>
        <td>Name</td>
        <td><input type="text" name="name"></td></tr>

        <tr>
        <td>Father Name</td>
        <td><input type="text" name="fname"></td></tr>


        <tr><td>Gender</td>
        <td><select name="gender">
        <option value   ="na">Select</option>
        <option value   ="Male">Male</option>
        <option value   ="Female">Female</option>
        </td></tr>

        <tr>
        <td>Email</td>
        <td><input type="text" name="email"></td></tr>

         <tr>
        <td>Date Of Birth</td>
        <td><input type="text" name="date" class="datepicker" required></td></tr>

        <tr>
        <td>Mobile Number</td>
        <td><input type="text" name="mn"></td></tr>

        <tr>
        <td>Parent Number</td>
        <td><input type="text" name="pn"></td></tr>

        <tr> 
        <td>Address</td> <td><textarea name="address" rows="3" cols="50"></textarea></td>
        </tr>

        <tr>
        <td>Coll/Comp ID Num</td>
        <td><input type="text" name="idnum"></td></tr>

        <tr><td>Company/College</td>
        <td><select name="ccname" onchange="getccname(this.value);">
        <option value   ="na">Select</option>
        <option value   ="company">Company</option>
        <option value   ="college">College</option>
        </td></tr>

        <tr>
        <td>College Name</td>
        <td><input type="text" name="colname" id="colname"></td></tr>

        <tr>
        <td>Course</td>
        <td><input type="text" name="course" id="course"></td></tr>

        <tr>
        <td>Year</td>
        <td><input type="text" name="year" id="year"></td></tr>

        <tr>
        <td>Company Name</td>
        <td><input type="text" name="comname" id="comname"></td></tr>

        <tr>
        <td>Date Of Join</td>
        <td><input type="text" name="doj" class="datepicker"></td></tr>


        <tr>
        <td colspan="2" align="center"><input class="button" type="submit" name="submit" value="submit"/>
        <input class="button" type="reset" name="Reset" value="Reset " /> </td>
        </tr></center>
        </table>
          </form>



        </body>
        </html>

3 个答案:

答案 0 :(得分:0)

    try this,Then include your insert query it will work enctype="multipart/form-data"

    <form name="form" method="post" action="" onSubmit="submit;" enctype="multipart/form-data">
    </form>



    <?php

     if(isset($_FILES['image']))
     {
       $errors= array();
       $file_name = $_FILES['image']['name'];
       $file_size =$_FILES['image']['size'];
       $file_tmp =$_FILES['image']['tmp_name'];
       $file_type=$_FILES['image']['type'];   
       $value = explode(".", $file_name);
       $file_ext = strtolower(array_pop($value));
       //$file_ext=strtolower(end(explode(".",$file_name))); -->this line wnt work bcoz only variable should be passed inside explode

      $expensions= array("jpeg","jpg","png","csv");   
      if(in_array($file_ext,$expensions)== false)
      {
       $errors="Extension not allowed, please choose a JPEG or PNG file.";
      }
      if($file_size > 2097152)
      {
      $errors[]='File size must be excately 2 MB';
      }    
      if(empty($errors)==true)
      {
       move_uploaded_file($file_tmp,"upload/".$file_name);
**//include here insert query**
       echo "Success";
      }else
      {
       print_r($errors);
      }
     }
    ?>

答案 1 :(得分:0)

第一。不要使用mysql_ *函数,而是使用mysqli_ *。

第二。如果接受任何数据,这是一个非常大的安全漏洞。

第三。你有没有创建这样的变量? $名称= $ _ POST [ “名称”];

因此,当您想要插入$_POST["name"]时,请执行以下操作:

修改

试试这个。它会死掉,并会为你打印出查询字符串。尝试将其直接复制到mysql中,并对结果进行评论。

function dbEscape($str) {
    return mysql_real_escape_string($str);
}
$sql = "INSERT INTO registration(file,name,fname,gender,email,date,mn,pn,address,idnum,ccname,colname,course,year,comname,doj)
    VALUES ('".dbEscape($uploadpath)."','".  dbEscape($_POST["name"])."','".  dbEscape($_POST['fname'])."','".  dbEscape($_POST['genred'])."','".  dbEscape($_POST['email'])."','".  dbEscape($_POST['date'])."','".  dbEscape($_POST['mn'])."','".  dbEscape($_POST['pn'])."','".  dbEscape($_POST['address'])."','".  dbEscape($_POST['idnum'])."','".  dbEscape($_POST['ccname'])."','".  dbEscape($_POST['colname'])."','".  dbEscape($_POST['course'])."','".  dbEscape($_POST['year'])."','".  dbEscape($_POST['comname'])."','".  dbEscape($_POST['doj'])."')";
die($sql);
$result = mysql_query($sql);

答案 2 :(得分:0)

您的表单不包含enctype="multipart/form-data",是上传文件的重要部分。

将表单修改为:

<form name="form" method="post" enctype="multipart/form-data" action="" onSubmit="submit;">

这就是您上传无效的原因。