Jump to content

data not inserting in mysql database


dondada

Recommended Posts

hello friends,

actually i am trying to insert data in mysql

database.

here is my code

<?php
	include_once("../includes/database.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert Product</title>
</head>

<body>
	<form action="insert_product.php" method="post" enctype="multipart/form-data">
    	<table align="center" width="600">
        <tr align="center">
        	<td colspan="8"><h2> Insert New Post</h2></td>
        </tr>
        <tr>
        	<td align="right"><b>Product Title :</b></td>
            <td><input type="text" name="product_title"  size="50" style="background-color:#06C; color:#FFF"  /></td>
        </tr>
         <tr>
        	<td align="right"><b>Product Category :</b></td>
            <td>
            	<select name="product_cat" style="background-color:#06C; color:#FFF" >
                	<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 Platform :</b></td>
            <td>
            	<select name="product_brand" style="background-color:#06C; color:#FFF" >
                	<option>Select A Platform </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 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" style="background-color:#06C; color:#FFF"  /></td>
        </tr>
         <tr>
        	<td align="right" valign="top"><b>Product Description :</b></td>
            <td><textarea name="product_desc" cols="50" rows="10" style="background-color:#06C; color:#FFF"  ></textarea></td>
        </tr>
         <tr>
        	<td align="right"><b>Product Keywords :</b></td>
            <td><input type="text" name="product_keywords" size="50" style="background-color:#06C; color:#FFF" /></td>
        </tr>
         <tr align="center">
            <td colspan="8"><input type="submit" name="insert_post" value="Submit Now" /></td>
        </tr>
        </table>
    </form>
</body>
</html>

<?php
	if(isset($_POST['insert_post'])){
		$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'];
		$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_image/$product_image");
		
		$sql = "INSERT INTO products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) VALUES ('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keywords')";
		$query = mysqli_query($con, $sql);
		if($query){
			echo "<script>alert('Product Has Been Inserted')</script>";	
			echo "<script>windoow.open('insert_product.php','_self')</script>";
			exit();
		}else{
			echo "<script>alert('errror')</script>";	
		}
	}
?> 
 
but somehow its not inserting

data into my table can somebody tell wherre

m i doing mistake.

 

the categories and brands are displaying from database. But its not inserting data

 

here is my database script.

 <?php
 $con = mysqli_connect("localhost","root","","sg");
?>    
Link to comment
https://forums.phpfreaks.com/topic/293699-data-not-inserting-in-mysql-database/
Share on other sites

Is the image being uploaded? Does it get to the actual insert code? You need some form of error trapping to help you (and us) identify your issue or point you in the right direction. At the very least you may consider after the $sql string is created temporarily putting

echo $sql; 

to show you whether the correct SQL is being used - i.e. whether items that are required fields are not being supplied correctly in the SQL etc etc

 

Good luck

 

edit: p.s. the error trap you've added appears to only be a connection specific error trap - barand supplied you with a query friendly one, did you add that? Your issue doesn't seem to be with the connection - after all, aren't you able to read from the database?

How can we figure it out when you give us little more than "it doesn't work"?

 

Have you tried outputting the content from mysqli_error() after attempting the insert query?

 

What is the output from "echo $sql;" ? We are not looking over your shoulder seeing what you see on the screen.

 

I doubt anyone here is going to download the content of an unknown file. If you got an email saying you had won 1 million dollars and had to click on a download link for details, would you? Post the output from "SHOW CREATE TABLE products".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.