Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > PHP

Reply
 
LinkBack Thread Tools
Old July 15th, 2006, 01:41 PM   #1
Highly Reputable Member
 
masonbarge's Avatar
 

Join Date: Jan 2006
Location: Atlanta GA
Posts: 649
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 masonbarge will become famous soon enough
Uploading form-generated array to MySQL

I have three pertinent tables: articles, categories, and art_cat_id, which is a key table: primary key (art_cat_id) and the id's from the two other tables. It's a many to many relationship.

The content is entered into a form and uploaded in an INSERT query automatically which works fine. My problem is that I want to have multiple categories, generated by checkboxes, upload to the key table with the two main id's in as many rows as there are categories checked.

This also works fine except for two irritating bugs:

Problem 1: I always get an extra insert with a blank category
Problem 2: I always get an error message that I forgot to enter a category, even though the INSERT has performed correctly. I think the php unchecks the check boxes as it makes each query.

The applicable code is:

Code:
     // Check for category.
        if (!empty($_POST['cat[]'])) {
                $cat = $_POST['cat'];
        } else {
                $cat[] = FALSE;
                echo '<p><font color="red">Please enter a category!</font></p>';
        }

*  *  *

 if ($t && $h && $lc && $rc && $cat && $date) { // If everything's OK.

                // Add the article to the  table.
                $query = "INSERT INTO articles (title, news, headline, teaser, teaser_hl, content_left, content_right, date) VALUES ('$t', '$n', '$h', '$te', '$th', '$lc', '$rc', '$date')";
                $result = @mysql_query ($query); // Run the query.
                $aid = @mysql_insert_id(); // Get the article ID.

                if ($aid > 0) { // New article has been added.
                        echo ("The article posted successfully into articles with the title $title and id $aid <br />");  
                                  foreach ($cat as $key =>$value) {
                                     {
                                      $query2 = "INSERT INTO art_cat_id (art_id, cat_id) VALUES ('$aid', '$value')";
                                    $result2 = mysql_query ($query2); 
                                    $acid = @mysql_insert_id();         //Get the ID for the category-article key table.
                                            if ($acid > 0)  {        //Entry to key table has been made.
                                                        echo ("The art_cat_id key table has been updated for this article with category $value  <br />");
                                            } else {
                                                        echo ("The art_cat_id key table was NOT updated.  <br />");    
                                            }        // end of IF entry to key table
                                    }        // end of category value IF inside foreach
                                   }            // end of foreach function                        
                } else { // If first query did not run OK.
                        echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Public message.
                        echo '<p><font color="red">' . mysql_error() . '<br /><br />Query: ' . $query . '</font></p>'; // Debugging message.
                }

        } else { // If one of the data tests failed.
                echo '<p><font color="red">Please try again.</font></p>';
        }

} // End of the main submitted conditional.

*       *          *

<form action="add_article_2.php" method="post">
        <fieldset><legend>Fill out the form to enter an article in the appropriate table of the articles database:</legend>
        
        <p><b>News?</b><input type="radio" name="news" value="0" checked="checked">No</input>
                                        <input type="radio" name="news" value="1" >Yes</input> </p><br />
                                        
        <p>Date (YYYY-MM-DD):</b> <input type="text" name="date" size="10" maxlength="10" value="<?php if (isset($_POST['date'])) echo $_POST['date']; ?>" /></text><br /></p>
        

        <p><b>Category or Categories:</b> 
<input type="checkbox" name="cat[]"value="nut" />Nutrition</input> 
<input type="checkbox" name="cat[]"value="exe" />Exercise</input> 
<input type="checkbox" name="cat[]"value="med" />Medical</input> 
<input type="checkbox" name="cat[]"value="men" />Men's Health</input> 
<input type="checkbox" name="cat[]"value="wom" />Women's</input> 
<input type="checkbox" name="cat[]"value="und" />Under 40</input> 
<input type="checkbox" name="cat[]"value="sen" />Seniors</input> 
<input type="checkbox" name="cat[]"value="lon" />Live Longer</input> 
<input type="checkbox" name="cat[]"value="loo" />Looking Good</input> 
<input type="checkbox" name="cat[]"value="sta" />Getting Started</input> 
<input type="checkbox" name="cat[]"value="str" />Stress</input> 
<input type="checkbox" name="cat[]"value="irr" />Irrel</input> 
<input type="checkbox" name="cat[]"value="mis" />Misc</input>
</p>
masonbarge is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 15th, 2006, 01:46 PM   #2
Highly Reputable Member
 
masonbarge's Avatar
 

Join Date: Jan 2006
Location: Atlanta GA
Posts: 649
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 masonbarge will become famous soon enough
Re: Uploading form-generated array to MySQL

I figured it out about one minute after I posted it. <<
if (!empty($_POST['cat[]'])) >>

should have been

if (!empty($_POST'cat[]'))

I really suffered doing this, LOL, and it was just a dumb little array syntax thing. Hate arrays.
masonbarge is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks

Tags
uploading , formgenerated , array , mysql


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox[] array into mysql... OUT? ppgpilot PHP 1 March 13th, 2008 05:37 PM
FrontPage generated Form code to Dreamweaver femyram HTML, XHTML and CSS 2 October 11th, 2007 03:01 AM
Very confusing mySQL array alexgeek PHP 10 October 8th, 2007 01:29 PM
Another question about uploading images using form AdRock PHP 2 August 2nd, 2006 04:51 AM
array unable to check another array so as to be displayed Ozeona Flash and ActionScript 1 August 5th, 2005 06:26 AM


Search Engine Optimization by vBSEO 3.2.0 RC8