lasha Posted September 2, 2012 Share Posted September 2, 2012 Hello, I have problem and i dont know how to solve it... may be you can help me Okey i have form which include one textfield named "name" in which user must enter his/her name and 5 category checkbox cat1, cat2, cat3, cat4, cat5,. now I want, how many categories (checkbox) are selected so many same name records to be inserted in my database... Sorry for my english, i think i explained everithing normally... Thank you... ! With respect, Lasha Link to comment https://forums.phpfreaks.com/topic/267913-php-mysql-insert-as-many-same-named-record-to-database-as-many-category-check/ Share on other sites More sharing options...
Barand Posted September 2, 2012 Share Posted September 2, 2012 In your form, name the checkboxes "category[]". Those that are checked by the user will be POSTed. To process foreach ($_POST['category') as $category) { // insert in to table } Instead of storing the user names and category names as your example shows, you should store the user and category ids in that table Link to comment https://forums.phpfreaks.com/topic/267913-php-mysql-insert-as-many-same-named-record-to-database-as-many-category-check/#findComment-1374641 Share on other sites More sharing options...
lasha Posted September 2, 2012 Author Share Posted September 2, 2012 Heeey Thank you for reply... I asked for this in many forum and only you answered i'm very pleased Okey, I'm new with php, i wrote this code and dont know how to realise plz help me with this too... here is a code: <?php if (isset($name) && isset($cat1) && isset($cat2) && isset($cat3) && isset($cat4) && isset($cat5)) { $result = mysql_query ("INSERT INTO info (name,cat) VALUES ('$name','$cat')"); if ($result == 'true') {echo "<p class='close'>information added... <a href=" . "javascript:CloseWindow()" . ">Close This Window</a></p>";} else {echo "<p>added! :|</p>";} } else { echo "<p>Enter more information...</p>"; } ?> i don't know... ma be i am going on wrong way... Link to comment https://forums.phpfreaks.com/topic/267913-php-mysql-insert-as-many-same-named-record-to-database-as-many-category-check/#findComment-1374643 Share on other sites More sharing options...
Barand Posted September 2, 2012 Share Posted September 2, 2012 First, have a "category" table. cat_id | cat_name --------|-------------- 1 | Category 1 2 | Category 2 3 | Category 3 4 | Category 4 5 | Category 5 And this is your basic code form.php <?php // connect to your db here $sql = "SELECT cat_id, cat_name FROM category"; $res = mysql_query($sql) or die(mysql_error()); $checkboxes = ''; while (list($id, $cat) = mysql_fetch_row($res)) { $checkboxes .= "<input type='checkbox' name='category[]' value='$id' /> $cat<br />"; } ?> <form method = 'post' action = 'process.php' > Name: <input type='text' name='name' size='50' /><br /> Categories:<br /> <?php echo $checkboxes ?> <input type='submit' name='btnSubmit' value='Submit' /> </form> process.php (you will need to add validation of data etc) <?php // connect to db here if (isset($_POST['category']) && !empty($_POST['name'])) { foreach ($_POST['category'] as $cat) { $values[] = sprintf ("(%d, '%s')", intval($cat), mysql_real_escape_string($_POST['name'])); } $sql = "INSERT INTO info (cat, name) VALUES " . join(',', $values); mysql_query($sql); // insert the rows } header("location: form.php"); ?> Link to comment https://forums.phpfreaks.com/topic/267913-php-mysql-insert-as-many-same-named-record-to-database-as-many-category-check/#findComment-1374651 Share on other sites More sharing options...
lasha Posted September 2, 2012 Author Share Posted September 2, 2012 Oh thank you very much... it is exactly what i wonder to do :DDD Finally my problem is solved... Not enough words to thank you Best wishes, Lasha Link to comment https://forums.phpfreaks.com/topic/267913-php-mysql-insert-as-many-same-named-record-to-database-as-many-category-check/#findComment-1374702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.