IrixGuy Posted January 26, 2009 Share Posted January 26, 2009 How can I export these results to a MySQL table instead of just echoing them to the webpage? I'm new to the PHP world and I'm dumbfounded here. Thanks in advance for your help. The following script that I have created will pull a list of checkbox items from a MySQL database. I can then select the checkboxes from the list, click "Submit" and then view the results that I checked. <?php include 'dbc.php'; ?> <form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post"> <p> <?php $query = "SELECT * FROM golfcourse"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> <input type="checkbox" name="golfcourse[]" value="<?php echo $line[golfcoursename]?>"><?php echo $line[golfcoursename]?> <br> <?php } ?> <input type="submit" name="btnSubmit" value="Submit" /> </form> <? if(count($_POST) > 0) { print_r($_POST['golfcourse']); echo "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/142433-help-with-exporting-checkbox-results-to-mysql/ Share on other sites More sharing options...
cooldude832 Posted January 26, 2009 Share Posted January 26, 2009 The best way to handle a series of checkboxes is to create them as a post array <input type="checkbox" name="checks[LikesCandy]" value="1" />Likes Candy<br /> <input type="checkbox" name="checks[LikesMilk]" value="1" />Likes Milk<br /> then in your processor you use a foreach loop <?php foreach($_POST['checks'] as $key=>$value){ if($value == "1"){ #Alter Query or do its own insert using $key as the desired field to be updated } } ?> the if statement is not required usually but some browsers will throw all unchecked boxes across, usually only check boxes are passed. Link to comment https://forums.phpfreaks.com/topic/142433-help-with-exporting-checkbox-results-to-mysql/#findComment-746309 Share on other sites More sharing options...
IrixGuy Posted January 29, 2009 Author Share Posted January 29, 2009 Thanks again for the help. So I want to post the results into MySQL table "users" field "golfcourseass." How do I accomplish that? Keep in mind that the value listed below pulls from MySQL. More items are being added to the database that it pulls from daily, so I have no idea how many checkboxes it might produce. In case you're wondering what I'm doing, I'm creating a list of golf holes that a player can select from. They click the checkboxes and it adds their selections to a field within their user profile. Please don't laugh at my newness here. Figuring out how to populate the list of checkboxes from the database took me a few days :'( <?php include 'dbc.php'; ?> <form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post"> <p> <?php $query = "SELECT * FROM golfcourse"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> <input type="checkbox" name="checks[LikesCandy]" value="<?php echo $line[golfcoursename]?>"><?php echo $line[golfcoursename]?> <br> <?php } ?> <input type="submit" name="btnSubmit" value="Submit" /> </form> <?php $query = "UPDATE users SET golfcourseass = ('$_POST['checks']') or die(mysql_error())"; ?> Link to comment https://forums.phpfreaks.com/topic/142433-help-with-exporting-checkbox-results-to-mysql/#findComment-749172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.