简体   繁体   中英

Inserting selected data from table to another table

I want to add the selected data from table1 to another table2 here's my supposed output.

Product Inventory List <-- displays the added product from 2nd table

Product List <-- this one display the products from 1st table

In "Product List" I have my one listed product then if I click the "Add" Link, the data should be listed in the "Product Inventory List"

Anyway here's my code for displaying the data of 1st table

<?php 
$product_list = "";
$sql = mysql_query("SELECT * FROM productinformation1 ORDER BY date_added DESC");
$productCount=mysql_num_rows($sql);
if($productCount>0){
    while($row = mysql_fetch_array($sql)){
        $id = $row["ProductNo"];
        $product_name = $row["ProdName"];
        $product_list .= "Product ID:&nbsp;$id - $product_name &nbsp; &nbsp;&nbsp; &nbsp; &bull; <a href='#'>Edit</a><br>";
    }
}else{
    $product_list = "You have no products listed in your store yet.";
}

?>

And here's my code for displaying the data of 2nd table

<?php
$productsDB = "";
$sql1 = mysql_query("SELECT * FROM productinformation");
$productDBcount=mysql_num_rows($sql1);
if($productDBcount>0){
        while($row = mysql_fetch_array($sql1)){
        $id1 = $row["ProductNo"];
        $product_name = $row["ProdName"];
        $productsDB .= "Product ID:&nbsp;$id1 - $product_name &nbsp; &nbsp; &bull; <a href='product-add.php'> Add </a><br>";
        }
    }else{
    $productsDB = "You have no products listed in your store yet.";
}

?>

As you can see in displaying the data of 2nd table there's a link which is the "Add" and now I want that link function as well..

And I don't know how get the data of 1st table and insert it in 2nd table so that it will this display in my "Product Inventory List"

Please help me.

your link should look like:

<a href='product-add.php?idtoadd=".$id1."'> Add </a>

and in product-add.php you will be able to do:

$sql = "insert ignore into productinformation1 (ProductNo, ProdName)
select ProductNo, ProdName from productinformation
where ProductNo = " . intval($_GET['idtoadd'], 10);

NOTE1: do not use mysql_### functions, use PDO or mysqli

NOTE2: do not use single quotes in HTML attributes, use double quotes

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