我修复了所有语法错误,但是我试图在表上插入的项目没有传递到数据库中的表。这是代码:
<?php
session_start();
if(!isset($_SESSION["manager"])){
header("location:admin_login.php");
exit();
}
$id=preg_replace('#[^0-9]#i','',$_SESSION["id"]);
$manager=preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["manager"]);
$password=preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["password"]);
include "connect_to_db.php";
$sqlcommand="SELECT COUNT(id) FROM admin WHERE id='$id' AND username='$manager' AND password='$password' LIMIT 1";
$counting=mysqli_query($connect_dude,$sqlcommand);
$numrow=mysqli_num_rows($counting);
if($numrow==0){
echo "you better get out of here";
exit();
}
?>
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
if(isset($_POST["product_name"])){
$productname=preg_replace('#[^A-Za-z]#i','',$_POST["product_name"]);
$productprice=preg_replace('#[^0-9]#i','',$_POST["product_price"]);
$productdetails=preg_replace('#[^A-Za-z0-9]#i','',$_POST["product_detail"]);
$productdivision=$_POST["product_division"];
$productsubdivision=$_POST["product_subdivision"];
$sql="SELECT COUNT(id) FROM products WHERE product_name='$productname' LIMIT 1";
$counting=mysqli_query($connect_dude,$sql);
$numrow=mysqli_num_rows($counting);
if($numrow>0){
echo "you have added the product before";
}
else{
$sql="INSERT INTO products (product_name,price,details,category,subcategory,date_added) VALUES ($productname,$productprice,$productdetails,$productdivision,$productsubdivision,now())";
mysqli_query($connect_dude,$sql);
$pid=mysqli_insert_id();
$newfile=$pid.jpg;
move_uploaded_file($_FILES['image']['tmp_name'],"$productsubdivision/$newfile");
header("location: inventory_list.php");
exit();
}
}
?>
<html>
<head>
</head>
<body>
<fieldset>
<legend value="add something">Add product,dude</legend>
<form id="formo" action="inventory_list.php" method="post" enctype="multipart/form-data">
Product Name:<input type="text" id="product_name" name="product_name" placeholder="Type Product Name" /></br>
Product price:<input type="text" id="product_price" name="product_price" placeholder="Type Product price" /></br>
Product division:
<select id="product_division" name="product_division">
<option>select</option>
<option>food</option>
<option>apparel</option>
<option>beauty</option>
<option>toys</option>
</select>
</br>
Product type:
<select id="product_subdivision" name="product_subdivision">
<option>select</option>
<option>Rice</option>
<option>milk</option>
<option>Meat</option>
<option>sweet</option>
<option>fruits</option>
<option>cap</option>
<option>shirt</option>
<option>pant</option>
</select>
</br>
Product Detail:<textarea id="product_detail" name="product_detail" placeholder="Type Product detail,bro" ></textarea>
</br>
product Image:<input type="file" id="image" name="image" />
</br>
<input type="submit" name="button" id="button" value="Add This Item Now" />
</form>
</fieldset>
</body>
</html>
向我显示消息“您之前已添加产品”,但未插入表格。 我是初学PHP程序员。请帮助。
答案 0 :(得分:1)
您缺少字段上的name属性。 $_POST['product_name']
未设置。
<input type="text" id="product_name" placeholder="Type Product Name" />
应该是
<input type="text" name="product_name" id="product_name" placeholder="Type Product Name" />
您的条件是检查isset($_POST['product_name'])
以运行插入,因为缺少name属性,该条件永远不会评估为true。
旁注,您也在尝试接受文件。您需要在表单属性中使用enctype="miltipart/form-data"
:
<form id="formo" action="inventory_list.php" method="post" enctype="miltipart/form-data">
另外,请确保您拥有所有字段的名称属性。
您还要检查计数中的布尔值:
$numrow=mysqli_num_rows($counting);
if($counting==1){
应该是
$numrow=mysqli_num_rows($counting);
if($numrow > 0){