ebolt007 Posted January 23, 2012 Share Posted January 23, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255619-looping-inside-sql-insert-statement/ Share on other sites More sharing options...
dzelenika Posted January 23, 2012 Share Posted January 23, 2012 does your form using POST or GET? Quote Link to comment https://forums.phpfreaks.com/topic/255619-looping-inside-sql-insert-statement/#findComment-1310450 Share on other sites More sharing options...
ebolt007 Posted January 23, 2012 Author Share Posted January 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255619-looping-inside-sql-insert-statement/#findComment-1310453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.