2

我正在考虑以 PHP multi_query 的形式完成以下任务。但我试图弄清楚如何将列值从选择查询传递到插入和更新查询。

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

有人有什么建议吗?

4

1 回答 1

1

您需要执行第一个选择查询,然后使用其输出执行第二个和第三个查询。

$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);
}
于 2013-05-24T21:30:30.583 回答