tta013 Posted August 18, 2008 Share Posted August 18, 2008 Hi all, I had submitted the same question a few weeks back but I don't think I gave the whole picture of the problem. Basically I have a feedback form. So when a client click on a create a review, I want the client to fill out the questionnaire for the clubs that they have joined. (There are about 10 different clubs). So here is the code: <div> <?php if ($_REQUEST['action'] == 'create') { create_review($_REQUEST['id']); } ?> </div> --- On the another page, I have the following bit: function create_review($id) { $club_id = array('A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 9, 'I' => 10, 'J' => 11, 'K' => 12); $sql = "INSERT INTO Review (booking_id, contact_id, start_date, pause_date, finish_date, stage, client_info) VALUES "; $sql .= " (" . $id . ", " . $_SESSION['contact_id'] . ", CURDATE(), NULL, NULL, 0, '" . mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']) . "')"; $result = mysql_query($sql) or die(mysql_error()); $sql = "SELECT activity FROM Activities WHERE booking_id = " . $id . " AND activity IN ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')"; $result = mysql_query($sql) or die(mysql_error()); //this is where i think not working if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $sql = "INSERT INTO Club_Review (booking_id, club_id) VALUES (" . $id . ", " . $club_id[strtoupper($row['activity'])] . ")"; $result = mysql_query($sql) or die(mysql_error()); } } $sql = "INSERT INTO Review_Response (booking_id) VALUES (" . $id . ")"; $result = mysql_query($sql) or die(mysql_error()); } ---- The problem i have is when a client has booked more than one clubs, the program will only INSERT a random club into Club_Review instead of all the clubs the client has booked. I was wondering if someone can help me on this please. I must admit I am a PHP newbie. Regards, Link to comment https://forums.phpfreaks.com/topic/120164-while-loop-insert-query/ Share on other sites More sharing options...
Dowdy Posted August 18, 2008 Share Posted August 18, 2008 If you want each of the clubs on separate rows you could use foreach($ClubID as $Club) { //INSERT QUERY HERE } Link to comment https://forums.phpfreaks.com/topic/120164-while-loop-insert-query/#findComment-619124 Share on other sites More sharing options...
tta013 Posted August 18, 2008 Author Share Posted August 18, 2008 Dear Dowdy, Many thanks for your reply. I would def like each of the clubs on the seperate rows. Could you please calrify a litte bit for me of your reply. Are you saying I replace the WHILE loop with FOR Loop? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/120164-while-loop-insert-query/#findComment-619147 Share on other sites More sharing options...
Dowdy Posted August 18, 2008 Share Posted August 18, 2008 yes replace while with foreach (for each acts differently to a standard for loop). You will need to keep the $row=mysql_fetch_assoc() though as you use it in your query (put the foreach on the line below it). Are you getting the club id from a table? As I don't see it in the code other than in the array at the top Link to comment https://forums.phpfreaks.com/topic/120164-while-loop-insert-query/#findComment-619173 Share on other sites More sharing options...
tta013 Posted August 18, 2008 Author Share Posted August 18, 2008 You get the club ID from the array on the first line.... Link to comment https://forums.phpfreaks.com/topic/120164-while-loop-insert-query/#findComment-619208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.