svgmx5 Posted June 4, 2010 Share Posted June 4, 2010 I'm having some problems, i have a checklist of items a user can check or uncheck, there are about 20 in total. I want the user to be able to check as many items as they want, and then upon form submission all the items are stored in a database. The database is set up as follows: item id item name item price if the person selects all 20 then all 20 rows should be inserted at the same time. Thats the problem i'm having, i can't insert all of the rows at once. I've tried using the for each loop but its not fully working, i even tried putting them in a $_session array, but th at didn't work quite well either. If anyone can help me out i would truly appreciate it Thanks in advanced! Quote Link to comment Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 Post your code. Also, what type of SQL are you using? There are several ways you could do this, but your code will help narrow down what will work, and what will be best. Quote Link to comment Share on other sites More sharing options...
svgmx5 Posted June 4, 2010 Author Share Posted June 4, 2010 thanks, i'm using MYSQL Database 5.0 (i think its 5.0) The code i'm using is below: Its allot of code, but like i said i have no clue what i'm really doing right now <?php (isset($_POST['apply'])){ $service1 = $_POST['service_']; $service2 = $_POST['service_2']; $service3 = $_POST['service_3']; $service4 = $_POST['service_4']; $service5 = $_POST['service_5']; $service6 = $_POST['service_6']; $service7 = $_POST['service_7']; $service8 = $_POST['service_8']; $service9 = $_POST['service_9']; $service10 = $_POST['service_10']; $service11 = $_POST['service_11']; $service12 = $_POST['service_12']; $service13 = $_POST['service_13']; $service14 = $_POST['service_14']; $service15 = $_POST['service_15']; $service16 = $_POST['service_16']; $service17 = $_POST['service_17']; $service18 = $_POST['service_18']; $service19 = $_POST['service_19']; $service20 = $_POST['service_20']; $service21 = $_POST['service_21']; $service22 = $_POST['service_22']; $service23 = $_POST['service_22']; $service24 = $_POST['service_23']; $service25 = $_POST['service_24']; //Then placing it all in a session array $_SESSION['services']=array(); $temp = array('service1' => $service1, 'service2' => $service2, 'service3' => $service3, 'service4' =>$service4, 'service5' => $service5, 'service6' => $service6, 'service7' => $service7, 'service8' => $service8, 'service9' => $service9, 'service10' =>$service10, 'service11' => $service11, 'service12' => $service12, 'service13' => $service13, 'service14' => $service14, 'service15' => $service15, 'service16' =>$service16, 'service17' => $service17, 'service18' => $service18, 'service19' => $service19, 'service20' => $service20, 'service21' => $service21, 'service22' =>$service22, 'service23' => $service23, 'service24' => $service24, 'service24' => $service24); array_push($_SESSION['services'], $temp); if(isset($_SESSION['services']) && $_SESSION['services']){ foreach($_SESSION['services'] as $k => $v){ $service1 = $v['service1']; $service2 = $v['service2']; $service3 = $v['service3']; $service4 = $v['service4']; $service5 = $v['service5']; $service6 = $v['service6']; $service7 = $v['service7']; $service8 = $v['service8']; $service9 = $v['service9']; $service10 = $v['service10']; $service11 = $v['service11']; $service12 = $v['service12']; $service13 = $v['service13']; $service14 = $v['service14']; $service15 = $v['service15']; $service16 = $v['service16']; $service17 = $v['service17']; $service18 = $v['service18']; $service19 = $v['service19']; $service20 = $v['service20']; $service21 = $v['service21']; $service22 = $v['service22']; $service23 = $v['service23']; $service24 = $v['service24']; $service24 = $v['service24']; $add_services = "INSERT INTO table (service, tracker) VALUES('$k', '$app_tracker')"; $run_q = mysql_query($add_services) or die(mysql_error()); } } } ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 I have no idea why you're using session variables, so I'm ignoring that. $query = "INSERT INTO table (service, tracker) VALUES "; $inserts = array(); for ($i = 1; $i <= 24; $i++){ if (isset($_POST['service_'.$i])){ $inserts[] = "('service_$i', '".$_POST['service_'.$i]."')"; } } if (count($inserts) > 0){ $query .= implode(', ', $inserts); $result = mysql_query($query) or die(mysql_error()); } else{ echo "Nothing checked!"; } Quote Link to comment Share on other sites More sharing options...
svgmx5 Posted June 4, 2010 Author Share Posted June 4, 2010 Awsome, it worked, thanks alot! I don't know why i was using session either, i just tried it Quote Link to comment 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.