我正在创建一个程序,人们可以在其中插入带有图像的任何产品。我面临的问题是,如果我输入产品详细信息,并且如果我点击提交按钮而不上传图片,它会插入记录。
如果用户没有上传任何图像,我希望它不插入记录。简而言之,我也想对图像使用验证。我已经将 (isset) 与产品字段一起使用,但是当我将 (isset) 与 Image 一起使用时,它会给我以下错误消息,如果我只是删除 isset($_POST['files[]']) 它工作得很好,但它会停止验证图像字段。
我在这里分享代码。守则相当冗长。任何帮助,将不胜感激。
请填写所有字段
silverbrooches.jpg
您的 SQL 语法有误;
检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行Gold_Ring2.png的 ''silverbrooches.jpg'、'upload/products/32046silverbrooches.jpg')' 附近使用正确的语法您的 SQL 语法有错误;
检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行Necklaces_Diamond.png的 ''Gold_Ring2.png'、'upload/products/207Gold_Ring2.png')' 附近使用正确的语法您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 ''Necklaces_Diamond.png'、'upload/products/6748Necklaces_Diamond.png')' 附近使用正确的语法
<?php
//Use Category_id insted of Sub_category_id
session_start();
if (isset ($_SESSION['username']) )
{
//echo "<div id='nav'";
echo "<ul><hr>
<li><a href='insert_product.php' >Add Product </a></li>
<li><a href='add_category.php'> Add Category </a></li>
<li><a href='add_sub_category.php'> Add Sub-Category </a></li>
<li><a href = 'view_products.php' >View All Products</a> </li>
<li><a href = 'all_categories.php' >View All Categories</a> </li>
<li><a href='view_all_sub_categories.php'>View All Sub Categories</a></li>
</ul></hr>";
error_reporting(E_PARSE); //To Remove Notices!!
global $current_id;
//$GLOBALS $current_id;
if(isset($_SESSION['username']))
{
include 'connect.php';
// $select_query= 'Select * from category';
// $select_query_run = mysql_query($select_query);
echo "<div id='line' >
<div id='form'>";
echo "
<form action='insert_product.php' method='POST' enctype='multipart/form-data' >
<table border=1>
<tr>
<td>
<label>Product Name:</label> </td> <td><input type='text' name='product_name' />*Required</td></tr>
<tr><td><label>Item No:</label></td> <td><input type='text' name='item_no' ></td></tr>
<tr><td>Recipient </td> <td> <input type='text' name='recipient' ></td></tr>
<tr><td> Total Carat Weight</td> <td><input type='text' name= 'total_carat_weight' ></td></tr>
<tr><td> Metal </td><td><input type='text' name='metal' ></td></tr>
<tr><td> Stone Shape </td><td><input type='text' name='stone_shape' ></td></tr>
<tr><td> Stone Type</td><td> <input type='text' name='stone_type'></td></tr>
<tr><td> Stone Setting</td><td> <input type='text' name='stone_setting'></td></tr>
<tr><td> Wastage </td><td><input type='text' name='wastage'></td></tr></br></br>
<tr><td> Retail_price </td><td><input type='text' name='retail_price' ></td></tr>
<tr><td> Actual Price: </td><td> <input type= 'text' name= 'price' /></td></tr>*Required
<tr><td> Description:</td><td><input type='text' name='description' /></td></tr>
<tr><td> Image1:</td><td> <input type='file' name= 'files[]' ></td></tr> *Required
<tr><td> Image2:</td><td> <input type='file' name= 'files[]' ></td></tr>
<tr><td> Image3:</td><td> <input type='file' name= 'files[]' ></td></tr></table> ";
/*------------------
Drop Down List Start
------------------ */
/*----------------
Drop Down List
---------------*/
echo "<select name='category'>";
$select_query= 'Select * from category';
$select_query_run = mysql_query($select_query);
$sub_category_query="Select * from sub_categories
where category_id='".$select_query_array['category_id']."'";
// $sub_category_query="Select * from sub_categories ";
// $sub_query_run= mysql_query($sub_category_query);
while ($select_query_array= mysql_fetch_array($select_query_run) ) {
echo "<optgroup label='".$select_query_array['name']."' >".
//$sub_category_query="Select * from sub_categories";
$sub_category_query="Select * from sub_categories
where category_id='".$select_query_array['category_id']."'";
$sub_query_run= mysql_query($sub_category_query);
while ($sub_query_run_fetch= mysql_fetch_array($sub_query_run) ) {
echo "<option value='".$sub_query_run_fetch['sub_category_id'] . "' >" .
htmlspecialchars($sub_query_run_fetch['sub_category_name']) . "</option>";
}
echo "</optgroup>";
}
echo "</br>";
$selectTag= "</br><input type='submit' value='Insert Product' /></select></form>";
echo "</div></div>";
echo $selectTag;
/*----------------
Drop Down List
---------------*/
/*-----------------
Drop Down List End
------------------*/
if(isset($_POST['product_name']) && isset($_POST['price']) && isset($_POST['description'] )
&& isset($_POST['files[]'])
)
{
$product_name = $_POST['product_name'];
$price = $_POST['price'];
$description = $_POST['description'];
$category = $_POST['category'];
$query= "insert into products (name, sub_category_id )
VALUES( '$product_name', $category )";
if($query_run= mysql_query($query) )
{
echo 'Data Inserted';
$current_id= mysql_insert_id();
//$_SESSION['current_id']= mysql_insert_id();
}
else
{
'Error In SQL'.mysql_error();
}
/*-----------------
INSERT Description!!
-------------------*/
$item_no= $_POST['item_no'];
$recipient= $_POST['recipient'];
$total_carat_weight= $_POST['total_carat_weight'];
$metal= $_POST['metal'];
$stone_shape= $_POST['stone_shape'];
$stone_type = $_POST['stone_type'];
$stone_setting = $_POST['stone_setting'];
$wastage = $_POST['wastage'];
$retail_price = $_POST['retail_price'];
$query_description= "insert into product_description VALUES($current_id,'$item_no','$recipient',
'$total_carat_weight','$metal','$stone_shape','$stone_type','$stone_setting',
'$wastage','$retail_price','$price' ,'$description' ) ";
if(mysql_query($query_description))
{
echo 'Insert descrition successfull';
}
else
{
echo mysql_error();
}
}
else
{
echo '</br>Plesae fill all the Fields';
}
/*-----------------
IMAGE QUERY 2
------------------*/
if (isset($_FILES['files'])
|| ($_FILES["files"]["type"] == "image/jpeg"))
{
foreach($_FILES['files']['tmp_name'] as $key=> $tmp_name)
{
//echo $tmp_name."<br>";
echo $image_name= $_FILES["files"]["name"][$key];
$random_name= rand().$_FILES["files"]["name"][$key];
$folder="upload/products/" .$random_name;
move_uploaded_file($_FILES["files"]["tmp_name"][$key],
"upload/products/" . $random_name);
// print_r($_FILES);
$sql = "Insert into product_images (product_id,name,images)
VALUES ($current_id,'$image_name', '$folder')";
if (mysql_query($sql))
{
echo 'Done';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY 2- END
------------------*/
}
else
{
echo 'You Must Log in To View this Page!';
}
}
else
{
echo "You Must need to login to View this Page";
}
?>