2

I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.

$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' ;";*/

Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.

Anyone got any suggestions?

1 Answer 1

1

You need to execute select query 1st then use its output to execute 2nd & 3rd 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;" ;

$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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.