Hi,
I've been searching the threads for weeks now looking for a solution for inserting multiple values into MySql field but those that I found (there are many) were too specific, couldn't apply to my case. Please help, so others also could use the examples as an ultimate solution for their checkbox related problems.
I'm trying to make a code that does auto email notification based on categories. User could check the categories that he wants to receive notification from.
I have a form that builds checkboxes dinamically from the table "category". Checkboxes are using the name and id of the categories. (see below)
fields in "category" table: root_id, papa_id, cat_name, add_date
Now when a user check the boxes and hit "Add" it would be saved in table "notice". I'd like to have the checkbox values go to one field: "cat_id" (with "implode" function?)
fields in "notice" table: notice_id, cat_id, country, email
The same form would be used for adding and removing categories with e.g. two buttons: "ADD" "REMOVE". That means when user access the form later the checboxes that he has previously checked should be checked, so he could see which category he is subscribed to.
It would be nice to have an error checking when someone tries to subscribe to the same category like: "You have already subscribed to this cat"
Thanks for the patience and your help.
This is the code that builds the form, should save and remove the categories:
if( isset( $action ) )
{
if( $action == "add" )
{
////// THIS IS THE PART WHERE I NEED YOUR HELP ////////
mysql_query( "INSERT INTO $notice_tbl ( cat_id, country, email ) VALUES ( '$cat_id[]', '$country', '$email' )" ) or error( mysql_error() );
displayHeader( "$ADVT_NAME > Auto Notify" );
echo "Congratulations! This category has been added to you list successfully.\n";
echo "<br><a href=\"./index.php?cat=$cat\"><b>Back</b></a> | <a href=\"./index.php\"><b>Home</b></a></p>\n";
}
else
{
displayHeader( "$ADVT_NAME > Auto Notify" );
echo "Sorry, this category is already existed in your list.\n";
echo "<br><a href=\"./index.php?cat=$cat\"><b>Back</b></a> | <a href=\"./index.php\"><b>Home</b></a></p>\n";
}
displayFooter();
}
}
else
{
displayHeader( "$ADVT_NAME > Auto Notify" );
$cats = mysql_query( "SELECT cat_name, root_id FROM $category_tbl WHERE papa_id='' ORDER BY add_date DESC" ) or error( mysql_error() );
if( mysql_num_rows( $cats ) == 0 )
echo "<p align=\"center\"><font size=\"4\">No Category Found</font></p>\n";
else
{
include("$full_path/templates/member_menu.php");
echo "\n<div align=\"center\">\n";
echo " <center>\n";
echo " <table border=\"0\" cellpaddig=\"0\" cellspacing=\"0\">\n";
echo " <form enctype=\"multipart/form-data\" method=\"post\" action=\"$PHP_SELF?action=add\">\n";
$i = 1;
while( $catsrow = mysql_fetch_array( $cats ) )
{
echo " <tr>\n";
echo " <td>";
echo "<input type=\"checkbox\" name=\"cat_id[]\" value=\"{$catsrow['root_id']}\">{$catsrow['cat_name']}\n";
echo " <br> \n";
echo " </td>\n";
echo " </tr>\n";
$i++;
}
echo " <tr>\n";
echo " <td>Country</td>\n";
echo " <td><select size=\"1\" name=\"country\">\n";
echo " <option>Budapest</option>\n";
echo " <option>USA</option>\n";
echo " <option>Canada</option>\n";
echo " <option>Mexico</option>\n";
echo " <option>EU</option>\n";
echo " <option>Hungary</option>\n";
echo " <option>UK</option>\n";
echo " <option>Austria</option>\n";
echo " </select></td>\n";
echo " </tr>\n";
echo " <td><input type=\"submit\" value=\"Submit\">";
echo " </table>\n";
echo " </form>\n";
echo " </center>\n";
echo "</div>\n";
}
displayFooter();
}