rachae1 Posted September 12, 2008 Share Posted September 12, 2008 Hi hopefully this will be easy for somebody to help me with. I have a form that adds products to the database. The form has a drop down box that picks up a category that is already in the database. My form works fine with the option box but now I want to be able to add multiple categories to the product and I was wondering how I change this code to checkboxes: Name:<input type='text' name='name'><br/> Title:<input type='text' name='title'><br/> Category: <select name='tags'> <? $query = mysql_query("SELECT * FROM categorys ORDER BY id ASC") or die(mysql_error()); while($row=mysql_fetch_array($query)) { echo "<option value='$row[id]'>$row[tags]"; } ?> </select> <input name="submit" type="submit" value="add_product" /> </form> I tried to change it to <input type='checkbox' name='tags[]' value='$row[id]'>$row[tags] but it didnt work. Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/ Share on other sites More sharing options...
ratcateme Posted September 12, 2008 Share Posted September 12, 2008 how didn't that work? it looks good to me Scott. Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639664 Share on other sites More sharing options...
rachae1 Posted September 12, 2008 Author Share Posted September 12, 2008 Hi thanks for the reply, Ok I think I have figured part of it out, I have tried the implode function but now it is only posting the first value in the checkboxes. This is my back end code: if ($submit == 'add_product') { foreach($_POST['tags'] as $value) { $value="'".$value."'"; } $tags=implode(",", $_POST['tags']); $name = mysql_real_escape_string(strip_tags($_POST['name'])); $title = mysql_real_escape_string(strip_tags($_POST['title'])); $category = mysql_real_escape_string(strip_tags($_POST['category'])); $date = date("m/d/Y"); $time = time(); //we begin error checking.... $error_msg = array(); if(empty($name)) { $error_msg[] = "Please insert a name!<br />"; } //print the errors, if any if(count($error_msg)>0) { echo "<strong>ERROR:</strong><br>n"; foreach($error_msg as $err) echo "$err"; } //everythings ok, insert it to the DB else { $sql = "INSERT INTO tutorials (submitter, title, cat_id, date_submitted, time_submitted) VALUES ('$name', '$title', '$tags', '$date', '$time')"; mysql_query($sql) or die(mysql_error()); echo "Tutorial added for review!"; } } Do I need to change something in the database at the moment the cat_id is set to int(25) Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639667 Share on other sites More sharing options...
ratcateme Posted September 12, 2008 Share Posted September 12, 2008 this line needs to be change to foreach($_POST['tags'] as &$value) { $value="'".$value."'"; } so $value is passed as reference and is changed back in the original array when you change it and you proberly want to add a escape the tags value before you put it into your database try adding this to the script $tags = mysql_real_escape_string(strip_tags($tags)); Scott. Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639673 Share on other sites More sharing options...
rachae1 Posted September 12, 2008 Author Share Posted September 12, 2008 Hi, I have just added that code in but I am still only getting one value added under cat_id. :S This is the amended code if ($submit == 'add_product') { foreach($_POST['tags'] as &$value) { $value="'".$value."'"; } $tags=implode(",", $_POST['tags']); $tags = mysql_real_escape_string(strip_tags($tags)); $name = mysql_real_escape_string(strip_tags($_POST['name'])); $title = mysql_real_escape_string(strip_tags($_POST['title'])); $category = mysql_real_escape_string(strip_tags($_POST['category'])); $date = date("m/d/Y"); $time = time(); //we begin error checking.... $error_msg = array(); if(empty($name)) { $error_msg[] = "Please insert a name!<br />"; } //print the errors, if any if(count($error_msg)>0) { echo "<strong>ERROR:</strong><br>n"; foreach($error_msg as $err) echo "$err"; } //everythings ok, insert it to the DB else { $sql = "INSERT INTO tutorials (submitter, title, cat_id, date_submitted, time_submitted) VALUES ('$name', '$title', '$tags', '$date', '$time')"; mysql_query($sql) or die(mysql_error()); echo "Tutorial added for review!"; } } Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639678 Share on other sites More sharing options...
ratcateme Posted September 12, 2008 Share Posted September 12, 2008 i missed what you said about the filed settings you might need to change it to a varchar filed Scott. Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639685 Share on other sites More sharing options...
rachae1 Posted September 12, 2008 Author Share Posted September 12, 2008 Thanks Scot I think its worked I now have the value showing as '3','4' I will now need to figure out how to display this but I will give that a go first. Rach Link to comment https://forums.phpfreaks.com/topic/123910-quick-question-hopefully-replace-option-box-with-checkboxes/#findComment-639691 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.