簡體   English   中英

將選定的數據從表插入到另一個表

[英]Inserting selected data from table to another table

我想將表1中的選定數據添加到另一個表2中,這是我的假定輸出。

產品庫存清單<-顯示第二個表中添加的產品

產品列表<-此列表顯示第一張表中的產品

在“產品列表”中,我有一個列出的產品,然后,如果單擊“添加”鏈接,則數據應列在“產品庫存列表”中

無論如何,這是我的代碼,用於顯示第一張表的數據

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

?>

這是我的代碼,用於顯示第二張表的數據

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

?>

正如您在顯示第二個表的數據中看到的那樣,有一個鏈接是“添加”,現在我也希望該鏈接功能。

而且我不知道如何獲取第一張表的數據並將其插入第二張表,以便將其顯示在我的“產品庫存清單”中

請幫我。

您的鏈接應如下所示:

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

在product-add.php中,您可以執行以下操作:

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

注意1:不要使用mysql _ ###函數,請使用PDO或mysqli

注意2:不要在HTML屬性中使用單引號,而應使用雙引號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM