2014-10-15 2 views
-2

데이터베이스에 이미지를 업로드 할 수 없습니다. 양식을 제출 한 후 다음 오류가 표시됩니다.데이터베이스에 이미지를 삽입 할 수 없습니다.

There was an error uploading the data, please try again!

내 코드는 다음과 같습니다.

<?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> 
+2

왜 이미지를 데이터베이스로 업로드하려고합니까 ** 데이터를 가져 오는 데 ** 사이트 성능이 저하됩니다. 대신 img를 dir에 저장하고 참조를 데이터베이스에 저장할 수 있습니다. – Ronser

+0

분명히 실패하고있다. 준비된 쿼리를 사용하지 않고, 오타가있을 수 있습니다. MYSQLI 오류보고를 사용하십시오. (준비된 명령문으로 전환 할 때) – Sablefoste

+0

어떻게해야 할 지 말해 줄 수 있습니다. – vyuser

답변

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); 
     } 
    } 
    ?> 
+0

dint worked .... – vyuser

0

1. mysql_ * 함수를 사용하지 말고 대신 mysqli_ *를 사용하십시오.

2nd. 모든 데이터를 수락하면 매우 큰 보안 허점입니다.

3rd. 왜 이런 변수를 만드나요? $ name = $ _ POST [ "name"];

편집 :

이 시도는 $_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); 
+0

내가 세부 정보 코드를 제공 할 수는 있지만, dint는 – vyuser

+0

으로 편집 코드를 보았고, 내가 요청한 결과와 SQL 쿼리를 게시하십시오. – vaso123

0

양식에 enctype="multipart/form-data"이 포함되어 있지 않으며 파일을 업로드 할 때 필수적인 부분입니다.

은 읽을 양식을 수정 업로드가 작동하지 않는 이유입니다

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

합니다.

관련 문제