dondada Posted January 6, 2015 Share Posted January 6, 2015 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"); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 6, 2015 Share Posted January 6, 2015 While developing, instead of just outputting "errror", try outputting something helpful to you, like mysqli_error Quote Link to comment Share on other sites More sharing options...
dondada Posted January 6, 2015 Author Share Posted January 6, 2015 i add this lines in else statement if(mysqli_connect_errno()){ echo mysqli_connect_errno(); exit(); } bt still not working Quote Link to comment Share on other sites More sharing options...
wezhind Posted January 6, 2015 Share Posted January 6, 2015 (edited) 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? Edited January 6, 2015 by wezhind Quote Link to comment Share on other sites More sharing options...
dondada Posted January 6, 2015 Author Share Posted January 6, 2015 yah the images are uploading to the folder, and yah i am able read , categories and brands and they are showing in the from when uses echo in sql its working ... let me upload my database Quote Link to comment Share on other sites More sharing options...
dondada Posted January 6, 2015 Author Share Posted January 6, 2015 http://www.datafilehost.com/d/bca637ec this is my table where m trying to insert data Quote Link to comment Share on other sites More sharing options...
Barand Posted January 6, 2015 Share Posted January 6, 2015 Does this mean you have given up trying to find the reason for the error (even though you've been told how) and want us to do the work for you? Quote Link to comment Share on other sites More sharing options...
dondada Posted January 6, 2015 Author Share Posted January 6, 2015 no no i did what i can if you figured the problem please point me out i will do that .. i didn't meant to work for me. sorry if i done something wrong Quote Link to comment Share on other sites More sharing options...
Barand Posted January 6, 2015 Share Posted January 6, 2015 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". 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.