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>";
}
}
?>
Hoo boy, 이것은 취약한 코드입니다! 변수를 SQL 쿼리에 직접 삽입하는 것은 매우 위험합니다! – Ben
코드에 몇 가지 실수가 있습니다. 1) 삽입하기 전에 사용자가 제출 한 데이터를 확인하거나 이스케이프 처리하지 않습니다. 2) 닫는 HTML 태그 뒤에 html을 반향해서는 안됩니다. –
데이터베이스 연결을 확인하십시오. –