1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help Needed urgently: check mysql record if already exist

Discussion in 'PHP' started by hallianonline, Jan 22, 2012.

  1. #1
    hello
    I am facing a problem while creating a a code
    I need to add 3 validations into this code
    validations are s follow

    1. check if a url address already exist into database then give an error i.e. URL address already exist in database

    2. check if title row of the database is blank(unable to detect by function it gives error i.e. We cannot add this url because we are unable to detect its title

    3. check if url contains https:// protocol it give error i.e. You are not allowed to add https:// URLs


    My php code is

    $sql="INSERT INTO $tbl_name(webname, urladdr)VALUES('$title', '$webname')";
    $result=mysql_query($sql);
    
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='index.php'>Back to main page</a>";
    }
    
    else {
    echo "ERROR";
    }
    PHP:
    $tbl_name is weblist (which I already defined in function)

    webname, urladdr are rows in that table


    $title is a website title detected by the function

    $webname
    is the URL address of the website


    Please solve my problem as soon as possible
    Thanks in advance
     
    Solved! View solution.
    hallianonline, Jan 22, 2012 IP
  2. #2
    Hello, hallianonline. Are you looking for something like this?

    
    <?php
    
    //connect to db here
    
    $query = mysql_query("SELECT * FROM '$table_name' WHERE `urladdr` = '{$webname}'");
    $rows = mysql_num_rows($query);
    
    if($rows == 1)
    {
        echo 'URL address already exist in database.';
    }
    else
    {
        $result = mysql_fetch_assoc($query);
        if($result['webname'] == '')
        {
            echo 'We cannot add this url because we are unable to detect its title';
        }
        else
        {
            $match = preg_match("/https/i",$webname);
            if($match)
            {
                echo 'You are not allowed to add https:// URLs';
            }
            else
            {
                $query2 = mysql_query("INSERT INTO '$tbl_name'(webname, urladdr)VALUES('{$title}', '{$webname}')");
                if($query2)
                {
                    echo 'Successful. <br /><a href="index.php">Back to main page</a>';
                }
                else
                {
                    echo 'ERROR';
                }
            }
        }
    }
    
    ?>
    
    PHP:
     
    Haxalot, Jan 22, 2012 IP
  3. hallianonline

    hallianonline Active Member

    Messages:
    104
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Yes you are right I need the same thing thanks for solving but I am getting getting errors

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\database\pg2.php on line 45

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\database\pg2.php on line 53


    please resolve
     
    hallianonline, Jan 22, 2012 IP
  4. Haxalot

    Haxalot Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Your query is returning with an error, instead of results which is why you are getting those errors.

    Also what i put above was a bit illogical, so try this instead:
    
    <?php
    
    //connect to db here
    
    $query = mysql_query("SELECT * FROM '$table_name' WHERE `urladdr` = '{$webname}'");
    $rows = mysql_num_rows($query);
    $result = mysql_fetch_assoc($query);
    
    if($rows == 1 && $result['webname'] !== '')
    {
        $match = preg_match("/https/i",$webname);
        if($match)
        {
            echo 'You are not allowed to add https:// URLs';
        }
        else
        {
            $query2 = mysql_query("INSERT INTO '$tbl_name'(webname, urladdr)VALUES('{$title}', '{$webname}')");
            if($query2)
            {
                echo 'Successful. <br /><a href="index.php">Back to main page</a>';
            }
            else
            {
                echo 'ERROR';
            }
        }
    }
    elseif($rows == 1)
    {
        echo 'URL address already exist in database.';
    }
    else
    {
         echo 'We cannot add this url because we are unable to detect its title';
        die(mysql_error());
    }
    
    ?>
    
    PHP:
     
    Haxalot, Jan 23, 2012 IP