INSERT INTO mysql php question

Discussion in 'PHP' started by cgo85, Sep 8, 2008.

  1. #1
    I have a php mysql tag cloud setup for my website. The issue is that it's storing stuff in database that have no values. How can I stop it from doing this. I need a command that will tell it to only store the info if there is a value for field 'term'... here is a snippet of the code.

    $parse = parse_url($_SERVER['HTTP_REFERER']);
    $se = $parse["host"];
    $raw_var = explode("&", $parse["query"] );
    foreach ($raw_var as $one_var) {
        $raw = explode("=", $one_var);
        $var[$raw[0]] = urldecode ($raw[1]);
    }
    $se = explode (".", $se);
    switch ($se[1]) {
        case 'yahoo':
            $keywords = $var['p'];
            break;
        case 'aol':
            $keywords = $var['query'];
            break;
        default:
            $keywords = $var['q'];
    }
    unset($parse, $se, $raw_var, $one_var, $var);
    
    mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
    PHP:

     
    cgo85, Sep 8, 2008 IP
  2. chanakya

    chanakya Peon

    Messages:
    361
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if ($keywords <> '')
    {
    mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
    }
    
    PHP:
     
    chanakya, Sep 8, 2008 IP
  3. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #3
    ^above

    or you could do

    if(strlen($keyword) > 3)
    {
    ...insert into database
    }

    that way single letters won't show up as tags.
     
    clinton, Sep 8, 2008 IP
  4. forall

    forall Well-Known Member

    Messages:
    176
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Many web developer use this, instead of <>''
    Anyway both will give same result.
    
    if (!empty($keyword))
    {
    mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
    }
    
    PHP:
    Or you can use more advantage option with set of minimum string lenght, that have been posted by clinton
     
    forall, Sep 9, 2008 IP
  5. The Universes

    The Universes Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The Universes, Sep 9, 2008 IP
  6. kapinder

    kapinder Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can also use

    if (trim($keywords) <> '')
    {
    mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
    }

    trim function removes the space at the both ends.
     
    kapinder, Sep 9, 2008 IP