我无法使用php将三个图像插入mysql数据库
我是php新手,这是我的第一个作品。实际上我已经创建了一个“插入产品”页面,它有三个图像。。。。此页面已连接到数据库。。。 当我插入三个图像时,它不会提示“产品已插入”,但当我删除最后两个图像时。。它成功地插入。。。 谢谢你的帮助。。 这是密码___________________我无法使用php将三个图像插入mysql数据库,php,file-upload,Php,File Upload,我是php新手,这是我的第一个作品。实际上我已经创建了一个“插入产品”页面,它有三个图像。。。。此页面已连接到数据库。。。 当我插入三个图像时,它不会提示“产品已插入”,但当我删除最后两个图像时。。它成功地插入。。。 谢谢你的帮助。。 这是密码___________________ <form action="insert_product.php" method="post" enctype="multipart/form-data"> &l
<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>";
}
}
}
?>
插入新产品:
产品名称
产品类别
选择一个类别
打印(美元文件)
多图像数组与您编写的不同
首先打印文件数组,代码中将出现错误
谢谢请确保文件大小在php设置的默认上载大小限制内。如果您的图像文件大于2M会导致问题,则php为upload_max_filesize设置的默认大小为“2M”。可能您尝试上载的其他两个文件大于2M。您遇到了什么错误?图片被上传了吗?文件没有被上传,而且当我插入三张图片时,它也不会回显“插入”…这不是答案。这可能是一封信非常感谢你。。。这就是问题所在。。我什么时候限制了它的大小成功插入…再次感谢
//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>";
}
}
}