Jump to content

WHILE LOOP INSERT query


tta013

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.