Jump to content

Looping inside SQL insert statement


ebolt007

Recommended Posts

I have a bunch of checkboxes I am pulling from a database, like so.

<?$true_query = mssql_query("SELECT * FROM checkbox_datbase ORDER BY ID ASC");
                  	while ($true_row = mssql_fetch_assoc($true_query))
                  	{
                  		$eth_id = $true_row['ID'];
                  		$eth_name = $true_row['Value'];
                  		echo "<input type=\"checkbox\" name=\"eth_$eth_id\" value=\"1\" class=\"input\" id=\"Ethnic_01\"/>$eth_name <br />";
                  		}
?>

How do I put these into a database that I have settup where it needs to match up with a userid. I am trying to do this, and can get it to look correctly by echoing out the various parts, but I can't put while loops into a variable, right? So how would I get all of this into one line in my $sql variable?

 

echo "INSERT INTO new_database (UserId,"; 
                  $true_query = mssql_query("SELECT * FROM checkbox_datbase ORDER BY ID ASC");
                  	while ($true_row = mssql_fetch_assoc($true_query))
                  	{
                  		$eth_id = $true_row['ID'];
                  		$eth_eth_id = $_POST['eth_' . $eth_id . ''];
                       echo " $eth_id, ";
                      }
                      echo "DateUpdated) VALUES ('$user_ID', ";
                     
                    $true_query = mssql_query("SELECT * FROM checkbox_datbase ORDER BY ID ASC");
                  	while ($true_row = mssql_fetch_assoc($true_query))
                  	{
                  		$eth_id = $true_row['ID'];
                  		$eth_eth_id = $_POST['eth_' . $eth_id . ''];
                       echo "'$eth_eth_id', ";
                      }
                     echo "'$currenttime')";

 

This echoes something that looks like this

INSERT INTO user_ethnicity (UserId, 1, 2, 3, 4, 5, 6, 7, 8, DateUpdated) VALUES ('', '', '', '', '', '', '', '', '', '2012-01-23 13:24:18 PM')

, which I need the actual statement to be

$sql = "INSERT INTO user_ethnicity (UserId, 1, 2, 3, 4, 5, 6, 7, 8, DateUpdated) VALUES ('259', '', '', '1', '', '1', '', '', '', '2012-01-23 13:24:18 PM')";

 

Can anyone help me with this?

 

Link to comment
https://forums.phpfreaks.com/topic/255619-looping-inside-sql-insert-statement/
Share on other sites

It uses $POST. I've been reading more and it seems like you can do a MSSQL WHILE loop inside the statement but I still wouldn't know how to pull these variables or set it up.

Sorry the above part should actually show

INSERT INTO user_ethnicity (UserId, 1, 2, 3, 4, 5, 6, 7, 8, DateUpdated) VALUES ('259', '', '', '1', '', '1', '', '', '', '2012-01-23 13:24:18 PM')

, which I need the actual statement to be

$sql = "INSERT INTO user_ethnicity (UserId, 1, 2, 3, 4, 5, 6, 7, 8, DateUpdated) VALUES ('259', '', '', '1', '', '1', '', '', '', '2012-01-23 13:24:18 PM')";

 

I just used this as an example and didn';t put the UserID or the 1's in from the post as if user 259 selected 2 chackboxes. Just want to make sure I have people going in the correct direction, I can get the $posts to work, it's just how do I loop inside the sql insert statement.

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.