如何在php中使用id获取数据并插入?

How to get data and insert using id in php?

我在php做购物网站,有一期产品数据。

First I make **product** datatabe below.

    CREATE TABLE IF NOT EXISTS `product` (                                    
       `product_id` int(11) NOT NULL AUTO_INCREMENT,                               
       `product_sales_id` int(11) NOT NULL,                                        
       `product_name` varchar(100) NOT NULL,                                        
       `product_price` varchar(5000) NOT NULL,                                       
       `image_name` varchar(300) NOT NULL,                                               
       PRIMARY KEY (`product_id`)
    )


And Second table **product sales** below :

    CREATE TABLE IF NOT EXISTS `product_sales` (                                  
       `product_sales_id` int(11) NOT NULL,                                          
    )

Below page it admin site page when select product to insert in database and 
show in product page.

Add_Product.php : 

    <?php 
        include_once('include/connection.php');
        include_once('include/function.php');
    ?>

    <?php 
        if(isset($_POST['submit'])){
            $product_name = mysqli_real_escape_string($conn,$_POST['product_name']);
            $product_price = mysqli_real_escape_string($conn,$_POST['product_price']);  
            $image_name = mysqli_real_escape_string($conn, basename( $_FILES["product_image"]["name"]));

            $target_dir = "upload/";
            $target_file = $target_dir . basename($_FILES["product_image"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            $filetype = $_FILES["product_image"]["type"];

            $image_upload = move_uploaded_file($_FILES["product_image"]["tmp_name"], $target_file);

            if($image_upload){
                mysqli_query($conn,"INSERT INTO product(product_name,product_price,image_name) VALUES ('".$product_name."','".$product_price."','".$image_name."')");   
            }

        }
    ?>

我在更多页面上制作了产品销售页面,但我不知道该怎么做 上面显示我将 table product_sales 和他的 id 存储在产品 table 中 如何在数据库中添加 product_sales 并使用 id 在页面中显示。

product_salestable中的product_sales_id修改为主键,product_salestable中添加product_id字段作为外键。

产品售出后,您可以将product_id存储在product_salestable中。

并且您可以使用连接查询 product_sales table 中存在的 product_id 从产品 table 中获取已售出的产品详细信息。

如果您在购物网站上工作,那么您错过了一些非常重要的东西。

您需要为购物车制作一个 temp table,其中将包含用户将添加的所有产品,当用户在查看产品后下订单时,它们就会进入 products_sales table 从临时 table.

中删除后

此外,您需要在 product_sales table 中提供 product_id,但无需在 products table 中提供 product_sales_id,因为产品永远不会永远属于任何特定的订单。