【问题标题】:Inserting selected data from table to another table将选定数据从表插入到另一个表
【发布时间】:2013-10-31 20:04:29
【问题描述】:

我想将 table1 中的选定数据添加到另一个 table2 这是我假设的输出。

产品库存清单

产品列表

在“产品列表”中 我有一个列出的产品,然后如果我单击“添加”链接,数据应列在“产品库存列表”中

无论如何,这是我显示第一个表数据的代码

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

?>

正如您在显示第二张表的数据时所见,有一个链接是“添加” 现在我也想要那个链接功能..

我不知道如何获取第一个表的数据并将其插入到第二个表中,以便它显示在我的“产品库存列表”中

请帮帮我。

【问题讨论】:

    标签: php sql


    【解决方案1】:

    您的链接应如下所示:

    <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属性中使用单引号,使用双引号

    【讨论】:

    • productinformation1 中什么也没发生 :( 我把代码 in product-add.php @llya
    • @JoemarAlfeche 如果什么都没发生 - 那么发生了一些错误,你执行了存储在变量中的 sql 吗?
    • @llyaBursov 是的,先生。表 1 中的值即产品信息已显示,但是当我单击添加时,它的某些数据并未存储在表 2 中,即产品信息 1。
    • @JoemarAlfeche 在 sql 执行期间如何检查错误?
    • @llyaBursov 你的意思是使用错误处理?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2020-09-28
    • 2015-10-08
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多