未加星标

Unable to insert data into the mysql table

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I am not able to insert product and also alert script is not working. I'm trying to insert some data into my database using a form. After filling the form I am taking data using method="POST". I have no syntax errors, although I can't insert the data from my form to my database.

<php include("includes/db.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>Untitled Document</title> <script src="//tinymce.cachefly.net/4.3/tinymce.min.js"></script> <script>tinymce.init({ selector:'textarea' });</script> </head> <body bgcolor="#999999"> <form method="post" action="insert_product.php" enctype="multipart/form-data"> <table width="700" align="center" border="1" bgcolor="#0099CC"> <tr align="center"> <td colspan="2"><h1>Insert New Product:</h1></td> </tr> <tr> <td align="right"><b>Product Title</b></td> <td><input type="text" name="product_title" size="50"/></td> </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 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 align="right"><b>Product Image 1</b></td> <td><input type="file" name="product_img1"/></td> </tr> <tr> <td align="right"><b>Product Image 2</b></td> <td><input type="file" name="product_img2"/></td> </tr> <tr> <td align="right"><b>Product Image 3</b></td> <td><input type="file" name="product_img3"/></td> </tr> <tr> <td align="right"><b>Product Price</b></td> <td><input type="text" name="product_price"/></td> </tr> <tr> <td align="right"><b>Product Description</b></td> <td><textarea name="product_desc"cols="35" rows="10"></textarea></td> </tr> <tr> <td align="right"><b>Product Keywords</b></td> <td><input type="text" name="product_keywords" size="50"/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="Insert Product" value="Insert Product"/></td> </tr> </table> </form> </body> </html> <?php if (isset($_POST['insert_product'])){ //text data variables $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_keywords =$_POST['product_keywords']; //image names $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_keywords=='' OR $product_img1==''){ echo "<script>alert('Please fill all the fields!')</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 successfully')</script>"; } } } ?>

just one line mistake.Replace

<input type="submit" name="insert_product" value="Insert Product"/>

to

<input type="submit" name="insert_product" value="Insert Product"/> Since you have checked if(isset($_POST[insert_product])). Try the code <?php include("includes/db.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>Untitled Document</title> <script src="//tinymce.cachefly.net/4.3/tinymce.min.js"></script> <script>tinymce.init({ selector:'textarea' });</script> </head> <body bgcolor="#999999"> <form method="post" action="insert_product.php" enctype="multipart/form-data"> <table width="700" align="center" border="1" bgcolor="#0099CC"> <tr align="center"> <td colspan="2"><h1>Insert New Product:</h1></td> </tr> <tr> <td align="right"><b>Product Title</b></td> <td><input type="text" name="product_title" size="50"/></td> </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 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 align="right"><b>Product Image 1</b></td> <td><input type="file" name="product_img1"/></td> </tr> <tr> <td align="right"><b>Product Image 2</b></td> <td><input type="file" name="product_img2"/></td> </tr> <tr> <td align="right"><b>Product Image 3</b></td> <td><input type="file" name="product_img3"/></td> </tr> <tr> <td align="right"><b>Product Price</b></td> <td><input type="text" name="product_price"/></td> </tr> <tr> <td align="right"><b>Product Description</b></td> <td><textarea name="product_desc"cols="35" rows="10"></textarea></td> </tr> <tr> <td align="right"><b>Product Keywords</b></td> <td><input type="text" name="product_keywords" size="50"/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="insert_product" value="Insert Product"/></td> </tr> </table> </form> </body> </html> <?php if (isset($_POST['insert_product'])){ //text data variables $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_keywords =$_POST['product_keywords']; //image names $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_keywords=='' OR $product_img1==''){ echo "<script>alert('Please fill all the fields!')</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 successfully')</script>"; } } } ?>

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

tags: lt,gt,product,td,tr,name,title,Product,align,brand,cat,right,script,brands
分页:12
转载请注明
本文标题:Unable to insert data into the mysql table
本站链接:https://www.codesec.net/view/604937.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(23)