gpt4 book ai didi

php - mysql数据库不会在表上插入任何项目,浏览器也不会显示任何错误

转载 作者:行者123 更新时间:2023-11-29 13:16:15 25 4
gpt4 key购买 nike

我已经修复了所有语法错误,但是我尝试在表上插入的项目没有传递到数据库中的表。以下是代码:

   <?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 初学者。请帮忙。

最佳答案

您的字段中缺少名称属性。 $_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">

此外,请确保所有字段都有一个名称属性。

您还检查计数中的 bool 值:

$numrow=mysqli_num_rows($counting);
if($counting==1){

应该是这样

$numrow=mysqli_num_rows($counting);
if($numrow > 0){

关于php - mysql数据库不会在表上插入任何项目,浏览器也不会显示任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21420268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com