SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding to database

    Hi

    I am sure this is a simple question to most of you, but is causing me real problems.
    I am creating a kinda news section for my web site using PHP and MYSQL, and want to Categorise every news story to be one or more types of news.
    I am able to enter news stories into the database OK, but am having problems with the type of news issue.

    I am using checkboxes on my submit form. If I check one check box, I want one record inserted into a typeofnews table, but if two check boxes are checked I want two records added to that table. (The typeofnews table is a joining table between a story table and a type table)

    I looked at some of the other posts including the one at http://www.sitepointforums.com/showt...hreadid=25691, but although I thought this was helping me I can't get it to work.

    I wonder if someone could take a look at this code and point me in the right direction.

    PHP Code:
    if ("SUBMIT" == $submitstory) {
        foreach(
    $newstype as $val) {
                
    $sql2 = ("INSERT INTO typeofnews (type_ID, news_ID)
                    VALUES ('
    $val', '$storyid')");
                          if (
    mysql_query($sql2)) {
                            echo(
    "<CENTER>The type of story has been added.</CENTER>");
                
                } else {
                            echo(
    "<P>Error adding type of news: " .
                                 
    mysql_error() . "</P>");
                                          }
                        }
         ) 
    Thanks

    Keith

  2. #2
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What your trying to do sounds ok, but what exactly is your question? Its late and I might have missed it

    I have a question for you though, why does one story need to be a part of more than one group? Seems to me it should be in one or the other....*shug*

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Need a bit more code - where is the $newstype array defined?

    Whenever I do something like this I use variable variables (the thing were if $var = 'bwaf', $$var = whatever $bwaf equals). I'll generate a form with a check box for each of the news types, and name each one "newstypeX" where X is the ID of the news type. Then when the form is submnitted I loop through all of the news types checking to see if the variable $newstypeX (where X is again the ID) has been set using variable variables.

  4. #4
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I am sorry it has taken me so long to reply. I was hit by the Nimda virus.

    Well the good news is that I have fixed the problem and got it to work. The problem was that I was using the wrong type of bracket to close the code shown in my previous message.

    In answer to the other questions. The reason why a news story might need to be in more than one category is because a news story about last weeks World trade centre disarter, for example, might be in the categories of "disarter" and "New York".

    Thanks for trying to help.

    Keith


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •