我是php的新手,这是我的第一个工作......实际上我创建了一个“插入产品”页面,它有三个图像....这个页面连接到数据库... 当我插入三个图像时,它不会提示“插入产品”,但是当我删除最后两个图像时...它会成功插入... 谢谢您的帮助.. 这是代码___________________
<form action="insert_product.php" method="post" enctype="multipart/form-data">
<table width="700" align="center" border="1" bgcolor="#3398ce">
<tr align="center">
<td colspan="2"><h2>Insert New Product:</h2></td>
</tr>
<tr>
<td align="right"><b>Product Title</b></td>
<td><input type="text" name="product_title" size="50"/></td>
</tr>
<tr>
<td align="right"><b>Product Category</b></td>
<td>
<select name="product_cat">
<option>Select a Category</option>
<?php
$sel_cats ="select * from categories";
$run_cats =mysqli_query($con, $sel_cats)
or die("Error: ".mysqli_error($con));
while($row_cats=mysqli_fetch_array($run_cats)){
$cat_id =$row_cats['cat_id'];
$cat_title=$row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Brand</b></td>
<td>
<select name="product_brand">
<option>Select a Brand</option>
<?php
$sel_brands ="select * from brands";
$run_brands =mysqli_query($con, $sel_brands)
or die("Error: ".mysqli_error($con));
while($row_brands=mysqli_fetch_array($run_brands)){
$brand_id =$row_brands['brand_id'];
$brand_title=$row_brands ['brand_title'];
echo "<option value='$brand_id'>$brand_title</option>";
}
?>
</select>
</td>
</tr>
<tr> <td align="right"><b>Product Image 1</b></td>
<td><input type="file" name="product_img1"/></td>
</tr>
<tr>
<td align="right"><b>Product Image 2</b></td>
<td><input type="file" name="product_img2"/></td>
</tr>
<tr>
<td align="right"><b>Product Image 3</b></td>
<td><input type="file" name="product_img3"/></td>
</tr>
<tr>
<td align="right"><b>Product Price</b></td>
<td><input type="text" name="product_price" size="20"/></td>
</tr>
<tr>
<td align="right"><b>Product Description</b> </td>
<td><textarea name="product_desc" cols="37" rows="10"></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords</b></td>
<td><input type="text" name="product_keywords" size="80"/></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="insert_product" value="Insert" size="50"/></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include('includes/connect.php');
if(isset($_POST['insert_product'])){
//text data variables//
$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'];
$status = 'on';
$product_keywords=$_POST['product_keywords'];
//image data variables//
$product_img1 =$_FILES['product_img1']['name'];
$product_img2 =$_FILES['product_img2']['name'];
$product_img3 =$_FILES['product_img3']['name'];
//image temp names//
$temp_name1 =$_FILES['product_img1']['tmp_name'];
$temp_name2 =$_FILES['product_img2']['tmp_name'];
$temp_name3 =$_FILES['product_img3']['tmp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keywords=='' OR $product_img1==''){
echo "<script>alert('Please fill all the fields!')</script>";
exit();
}
else {
move_uploaded_file($temp_name1,"images/$product_img1");
move_uploaded_file($temp_name2,"images/$product_img2");
move_uploaded_file($temp_name3,"images/$product_img3");
$insert = "insert into products(cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status,product_keywords) values
('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status','$product_keywords')";
$run_product =mysqli_query($con, $insert);
if($run_product){
echo "<script>alert('Product Inserted')</script>";
}
}
}
?>
任何人都可以帮助解决语法错误..... 你能猜到这里的语法错误吗$ product_img1 = $ _ FILES ['product_img1'] ['name']; $ product_img2 = $ _ FILES ['product_img2'] ['name']; $ product_img3 = $ _ FILES ['product_img3'] ['name'];
//image size varibles//
$pro_img1_size = $_FILES['product_img1']['size'];
$pro_img2_size = $_FILES['product_img2']['size'];
$pro_img3_size = $_FILES['product_img3']['size'];
//image type variables//
$pro_img1_type = $_FILES['product_img1']['type'];
$pro_img2_type = $_FILES['product_img2']['type'];
$pro_img3_type = $_FILES['product_img3']['type'];
//image temp names//
$temp_name1 =$_FILES['product_img1']['tmp_name'];
$temp_name2 =$_FILES['product_img2']['tmp_name'];
$temp_name3 =$_FILES['product_img3']['tmp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keywords=='' OR $product_img1==''){
echo "<script>alert('Please fill all the fields!')</script>";
exit();
}
if($pro_img1_type=="product_img1/jpeg" OR $pro_img1_type=="product_img1/png" OR $pro_img1_type=="product_img1/gif" OR $pro_img2_type=="product_img2/jpeg" OR $pro_img2_type=="product_img2/png" OR $pro_img2_type=="product_img2/gif" OR $pro_img3_type=="product_img3/jpeg" OR $pro_img3_type=="product_img3/png" OR $pro_img3_type=="product_img3/gif")
{
if($pro_img1_size<=100000 AND $pro_img2_size<=100000 AND $pro_img3_size<=100000 ){
move_uploaded_file($temp_name1,"pro_images/$product_img1");
move_uploaded_file($temp_name2,"product_images/$product_img2");
move_uploaded_file($temp_name3,"product_images/$product_img3");
}
else {
echo "<script>alert('Image is larger, only 100kb size is allowed')</script>";
exit();
}
echo "<script>alert('Image type is invalid')</script>";
exit();
}
else {
$insert = "insert into products(cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status,product_keywords) values
('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status','$product_keywords')";
$run_product =mysqli_query($con, $insert);
if($run_product){
echo "<script>alert('Product Inserted')</script>";
}
}
}
答案 0 :(得分:0)
的print_r($ _ FILES);
多个图像数组与您编写的不一样。
首先打印文件数组,您将在代码中收到错误。
由于
答案 1 :(得分:0)
确保文件大小在php设置的默认上传大小限制范围内。 php为upload_max_filesize设置的默认大小为&#34; 2M&#34;如果您的图像文件大于2M,则会导致问题。您尝试上传的其他两个文件可能大于2M。