EDITED I am using HTML forms and PHP to upload data to a MySQL database. When I submit the information an image that i uploaded gets moved to a specific folder(the way i intended it to), but the data does not get uploaded to the data base.
I added most of the code now, the site didn't allow me to add everything, I deleted some lines that in my opinion don't have anything to do with the problem at hand. so maybe you'll see the problem:
<!DOCTYPE>
<?php
include("includes/db.php");
?>
<html>
<head>
<title>Inserting Product</title>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
</head>
<body bgcolor="grey">
<form action="insert_product.php" method="post" enctype="multipart/form-
data">
<table align="center" width="795" border="2" bgcolor="lime">
</tr>
<tr>
<td align="right"><b>Product Category:</b></td>
<td>
<select name="product_cat" >
<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_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Publisher:</b></td>
<td> <select name="product_dev">
<option>Select a Publisher</option>
<?php
$get_devs = "select * from developers";
$run_devs = mysqli_query($con, $get_devs);
while ($row_devs=mysqli_fetch_array($run_devs)){
$dev_id = $row_devs['dev_id'];
$dev_title = $row_devs['dev_title'];
echo "<option value='$dev_id'>$dev_title</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 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'])){
$product_title = $_POST['product_title'];
$product_cat= $_POST['product_cat'];
$product_pub = $_POST['product_pub'];
$product_dev = $_POST['product_dev'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
$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_pub,product_dev,
product_title,product_price,product_desc,product_image,
product_keywords) values ('$product_cat','$product_pub','$product_dev',
'$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>";
}
}
?>
Your query seems fine.
Just verify your MySQLi connection.
It should be like:
$con = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DATABASE_NAME");
Related
when i run the code it give error of object of class mysqli_connect could not be converted to string in line 135. please some one provide me the solution of this problem.
i tried searching for solution but no one worked for me. bellow is the code showing error.
$row = mysqli_connect("$con,$insert_product");
accepted to run but showing error in line 135. what mistake do i have make.full code is here
<?php require_once ("includes/db.php");
?>
<!DOCTYPE html>
<html>
<head>
<title> product</title>
</head>
<body>
<form method="post" action="insert_product.php" enctype="multipart/form-data">
<table width="700" align="center">
<tr>
<td><h2>insert new product</h2></td>
</tr>
<tr>
<td>Product title</td>
<td><input type="text" name="product_title"></td>
</tr>
<tr>
<td>Product category</td>
<td><select name="product_cat">
<option>Select a category</option>
<?php
$get_cats = "select * from category";
$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>Product brand</td>
<td><select name="product_brand">
<option>Select brand</option>
<?php
$get_brand = "select * from brand";
$run_brand= mysqli_query($con,$get_cats);
while ($row_brand=mysqli_fetch_array($run_brand))
{
$brand_id = $row_brand['brand_id'];
$brand_title=$row_brand['brand_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>product image1</td>
<td><input type="file" name="product_img1"></td>
</tr>
<tr>
<td>Product image2</td>
<td><input type="file" name="product_img2"></td>
</tr>
<tr>
<td>Product image3</td>
<td><input type="file" name="product_img3"></td>
</tr>
<
<tr>
<td>Product price</td>
<td><input type="number" name="product_price"></td>
</tr>
<tr>
<td>Product desceptration</td>
<td><input type="text" name="product_desc"></td>
</tr>
<tr>
<td>Product keyword</td>
<td><input type="number" name="product_keyword"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$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 name
$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_keyword=='' OR $product_img1=='' OR $product_img2=='' OR $product_img3=='')
{
echo "<script> alert ('please insert the data in the form')</script>";
exit();
}
else{
//uploadinimage to the 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_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
if($row)
{
echo "<script> alert('insert sucessfully')</script>";
}
else
{
echo "<script> alert(' unsucessfull to insert')</script>";
}
}
?>
I don't think you're meaning to connect again. Rather, it appears you may be trying to perform a query?
Your code:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
Not only are you referencing a variable never defined in the script (though it probably is in the file you require at the top), but you're passing a query right to mysqli_connect(). I imagine what you actually meant to do is:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_query($con, $insert_product);
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?
I'm now making reservation system, and something is wrong with updating data. I made table updateMy_ReservationView.php like this.
this is an image of SelectMy_ReservationView.php I have an error with insert more codes because of this editor's error message.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>
And I made updating function file updateMy_ReservationDo.php like below.
include "connection.php";
$reservation_id=$_POST['reservation_id'];
$car_type=$_POST['car_type_id'];
$rent=$_POST['rent'];
$return_time=$_POST['return_time'];
$car_station=$_POST['car_station'];
$sql = "update reservation set car_type='$car_type_id',rent='$rent',
return_time='$return_time' and car_station='$car_station' where
reservation_id=$reservation_id ";
if (mysqli_query($conn, $sql)) {
echo "Reservation is updated successfully<br>";
echo "<p><p><a href=?page=selectMy_reservationView><button type=button>Show
all reservation</button></a>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Then Error message like this happen:
Notice: Undefined index: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 5
Notice: Undefined variable: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 10
Error: update reservation set car_type='',rent='2018-05-31', return_time='2018-06-01' and car_station='Shinchon' where reservation_id=17
Unknown column 'car_type' in 'field list'
What should I modify?
Use isset like following :
$reservation_id = isset($_POST['reservation_id']) ? $_POST['reservation_id'] : '';
$car_type = isset($_POST['car_type_id']) ? $_POST['car_type_id'] : '';
$rent = isset($_POST['rent']) ? $_POST['rent'] : '';
$return_time = isset($_POST['return_time']) ? $_POST['return_time'] : '';
$car_station = isset($_POST['car_station']) ? $_POST['car_station'] : '';
and your defined variable is $car_type but you used in your sql query $car_type_id
use :
$car_type
and make sure car_type field exists in your table
Change the name of car_type to car_type_id.The error is due to you are sending car_type and accessing car_type_id in updateMy_ReservationDo.php.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type_id">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>
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
The below program is not working.
add_product.php
<?php
$name=$_REQUEST["txtname"];
$price=$_REQUEST["txtprice"];
$category=$_REQUEST["ddlcategory"];
$weight=$_REQUEST["txtweight"];
$description=$_REQUEST["txtdescription"];
$img=$_REQUEST["btnimage"];
$connect=mysql_connect("localhost","root","","CakeShop") or die(mysql_error());
echo "Connected..";
mysql_select_db(CakeShop);
if(isset($_REQUEST['btnadd']))
{
//The if loop gets executed even if the image is selected....!
if(getimagesize($Files['btnimage']['tmp_name'])==FALSE)
{
$message="Please select an image";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
$insert="Insert into tblProduct(p_name,p_price,p_category,p_weight,p_description,p_image) values ('$name','$price','$category','$weight','$description','$img');";
mysql_query($insert) or die("Failed to insert data");
echo "<h3>Product Details Inserted........</h3>";
}
}
mysql_close();
?>
add_product.html
<html>
<head>
<title>Cake Central</title>
</head>
<body>
<h1>ADD NEW PRODUCT</h1>
<hr>
<form method="post" action="add_product.php">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="txtname"></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="txtprice"></td>
</tr>
<tr>
<td>Category:</td>
<td>
<select name="ddlcategory">
<option>Veg</option>
<option>Non-Veg</option>
</select>
</td>
</tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="txtweight"></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="2" cols="16" name="txtdescription"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input name="btnimage" type="file" /></td>
</tr>
<tr>
<td><input class="btn" type="submit" name="btnadd" value=" Add "></td>
</tr>
</table>
</form>
</body>
</html>
I have simplified your code to the best of my knowledge. You don't really need two different pages so I have removed the action = "" in the form tag. Please let me know how it goes.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
else
{
$db_selected = mysql_select_db('CakeShop', $conn);
if (!$db_selected)
{
die ('Can\'t Select DB : ' . mysql_error());
}
}
if(isset($_POST['check']) && $_FILES['file']['size'] > 0)
{
$name=$_POST["txtname"];
$price=$_POST["txtprice"];
$category=$_POST["ddlcategory"];
$weight=$_POST["txtweight"];
$description=$_POST["txtdescription"];
$tmpName = $_FILES['file']['tmp_name'];
$fp = fopen($tmpName, 'r');
$img = fread($fp, filesize($tmpName));
$img = addslashes($img);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO tblProduct (p_name,p_price,p_category,p_weight,p_description,p_image)
VALUES ('$name','$price','$category','$weight','$description','$img')";
mysql_query($query) or die('Error, query failed');
$imgid = mysql_insert_id();
echo "<br>Details successfully added to database<br>";
}
else
die('You have not selected any image');
?>
<html>
<head>
<title>Cake Central</title>
</head>
<body>
<h1>ADD NEW PRODUCT</h1>
<hr>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="txtname"></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="txtprice"></td>
</tr>
<tr>
<td>Category:</td>
<td>
<select name="ddlcategory">
<option>Veg</option>
<option>Non-Veg</option>
</select>
</td>
</tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="txtweight"></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="2" cols="16" name="txtdescription"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input name="btnimage" type="file" /></td>
</tr>
<tr>
<td><input type="hidden" name="check"></td>
</tr>
<tr>
<td><input class="btn" type="submit" name="btnadd" value=" Add "></td>
</tr>
</table>
</form>
</body>
</html>
Note: Since you are really new and have no idea about mysqli/PDO, I
have suggested a link and you will find alot of other useful articles
on this site as well, One of the reason why I did not change it to
mysqli/PDO was because I did not want you to get confused. cheers
Try adding the enctype attribute to the form tag.
<form method="post" action="add_product.php" enctype="multipart/form-data" >