if(isset($_POST['upload'])){
$name = $_POST['name'];
$album_id = $_POST['album'];
$file = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$random_name = rand();
if(empty($name) or empty($file)) {
echo "Please Fill all the Fields ! <br /><br />";
} else {
move_uploaded_file$file_tmp, 'uploads/'.$random_name.'.jpg');
mysql_query("INSERT INTO photos VALUE('', '$name', '$album_id', '$random_name.jpg')");
echo "Photo Uploaded !! <br /><br />";
}
}
This is how i managed to do it hope this helps
<?
function openDB()
{
global $conn, $username,$host,$password,$db;
$host = "localhost";
$username ="username";
$password= "password";
$db = "databasename";
$conn = mysql_connect($host, $username,$password) or die(mysql_error());
mysql_select_db($db,$conn) or die(mysql_error());
}
function closeDB()
{
global $conn;
mysql_close($conn);
}
?>
<form action="<? echo $_SERVER[PHP_SELF];?>" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="left"><b>Product Image:</b></td>
<td><input type="file" accept="image/*" name="product_image" size="60">
</td>
</tr>
<tr>
<td align="left"><b>Product File1:</b></td>
<td><input type="file" accept="*/*" name="product_support_file" size="60">
</td>
</tr>
<tr>
<td align="left"><b>Product File2:</b></td>
<td><input type="file" accept="*/*" name="product_support_file2" size="60">
</td>
</tr>
<tr>
<td align="left"><b>Product Price:</b></td>
<td><input type="text" name="product_price" size="60" required></td>
</tr>
<tr>
<td colspan="7" align="right"><input type="submit" name="insert_product" value="Insert Product Now"></td></tr>
</table>
</form>
<?
openDB();
$product_id = $_POST['product_id'];
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_type = $_POST['product_type'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
$product_link = $_POST['product_link'];
//getting the image from the feild
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp'];
$product_support_file = $_FILES['product_support_file']['name'];
$product_support_file_tmp = $_FILES['product_support_file']['tmp'];
$product_support_file2 = $_FILES['product_support_file2']['name'];
$product_support_file2_tmp = $_FILES['product_support_file2']['tmp'];
if(isset($_POST['insert_product']))
{
//gets the Text Feild Data
$insert_product = "insert into products(product_id,product_cat, product_type, product_title,
product_price, product_desc, product_image, product_support_file, product_support_file2, product_keywords,product_link)
values('','$product_cat','$product_type','$product_title',
'$product_price','$product_desc','$product_image','$product_support_file','$product_support_file2','$product_keywords','$product_link')";
if(mysql_query($insert_product))
{
//echo "
//Please enter the file path you wish to add your image to:
//<br><input type='text' name='' value=''></input>";
$file_directory ="../yourdirectory";
foreach($_FILES as $file_name => $file_array)
{
if(is_uploaded_file($file_array["tmp_name"]))
{
move_uploaded_file($file_array["tmp_name"],"$file_directory/"
.$file_array["name"]) or die ("Could not copy");
}
}
echo"<script language='Javascript'>window.open('ConformationUpload.php');</script>";
}
else{
echo"Product adding has been unsuccessfull<b>",mysql_error(),"</b>";
}
closeDB();
}
?>
Related
I am getting this error when I run the below code. I dont know why I am getting this error. I echoed the query to make sure that the syntax is correct I checked the query in a syntax checker to make sure that the query contains no syntax errors. and even tested in PHP my admin.
The query that is producing this error is the query that is in the for loop with numberofcolor and numberofsizes
Error description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
<?php include('includes/db.php'); include('../functions/functions.php');?>
<?php require './p_struct/head.php';?>
<?php require './p_struct/header.php';?>
<?php require './p_struct/sidenav.php';?>
<div class="main" style="font-size: 20px">
<form action="insert_product.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="chells">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="product_title" required="required"></td>
</tr>
<tr>
<td>category</td>
<td><select name="product_category" >
<option>Select a Category</option>
<?php
$get_cats = " select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while($row_cats = mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_data'];
echo "
<option vallue = '$cat_id' >$cat_title </option>
";
}
?>
</select></td>
</tr>
<tr>
<td>featured</td>
<td><select name="product_featured" >
<option>featured</option>
<option>not featured</option>
</select></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" name="product_price" required="required"></td>
</tr>
<tr>
<td>image</td>
<td><input type="file" name="product_image" required="required"></td>
</tr>
<tr>
<td>big image</td>
<td><input type="file" name="product_big_image" required="required"></td>
</tr>
<tr>
<td>cart image</td>
<td><input type="file" name="product_cart_image" required="required"></td>
</tr>
<tr>
<td>keywords</td>
<td><input type="text" name="product_keywords" required="required"></td>
</tr>
<tr>
<td> Colors</td>
<td>
<div class="checkbox">
<?php
$get_colors = "SELECT * FROM `product_colors`";
$run_get_colors = mysqli_query($con, $get_colors);
while($row_get_color = mysqli_fetch_array($run_get_colors))
{
$colorS = $row_get_color['color'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mycolor[]" value="'.$colorS.'" >'.$colorS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td> Sizes</td>
<td>
<div class="checkbox">
<?php
$get_sizes = "SELECT * FROM `product_sizes`";
$run_get_sizes = mysqli_query($con, $get_sizes);
while($row_get_sizes = mysqli_fetch_array($run_get_sizes))
{
$sizesS = $row_get_sizes['size'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mysizes[]" value="'.$sizesS.'" >'.$sizesS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td>description</td>
<td><textarea name="product_description" cols="20" rows="5" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Add Product" name="insert_post"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
global $con;
if( isset($_POST['insert_post']))
{
$a = rand(100, 999);
$b = rand(100, 999);
$product_id = $a.$b;
$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
$color_array = $_POST['mycolor'];
$sizes_array = $_POST['mysizes'];
$product_image = $_FILES['product_image']['name'];
$product_big_image = $_FILES['product_big_image']['name'];
$product_cart_image = $_FILES['product_cart_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
$product_big_image_tmp = $_FILES['product_big_image']['tmp_name'];
$product_cart_image_tmp = $_FILES['product_cart_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
move_uploaded_file($product__big_image_tmp,"product_big_images/$product_big_image");
move_uploaded_file($product_cart__image_tmp,"product_cart_images/$product_cart_image");
$insert_product = " insert into products (product_id,product_cat, product_featured, product_title, product_price, product_desc,product_image,product_big_image,product_cart_image,product_keywords) values ($product_id,'$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_big_image','$product_cart_image','$product_keywords')";
$run_insert_query = mysqli_query($con, $insert_product);
$number_of_colors = count($color_array);
for($i=0; $i <$number_of_colors; $i++)
{
$curr_color_value = $color_array[$i];
$add_color_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'color','$curr_color_value');";
$run_add_color_att = mysqli_query($con, $add_color_att);
/* if($run_add_color_att)
{
echo "COLOR ADDED!";
}
else {
echo "COLOR NOT ADDED!";
}*/
}
$number_of_sizes = count($sizes_array);
for($s=0; $s <$number_of_sizes; $s++)
{
$curr_size_value = $number_of_sizes[$s];
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
$run_add_size_att = mysqli_query($con, $add_size_att);
/* if($run_add_size_att)
{
echo "SIZE ADDED!";
}
else {
echo "SIZE NOT ADDED!";
}*/
}
if($run_insert_query)
{
echo "PRODUCT ADDED!";
}
else {
echo "PRODUCT NOT ADDED!";
}
} ?>
</div>
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
your using the incorrect quote type here the ` should be ' around the string values:
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'size','$curr_size_value')";
also should size be $size?
Code:
<!DOCTYPE html >
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
</head>
<body>
<form action="insert_product.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="product_title" required="required"></td>
</tr>
<tr>
<td>category</td>
<td><select name="product_category" >
<option>Select a Category</option>
<?php
$get_cats = " select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while($row_cats = mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_data'];
echo "
<option>$cat_title </option>
";
}
?>
</select></td>
</tr>
<tr>
<td>featured</td>
<td><select name="product_featured" >
<option>0</option>
<option>1</option>
</select></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" name="product_price" required="required"></td>
</tr>
<tr>
<td>image</td>
<td><input type="file" name="product_image" required="required"></td>
</tr>
<tr>
<td>keywords</td>
<td><input type="text" name="product_keywords" required="required"></td>
</tr>
<tr>
<td>description</td>
<td><textarea name="product_description" cols="20" rows="10" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Add Product" name="insert_post"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if( isset($_POST['insert_post']))
{
$product_title = $_POST['$product_title'];
$product_category = $_POST['$product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['$product_price'];
$product_keywords = $_POST['$product_keywords'];
$product_description = $_POST['$product_description'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
echo $insert_product = " insert into products (product_cat, product_featured, product_title, product_price, product_desc,product_image,product_keywords) values ('$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_keywords','$product_keywords')";
}
?>
this is the result I get from this code when I print it with echo
insert into products (product_cat, product_featured, product_title, product_price, product_desc,product_image,product_keywords) values ('','1','','','','champagne culture logo men.png','','')
can someone please tell me what I'm doing wrong? I've checked repeatedly checked to see if i had any spelling errors or anything of that nature but I cant seem to identify exactly what it is that I am doing wrong. Any help you could provide would be greatly appreciated
Remove the $ part from the key:
$product_title = $_POST['$product_title'];
$product_category = $_POST['$product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['$product_price'];
$product_keywords = $_POST['$product_keywords'];
$product_description = $_POST['$product_description'];
Should be
$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
You need to change this
$product_title = $_POST['$product_title'];
$product_category = $_POST['$product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['$product_price'];
$product_keywords = $_POST['$product_keywords'];
$product_description = $_POST['$product_description'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
to this
$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
NOTE
if there was no error, you need to make sure if the input is not empty, you can use empty() property to check if the variable/input is empty.
like
if(empty($_POST['product_title'])) {
echo "product title shouldn't empty";
}
I can all input type values view in my update page but, I can't display file input value for ex: photo. Here I can be displayed image location value in a separate part.
My problem for
When I update the only name or mother name or anyone without photo all fields values update correctly but my image is updated empty values. when I update with a photo and other details then only my photo updated correctly. so help me how to update photo values with edit without edit.....
update.php
<!doctype html>
<html>
<body>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$upd = $_GET['upd'];
mysql_connect('localhost','root','');
mysql_select_db("display");
$slc = "SELECT * from photos WHERE firstname = '$upd'";
$run = mysql_query($slc);
while($row=mysql_fetch_array($run)){
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$location=$row['location'];
}
?>
<div class="update">
<table align="center" border="">
<tr>
<td colspan="4"><center><h1 style="color:red">Student Form Updation</h1>
</center></td></tr>
<form method="post" enctype="multipart/form-data" action="update.php?upd=<?
php echo $firstname; ?> "/>
<tr><td><label>First Name:</label></td><td>
<input type="text" name="firstname" value="<?php echo $firstname ?> " />
</td>
<td><label>Last Name:</label></td><td>
<input type="text"name="lastname" value="<?php echo $lastname?>" /></td>
</tr>
<td><label>Select Photo</label></td>
<td> <input type="file" name="image" class="ed" id="location" value= "<?php
echo $location?>"/> <?php echo '<img width="100px" height="100px"
src="'.$location.'">'; ?>
</td>
</tr>
<tr><td></td><td><input type="submit" name="update" value="Update"
id="button1" /></td>
<td><input type="submit" formaction="errview.php" value="View" id="button1"
/></td><td></td></tr>
</form>
</div>
<?php
if(isset($_POST['update']))
{
$nid = $_GET['upd'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$file=$_FILES['image']['tmp_name'];
$image=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]
["name"]);
$location="photos/" . $_FILES["image"]["name"];
$update = "UPDATE photos SET firstname='$firstname', lastname =
'$lastname',location='$location' WHERE firstname='$nid'";
$run = mysql_query($update);
echo "<script>alert('Update SuccessFull!');location.href='update.php?
upd=$firstname'</script>";
}
?>
</body>
</html>
You Can Modify Your Update Code Like This:
If no image is selected It will not Update Your Image in Database It Will Only Update Your Image in the database if an image is selected
<?php
if (isset($_POST['update'])) {
$nid = $_GET['upd'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"], "photos/" . $_FILES["image"]["name"]);
$location = "photos/" . $_FILES["image"]["name"];
if ((!($_FILES['image']['name']))) /* If there Is No file Selected*/ {
$update = "UPDATE photos SET firstname='$firstname', lastname =
'$lastname' WHERE firstname='$nid'";
} else /* If file is Selected*/ {
$update = "UPDATE photos SET firstname='$firstname', lastname =
'$lastname',location='$location' WHERE firstname='$nid'";
}
$run = mysql_query($update);
echo "<script>alert('Update SuccessFull!');location.href='update.php?
upd=$firstname'</script>";
}
?>
<?php
if(empty($image)){
$product_img1=$row_edit['image'];
}
else{
$temp_name1=$_FILES['product_img1']['tmp_name'];
move_uploaded_file($temp_name1,"product_images/$product_img1");
}
?>
I am new in php. when form is submitted the data is not storing in database not getting what is wrong in code. Below is my code
<form method="post" action="insert_product.php" enctype="multipart/form-data">
<table width="700" align="center" >
<tr>
<td><h1>Insert New Product</h1></td>
</tr>
<tr>
<td><b>Product Title</b></td>
<td><input type="text" name="product_title"></td>
</tr>
<tr>
<td><b>Product Category</b></td>
<td>
<select name="product_cat">
<option>Select Category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con , $get_cats);
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><b>Product Brand</b></td>
<td>
<select name="product_brand">
<option>Select Brand</option>
<?php
$get_brands = "select * from brands";
$run_brands = mysqli_query($con , $get_brands);
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><b>Product Image1</b></td>
<td><input type="file" name="product_img1"></td>
</tr>
<tr><td><b>Product Image2</b></td>
<td><input type="file" name="product_img2"></td>
</tr>
<tr><td><b>Product Image3</b></td>
<td><input type="file" name="product_img3"></td>
</tr>
<tr>
<td><b>Product Price</b></td>
<td><input type="text" name="product_price"></td>
</tr>
<tr>
<td><b>Product Info</b></td>
<td> <textarea name="product_desc">Easy! You should check out MoxieManager!</textarea></td>
</tr>
<tr>
<td><b>Product Keyword</b></td>
<td><input type="text" name="product_keyword"></td>
</tr>
<tr>
<td><input type="submit" name="insert_product" value="Insert"></td>
</tr>
</table>
</form>
and php code of it
<?php
if (isset($_POST['insert_product'])) {
$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_keyword = $_POST['product_keyword'];
//image names//
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
//temp image 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_keyword=='' OR $product_img1=='' ) {
echo "<script>alert('Please Fill Everything')</script>";
exit();
}
else {
//uploading images to its folder
move_uploaded_file($temp_name1, "product_images/$product_img1");
move_uploaded_file($temp_name2, "product_images/$product_img2");
move_uploaded_file($temp_name3, "product_images/$product_img3");
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status)
values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status')";
$run_product = mysqli_query($con, $insert_product);
if ($run_product) {
echo "<script>alert('Product Inserted')</script>";
}
}
}
?>
Till alert(fill everything) is working means when i click submit without filling form the alert is coming but after that when i filled everything and submit it. But alert is not coming and also data is not saving in database. Please help me. adding my database structure also. Thanks
i'm trying to save image in database and images folder in same time, the image it's saving in images folder but not in database can someone please help i don't know what im doing wrong.
add.php
<?php
if (!defined('WEB_ROOT')) {
exit;
}
$errorMessage = (isset($_GET['error']) && $_GET['error'] != '') ? $_GET['error'] : ' ';
?>
<script>
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCategory(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('catdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
<div class="content-box-header">
<h3>Add New Restaurant</h3>
</div>
<form action="processTopping.php?action=add" method="post" enctype="multipart/form-data" name="frmAddTopping" id="frmAddTopping">
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
<tr>
<td width="150" class="label">Restaurant Name</td>
<td class="content"><input name="txtName" class="text-input large-input" type="text" id="txtName" /></td>
</tr>
<tr>
<td class="label">Restaurant Email</td>
<td class="content"><input name="txtEmail" class="text-input large-input" type="text" id="txtEmail" /></td>
</tr>
<tr>
<td class="label">Restaurant Phone</td>
<td class="content"><input name="txtPhone" class="text-input large-input" type="text" id="txtPhone" /></td>
</tr>
<tr>
<td class="label">Restaurant Address</td>
<td class="content"><input name="txtAddress" class="text-input large-input" type="text" id="txtAddress" /></td>
</tr>
<tr>
<td class="label">Restaurant Cuisine</td>
<td class="content"><input name="txtCuisine" class="text-input large-input" type="text" id="txtCuisine" /></td>
</tr>
<tr>
<td class="label">Restaurant Mini Order</td>
<td class="content"><input name="txtMini_order" class="text-input large-input" type="text" id="txtMini_order" /></td>
</tr>
<tr>
<td class="label">Restaurant Suburbs</td>
<td class="content"><input name="txtSuburbs" class="text-input large-input" type="text" id="txtSuburbs" /></td>
</tr>
<tr>
<td width="150" class="label">Restaurant Menu</td>
<td class="content"><input name="txtMenu" class="text-input large-input" type="text" id="txtMenu" /></td>
</tr>
<tr>
<td class="label">Restaurant Reviews</td>
<td class="content"><input name="txtReviews" class="text-input large-input" type="text" id="txtReviews" /></td>
</tr>
<tr>
<td class="label">Restaurant Logo</td>
<td class="content"><input name="txtLogo" class="text-input large-input" type="text" id="txtLogo" /></td>
</tr>
<tr>
<td class="label">Restaurant Takeaway Hours</td>
<td class="content"><input name="txtTakeawayHours" class="text-input large-input" type="text" id="txtTakeawayHours" /></td>
</tr>
<tr>
<td class="label">Restaurant Delivery Hours</td>
<td class="content"><input name="txtDeliveryHours" class="text-input large-input" type="text" id="txtDeliveryHours" /></td>
</tr>
<tr>
<td class="label">Restaurant Map</td>
<td class="content"><input name="txtMap" class="text-input large-input" type="text" id="txtMap" /></td>
</tr>
<tr>
<td class="label">Restaurant Promotions</td>
<td class="content"><input name="txtPromotions" class="text-input large-input" type="text" id="txtPromotions" /></td>
</tr>
<!-- <tr>
<td width="150" class="label">Image</td>
<td class="content"> <input name="fleImage" type="file" id="fleImage" class="box"> </td>
</tr>-->
<tr>
<td class="label" valign="middle">Images</td>
<td class="label">
<div id="main">
<table width="230" cellpadding="0">
<tr>
<td width="232" align="right">
<input type="file" name="fleImage[]" /></td>
</tr>
</table>
</div></td>
</tr>
</table>
<p align="center">
<input name="btnAddTopping" type="button" id="btnAddTopping" value="Add Restaurant" onClick="checkAddToppingForm();" class="button">
<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="button">
</p>
</form>
processTopping.php
<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
error_reporting(E_ALL);
checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'add' :
addTopping();
break;
case 'modify' :
modifyTopping();
break;
case 'delete' :
deleteTopping();
break;
default :
// if action is not defined or unknown
// move to main Topping page
header('Location: index.php');
}
function addTopping()
{
$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$phone = $_POST['txtPhone'];
$address = $_POST['txtAddress'];
$cuisine = $_POST['txtCuisine'];
$mini_order = $_POST['txtMini_order'];
$suburbs = $_POST['txtSuburbs'];
$menu = $_POST['txtMenu'];
$reviews = $_POST['txtReviews'];
$logo = $_POST['txtLogo'];
$takeaway_hours = $_POST['txtTakeawayHours'];
$delivery_hours = $_POST['txtDeliveryHours'];
$map = $_POST['txtMap'];
$promotions = $_POST['txtPromotions'];
$sql = "INSERT INTO restaurant_info(name, email, phone, address, cuisine, mini_order, suburbs, menu, reviews, logo, takeaway_hours, delivery_hours, map, promotions)
VALUES ('$name','$email','$phone','$address','$cuisine','$mini_order','$suburbs','$menu','$reviews','$logo','$takeaway_hours','$delivery_hours','$map','$promotions')";
dbQuery($sql);
header('Location: index.php');
}
//image
$uploadDir = '../../images/';
for($i=0;$i<count($_FILES["fleImage"]["name"]);$i++)
{
if($_FILES["fleImage"]["tmp_name"][$i] != '')
{
$image = $_FILES["fleImage"];
if (trim($image['tmp_name'][$i]) != '') {
$ext = substr(strrchr($image['name'][$i], "."), 1); //$extensions[$image['type']];
// generate a random new file name to avoid name conflict
$imagePath = md5(rand() * time()) . ".$ext";
list($width, $height, $type, $attr) = getimagesize($image['tmp_name'][$i]);
// make sure the image width does not exceed the
// maximum allowed width
if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
$result = createThumbnail($image['tmp_name'][$i], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
$imagePath = $result;
} else {
$result = move_uploaded_file($image['tmp_name'][$i], $uploadDir . $imagePath);
}
if ($result) {
// create thumbnail
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createFixThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH,THUMBNAIL_HEIGHT);
// create thumbnail failed, delete the image
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
// the product cannot be upload / resized
$imagePath = $thumbnailPath = '';
}
}
mysql_query("INSERT INTO restaurant_info(Rest_Logo)
VALUES ('$Rest_Logo')";
}
}
/*
Modify a Topping
*/
function modifyTopping()
{
$toppingId = (int)$_POST['hidToppingId'];
$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$phone = $_POST['txtPhone'];
$address = $_POST['txtAddress'];
$cuisine = $_POST['txtCuisine'];
$mini_order = $_POST['txtMini_order'];
$suburbs = $_POST['txtSuburbs'];
$menu = $_POST['txtMenu'];
$reviews = $_POST['txtReviews'];
$logo = $_POST['txtLogo'];
$takeaway_hours = $_POST['txtTakeawayHours'];
$delivery_hours = $_POST['txtDeliveryHours'];
$map = $_POST['txtMap'];
$promotions = $_POST['txtPromotions'];
$sql = "UPDATE restaurant_info
SET name='$name', email='$email', phone='$phone', address='$address', cuisine='$cuisine', mini_order='$mini_order', suburbs='$suburbs', menu='$menu', reviews='$reviews', logo='$logo', takeaway_hours='$takeaway_hours', delivery_hours='$delivery_hours', map='$map', promotions='$promotions'
WHERE rest_id = $toppingId";
dbQuery($sql);
header('Location: index.php');
}
/*
Remove a topping
*/
function deleteTopping()
{
if (isset($_GET['toppingId']) && (int)$_GET['toppingId'] > 0) {
$toppingId = (int)$_GET['toppingId'];
} else {
header('Location: index.php');
}
$sql = "DELETE FROM restaurant_info
WHERE rest_id = $toppingId";
dbQuery($sql);
header('Location: index.php');
}
?>
To save the file in database, you need to fetch content of file & save that in DB. You should choose datatype as longblob.
This can help you: http://forums.mysql.com/read.php?20,17671,27914
i just fixed it, i had to do some changes to the sql code.
<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
error_reporting(E_ALL);
checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'add' :
addTopping();
break;
case 'modify' :
modifyTopping();
break;
case 'delete' :
deleteTopping();
break;
default :
// if action is not defined or unknown
// move to main Topping page
header('Location: index.php');
}
function addTopping()
{
if($_FILES['bgImg']['tmp_name']!='')
{
$srcFile = $_FILES['bgImg']['tmp_name'];
$size = $_FILES['bgImg']['size'];
if($size<5227520) //if file is less than 5MB
{
$ext = substr(strrchr($_FILES['bgImg']['name'], "."), 1);
$ext = strtolower($ext);
if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif"))
{
//print error message
print "<script>alert('Unknown File Format Detected')</script>";
print "<script>window.location='index.php'</script>";
exit;
}
else
{
$img_name = md5(rand() * time()) . ".$ext";
$dest = '../../testingImageIntoFolderAndDatabase/upload/';
$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$phone = $_POST['txtPhone'];
$address = $_POST['txtAddress'];
$cuisine = $_POST['txtCuisine'];
$mini_order = $_POST['txtMini_order'];
$suburbs = $_POST['txtSuburbs'];
$menu = $_POST['txtMenu'];
$reviews = $_POST['txtReviews'];
$logo = $_POST['txtLogo'];
$takeaway_hours = $_POST['txtTakeawayHours'];
$delivery_hours = $_POST['txtDeliveryHours'];
$map = $_POST['txtMap'];
$promotions = $_POST['txtPromotions'];
//copy new image
move_uploaded_file($_FILES['bgImg']['tmp_name'],$dest.$img_name);
dbQuery("insert into restaurant_info set Rest_Logo='$img_name', name='$name', email='$email', phone='$phone', address='$address', cuisine='$cuisine', mini_order='$mini_order', suburbs='$suburbs', menu='$menu', reviews='$reviews', logo='$logo', takeaway_hours='$takeaway_hours', delivery_hours='$delivery_hours', map='$map', promotions='$promotions'");
print "<script>window.location='index.php'</script>";
exit;
}
}
else
{
print "<script>alert('File Size Exceeded!')</script>";
print "<script>window.location='index.php'</script>";
exit;
}
}
}
/*
Modify a Topping
*/
function modifyTopping()
{
$toppingId = (int)$_POST['hidToppingId'];
$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$phone = $_POST['txtPhone'];
$address = $_POST['txtAddress'];
$cuisine = $_POST['txtCuisine'];
$mini_order = $_POST['txtMini_order'];
$suburbs = $_POST['txtSuburbs'];
$menu = $_POST['txtMenu'];
$reviews = $_POST['txtReviews'];
$logo = $_POST['txtLogo'];
$takeaway_hours = $_POST['txtTakeawayHours'];
$delivery_hours = $_POST['txtDeliveryHours'];
$map = $_POST['txtMap'];
$promotions = $_POST['txtPromotions'];
$sql = "UPDATE restaurant_info
SET name='$name', email='$email', phone='$phone', address='$address', cuisine='$cuisine', mini_order='$mini_order', suburbs='$suburbs', menu='$menu', reviews='$reviews', logo='$logo', takeaway_hours='$takeaway_hours', delivery_hours='$delivery_hours', map='$map', promotions='$promotions'
WHERE rest_id = $toppingId";
dbQuery($sql);
header('Location: index.php');
}
/*
Remove a topping
*/
function deleteTopping()
{
if (isset($_GET['toppingId']) && (int)$_GET['toppingId'] > 0) {
$toppingId = (int)$_GET['toppingId'];
} else {
header('Location: index.php');
}
$sql = "DELETE FROM restaurant_info
WHERE rest_id = $toppingId";
dbQuery($sql);
header('Location: index.php');
}
?>