将Select查询中的列值传递给另一个查询以便在PHP中插入

时间:2013-05-24 21:14:33

标签: php mysql

我在考虑将以下内容作为PHP multi_query来完成。但我想弄清楚如何将列值从select查询传递给insert和update查询。

$query = "SELECT tbl_links.link, link_id
                    FROM tbl_links
                    INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
                    WHERE tbl_items.item_name like '".$items_name[$counter]."'
                                    AND NOT EXISTS (
                                            select link_id 
                                            from tbl_clickedlinks 
                                            where tbl_clickedlinks.link_id = tbl_links.link_id
                                            AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
                                            )
                    limit 0, 1;" ;
            $query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
            $query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/

问题是,我不确定如何将link_id值传递给其他查询。所以我想我可能不得不将查询重新排列成一个,但同样,我只是不确定如何解决这个问题。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:1)

你需要执行select query 1st然后使用它的输出来执行2nd&第三次查询。

$query = "SELECT tbl_links.link, link_id
                    FROM tbl_links
                    INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
                    WHERE tbl_items.item_name like '".$items_name[$counter]."'
                                    AND NOT EXISTS (
                                            select link_id 
                                            from tbl_clickedlinks 
                                            where tbl_clickedlinks.link_id = tbl_links.link_id
                                            AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
                                            )
                    limit 0, 1;" ;

$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
  $query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
  $query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/

  mysql_query($query2);
  mysql_query($query3);
}