Uploading form-generated array to MySQL

This is a discussion on "Uploading form-generated array to MySQL" within the PHP Forum section. This forum, and the thread "Uploading form-generated array to MySQL are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 15th, 2006, 17:41
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
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: Select all
     // 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>
Reply With Quote

  #2 (permalink)  
Old Jul 15th, 2006, 17:46
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
Reply

Tags
uploading, formgenerated, array, mysql

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 Forum 1 Mar 13th, 2008 20:37
FrontPage generated Form code to Dreamweaver femyram Web Page Design 2 Oct 11th, 2007 07:01
Very confusing mySQL array alexgeek PHP Forum 10 Oct 8th, 2007 17:29
Another question about uploading images using form AdRock PHP Forum 2 Aug 2nd, 2006 08:51
array unable to check another array so as to be displayed Ozeona Flash & Multimedia Forum 1 Aug 5th, 2005 10:26


All times are GMT. The time now is 11:48.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43