简体   繁体   中英

PHP Inventory Management Registration

I currently have a system where you can login, and insert values into a database to manage inventory. I'm trying to figure out a way to have users able to register and have it automatically create this table for them according to their session id, and then have them re-directed to a page with options to insert data into this table. The PHP code for inserting an item into the database is:

    <?php
    $con=mysqli_connect("localhost","root","root","inventory");

    // Check connection
    if (mysqli_connect_errno($con))
      {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }



    $sql = "INSERT INTO `orders` (ItemNumber, Quantity)
       VALUES
            ('".$_POST['ItemNumber']."', '".$_POST['Quantity']."' )";

     if (!mysqli_query($con,$sql))
     {
    die('Error: ' . mysqli_error($con));
     }
     echo "1 record added";


    mysqli_close($con);
     ?>

Can I change this so instead of inserting into "orders", it inserts into the table matching the users session id? Thanks

It is not good practice to create a table for each user.

Have an extra field in the orders table to denote the user who placed the order.

Table definition like this,

UserId,ItemNumber, Quantity

Insert Query

     $sql = "INSERT INTO `orders` (UserId,ItemNumber, Quantity)
   VALUES
        ('<UserId goes here from session data>','".$_POST['ItemNumber']."', '".$_POST['Quantity']."' )";

Simply add a field in ur orders table which represents id of the user who inserted the order..

The structure of 'orders' table can be like id primary key, item_id int, ordered_by user_id(foreign key)

..

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