简体   繁体   中英

mysql database doesn't insert any items on the table,browsers don't show any error either

I've fixed all the syntax errors,but items I'm trying to insert on the table are not passing on table in the database.Here is the code:

   <?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>

showing me the message,"you have added the product before",but not inserting into the table. I'm a beginner PHP programmer.Please help.

You're missing the name attribute on your fields. $_POST['product_name'] is not set.

<input type="text" id="product_name" placeholder="Type Product Name" />

should be

<input type="text" name="product_name" id="product_name" placeholder="Type Product Name" />

Your conditional is checking isset($_POST['product_name']) to run the insert, so because the name attribute is missing, that conditional is never evaluating to true.

Side note, you are also trying to accept a file. You need to have enctype="miltipart/form-data" in the form attributes:

<form id="formo" action="inventory_list.php" method="post" enctype="miltipart/form-data">

Also, make sure you have a name attribute for all fields.

You are also checking against a boolean in your count:

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

that should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM