filled data not inserting in mysql database - php

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

Related

object of class mysqli _connect could not be converted to string in line 135

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);

why am I getting a MySQl error

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?

update php&mysql : undefined index

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>

$_POST not working. when I echo it

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";
}

MySQL database not updating when form is submited

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");

Categories