2016-06-07 7 views
0

phpcode를 사용하여 양식에서 데이터를 가져 와서 데이터베이스에 업로드합니다. 그러나 데이터는 데이터베이스에 저장되지 않습니다. 여기 'insert_product.php'는 전체 코드가있는 파일이며 버튼을 클릭하면 양식이 동일한 페이지로 리디렉션됩니다. 누구든지 도울 수 있니? 이 전체 코드 인 경우제출 된 데이터가 데이터베이스에 업로드되지 않았습니다.

<form action="insert_product.php" method="post" enctype="multipart/form-data"> 

     <table align="center" width="795" border="2" bgcolor="#187eae"> 

      <tr align="center"> 
       <td colspan="7"><h2>Insert New Post Here</h2></td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Title:</b></td> 
       <td><input type="text" name="product_title" size="60" required/></td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Category:</b></td> 
       <td> 
       <select name="product_cat" > 
        <option>Select a Category</option> 
        <option value="1">Laptop</option> 
        <option value="2">Computer</option> 
       </select> 


       </td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Brand:</b></td> 
       <td> 
       <select name="product_brand" > 
        <option>Select a Brand</option> 
        <option value="1">LG</option> 
        <option value="2">Samsung</option> 
       </select> 


       </td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Image:</b></td> 
       <td><input type="file" name="product_image" /></td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Price:</b></td> 
       <td><input type="text" name="product_price" required/></td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Description:</b></td> 
       <td><textarea name="product_desc" cols="20" rows="10"></textarea></td> 
      </tr> 

      <tr> 
       <td align="right"><b>Product Keywords:</b></td> 
       <td><input type="text" name="product_keywords" size="50" required/></td> 
      </tr> 

      <tr align="center"> 
       <td colspan="7"><input type="submit" name="insert_post" value="Insert Product Now"/></td> 
      </tr> 

     </table> 


    </form> 


</body> 
</html> 
<?php 

    if(isset($_POST['insert_post'])){ 

     //getting the text data from the fields 
     $product_title = $_POST['product_title']; 
     $product_cat= $_POST['product_cat']; 
     $product_brand = $_POST['product_brand']; 
     $product_price = $_POST['product_price']; 
     $product_desc = $_POST['product_desc']; 
     $product_keywords = $_POST['product_keywords']; 

     //getting the image from the field 
     $product_image = $_FILES['product_image']['name']; 
     $product_image_tmp = $_FILES['product_image']['tmp_name']; 

     move_uploaded_file($product_image_tmp,"product_images/$product_image"); 

     $insert_product = "insert into products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) values ('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keywords')"; 

     $insert_pro = mysqli_query($con, $insert_product); 

     if($insert_pro){ 

     echo "<script>alert('Product Has been inserted!')</script>"; 
     echo "<script>window.open('index.php?insert_product','_self')</script>"; 

     } 
    } 








?> 
+1

Hoo boy, 이것은 취약한 코드입니다! 변수를 SQL 쿼리에 직접 삽입하는 것은 매우 위험합니다! – Ben

+0

코드에 몇 가지 실수가 있습니다. 1) 삽입하기 전에 사용자가 제출 한 데이터를 확인하거나 이스케이프 처리하지 않습니다. 2) 닫는 HTML 태그 뒤에 html을 반향해서는 안됩니다. –

+0

데이터베이스 연결을 확인하십시오. –

답변

0

당신과 같이 첫 번째 데이터베이스에 연결해야 거기에 어떤 쿼리를 실행하기 전에 데이터베이스에 연결해야 분명히 $con = mysqli_connect(...) 뭔가를 찾을 수 없습니다 :

$con = mysqli_connect("localhost","my_user","my_password","my_db"); 

데이터베이스 연결에 대한 추가 정보 here. 그리고 제가 당신이라면 먼저 연결이 작동하고 데이터를 테이블에 삽입 한 다음 파일 업로드에 대한 정보를 잘 파악해야만 간단하게 시작할 수 있습니다.

관련 문제