davidm732 Posted January 19, 2009 Share Posted January 19, 2009 Hi I'm fairly new to PHP, I would like to save the selections of the checkboxes and seperate them by commas to save in a single field in a row of my table. This is my full code and the code highlighted in red is my checkbox code. I think it needs to be an array converted to string...Any help is appreciated, thanks. <? include("config_fnsML.php"); $msg=''; $tdtitle = 'New Member'; $conn = db_connect(); ///////////////////////// if ($action==1){ if ($first){ $sql="INSERT INTO Mail_List( `FirstName`, `LastName`, 'Color' ) VALUES ( '".$first."', '".$last."' '".$color."' )"; if(mysql_query($sql)){ $msg .= 'Member added successfuly'; }else{ $msg .= 'Database ERROR:'.mysql_error(); } }else{ $msg .= 'You did not fill in all the fields, please try again'; } } ///////////////////////// ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title><? echo $tdtitle; ?></title> <link href="styles.css" rel="stylesheet" type="text/css"> <style type="text/css"></style> </head> <body> <br> <form action="<? echo $PHP_SELF; ?>?action=1" method="post" enctype="multipart/form-data" name="form1"> <table border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" width="665"> <tr bgcolor="#F3F3F3"> <td bgcolor="#F3F3F3" class="td1"><? echo $tdtitle; ?></td> </tr> <tr> <td bgcolor="#FFFFFF"><table border="0" align="center" cellpadding="2" cellspacing="0" width="100%"> <tr> <td width="12%" class="txt1">First: </td> <td width="88%"><input name="first" type="text" class="boxes" id="first" value="<? echo $first; ?>" size="70"></td> </tr> <tr> <td width="12%" class="txt2">Last: </td> <td width="88%"><input name="last" type="text" class="boxes" id="last" value="<? echo $last; ?>" size="70"></td> </tr> <td><b>Color</b></td> </tr> <tr> <td><input type="checkbox" id="color" name="color[]" value="red" />red</td> </tr> <tr> <td><input type="checkbox" id="color" name="color[]" value="blue" />blue</td> </tr> <tr> <td><input type="checkbox" id="color" name="color[]" value="orange" />orange</td> <tr> <td><br /> OTHER<input name="color[]" type="text" class="InputForm" id="color" size="25" /></td> </tr></table></td> <tr> <td> </td> <td><input name="Submit" type="submit" class="button1" value="Submit!"></td> </tr> </table></td> </tr> </table> </form> <br> </body> </html> [/] Link to comment https://forums.phpfreaks.com/topic/141466-checkbox-values-saved-into-a-single-database-table-row/ Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Share Posted January 19, 2009 <?php $color = implode(':', $_POST['color']); ?> That will join the color options with a colon so it will be a string like: blue:red:orange:OTHER And to get the values back into an array: <?php $colors = explode(':', $color); ?> Link to comment https://forums.phpfreaks.com/topic/141466-checkbox-values-saved-into-a-single-database-table-row/#findComment-740476 Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 i like to use serialize()/unserialize() as then you don't have to worry about the case where the separator is in one of the values. it uses more character space in the database, but is safer in the long run serialize() -> can convert an array to a string unserialize() -> converts the string back to the array Link to comment https://forums.phpfreaks.com/topic/141466-checkbox-values-saved-into-a-single-database-table-row/#findComment-740485 Share on other sites More sharing options...
davidm732 Posted January 19, 2009 Author Share Posted January 19, 2009 Thank you both. That little implode code had me pulling my hair out! This forum is awesome! Link to comment https://forums.phpfreaks.com/topic/141466-checkbox-values-saved-into-a-single-database-table-row/#findComment-740496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.