【发布时间】:2016-06-10 01:05:03
【问题描述】:
该页面基本上是一种用于将新产品添加到数据库中的产品表中的表单。表格还必须包括图片上传。该函数应该在将任何数据插入数据库之前回显查询。但是,每次我按下提交按钮时,它都不会显示查询,并且表单只会自行重置。我尝试了不同的解决方案,但它们不起作用。我将表单操作更改为新的 php 页面,但仍然无法正常工作。我还尝试使用两种不同的浏览器,并尝试显示错误代码。代码中有什么乱七八糟的地方吗?
<!DOCTYPE>
<?php
include("../includes/db.php");
?>
<html>
<head>
<title>Insert a Product</title>
<script src="//tinymce.cachefly.net/4.3/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>
</head>
<body>
<form name="submit" action="insert_product.php"method="POST"enctype="multipart/from-data">
<table align="center" width="800">
<tr align="center">
<td colspan="8"><h4>Insert New Post Here</h4></td>
</tr>
<tr>
<td align="right"><b>Product Title:</b></td>
<td><input type="text" name="pro_name" /></td>
</tr>
<tr>
<td align="right"><b>Product Price:</b></td>
<td><input type="text" name="price"/></td>
</tr>
<tr>
<td align="right"><b>Product Image:</b></td>
<td><input type="FILE" name="product_image" id="product_image"/></td>
</tr>
<tr>
<td align="right"><b>Product Color:</b></td>
<td><input type="text" name="Color"/></td>
</tr>
<tr>
<td align="right"><b>Product Location:</b></td>
<td>
<select name="location">
<option>Select a Location</option>
<?php
$get_location = "select * from location";
$run_location = mysqli_query($conn, $get_location);
while ($row_location=mysqli_fetch_array($run_location)){
$Loc_name = $row_location['Loc_name'];
$location_id = $row_location['location_id'];
echo "<option value='$location_id'>$Loc_name</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Supplier:</b></td>
<td><input type="text" name="pro_supplier"/></td>
</tr>
<tr>
<td align="right"><b>Product Cost:</b></td>
<td><input type="text" name="cost"/></td>
</tr>
<tr>
<td align="right"><b>Product Keywords:</b></td>
<td><input type="text" name="pro_keywords"/></td>
</tr>
<tr>
<td align="right"><b>Product Description:</b></td>
<td><textarea name="Pro_desc" cols="20" rows="10"/></textarea></td>
</tr>
<tr align="center">
<td colspan="7"><input type="submit" name="submit" value="Insert Product Now"/></td>
</tr>
</form>
</body>
</html>
<?php
if (isset($_POST['submit']) && isset($_FILES['product_image'])){
$pro_name = $_POST['pro_name'];
$price = $_POST['price'];
$Color = $_POST['Color'];
$cost = $_POST['cost'];
$pro_desc = $_POST['pro_desc'];
$pro_keywords = $_POST['pro_keywords'];
$product_image = $_FILES['product_image']['name'];
$product_imgtmp = addslashes (file_get_contents($_FILES['product_image']['tmp_name']));
echo $insert_product =
"insert into products
(pro_name, price, Color, cost, Pro_desc, pro_keywords, product_image)
VALUES
('$pro_name','$price','$Color','$cost','$pro_desc','$pro_keywords','$product_image')";
if ($conn->query($insert_product) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $insert_product . "<br>" . $conn->error;
}
}
?>
【问题讨论】:
-
action="insert_product.php"method="POST"enctype="multipart/from需要空间我的朋友空间 -
您正在回显赋值操作的结果,因此您将看到的只是
true。要查看您的查询,请将 sql 分配给$insert_product,然后分配给echo $insert_product -
另外,查看准备好的语句并绑定占位符,您的代码容易受到 SQL 注入的影响
-
这是一个该死的错字
from-data应该读作enctype="multipart/form-data- 表单表单而不是from并且错误报告会给你一些关于它的东西。 php.net/manual/en/function.error-reporting.php 未定义的索引 blah blah blah -
为您的
if (isset($_POST['submit']) && isset($_FILES['product_image'])){...}添加了else { echo "it broke..."; }会显示“它坏了”。
标签: php html mysql forms submit