我正在尝试按照教程进行操作,似乎无法将数据添加到数据库中,并且检查了代码并且似乎没有找到它无法正常工作的原因。代码应该将数据插入到数据库中,而是在单击按钮时刷新页面。请指出我出错的地方。因为我也是php的新手。下面是我的代码:
<!DOCTYPE>
<?php
include ("includes/db.php");
?>
<html>
<head>
<title>inserting Product</title>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
tinymce.init({ selector:'textarea' });
</script>
</head>
<body bgcolor="skyblue">
<form action="insert_product.php" method="post" enctype="multipart/form-data">
<table align="center" width="700" border="2" bgcolor="orange">
<tr align="center">
<td colspan="7"><h2>Insert New Post Here</h2></td>
</tr>
<tr>
<td align="right"><b>Product Title:</b></td>
<td><input type="text" name="product_title" size="60" required /></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['cats_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 a Brand</option>
<?php
$get_brands = "select * from brands";
$run_brands = mysqli_query($con, $get_brands);
While ($row_brands=mysqli_fetch_array($run_brands)){
//$cat_id = $row_cats['cats_id'];
$brand_title = $row_brands['brand_title'];
echo "<option value='brand_id'>$brand_title</option>";
}
?>
</td>
</tr>
<tr>
<td align="right"><b>Product Image:</b></td>
<td><input type="file" name="product_image" required /></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 Description:</b></td>
<td><textarea name="product_desc" cols="20" rows="10"></textarea></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="inset_post" value="Insert Product Now" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['insert_post'])){
//input data from the fields
$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'];
//input image from feild
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
//move_uploaded_file($product_image_tmp,"product_images/$product_image");
echo $insert_product = "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')";
//$insert_pro = mysqli_query($con, $insert_product);
//if($insert_pro){
//echo"<script>alert('Product has been inserted!')</script>";
//echo "<script>window.open('insert_product.php','_self'</script>)";
//}
}
?>
enter code here
答案 0 :(得分:1)
您的提交按钮名称为&#34; inset_post&#34;而不是&#34;插入&#34;