Dhaval
Dhaval

Reputation: 59

How to get data and insert using id in php?

I make shopping site in php and I have one issue of product data.

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."')");   
            }

        }
    ?>

I make on more page product sell page but i do not know how to make this Above show I make table product_sales and his id store in product table how to add product_sales in database and show in page using id through.

Upvotes: 0

Views: 81

Answers (2)

Amit Gupta
Amit Gupta

Reputation: 2806

If you are working on shopping site, then you are missing something very important.

You need to make one temp table for shopping cart that will contain all products that user will add and when user will place order after reviewing the products then they will come in products_sales table after removing from temp table.

Also you need to give product_id in product_sales table but no need to give product_sales_id in products table as products never belong to any particular order forever.

Upvotes: 0

Kannan K
Kannan K

Reputation: 4461

Change the product_sales_id in product_sales table into primary key and add product_id field as a foreign key in product_sales table.

Once the product is sold, you can store the product_id in the product_sales table.

And you can fetch the sold product details from product table using product_id which is present in the product_sales table using join query.

Upvotes: 1

Related Questions