If value exist i table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

If value exist i table

Post by Zeceer »

Hi,

I'm making this mailing list for my webshop, and when a user types in an e-mail the script should check is the e-mail already exist in the table. Is there any easy way of doing this? I've tried with the SELECT function inside a if variable, but with any luck.

The part of the script where the if exist have to be:

Code: Select all

if( $_POSTї'inout']=="in" )
{
     if( hghfghgh )
     {
     //Connects to the database

     $link = mysql_connect( $hostname, $username, $password );

     mysql_select_db( $db )
                 or die ( "Couldn't open $db: ".mysql_error() );

     //The query adding the data to the database

     $query = "INSERT INTO email_list ( `email` )
          VALUES( '".$_POSTї'email']."' )";

     mysql_query( $query, $link )
             or die (" Couldn't add data to "email_list" table: "
             .mysql_error() );

     mysql_close( $link );
     }
}
Thanx
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php

// Check to see if the email address already exists.

$email = "someone@somewhere.com";

$result = mysql_query("SELECT `email` FROM `email_list` WHERE `email`='{$email}' LIMIT 1");

if(mysql_num_rows($result) > 0)
{
    // Email address already exists
}
else
{
    // Email address does not exists
}

?>
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Thanx. But I stille get an error? Heres the whole script:

Code: Select all

<?php

include("config.php");

$result = mysql_query("SELECT FROM email_list WHERE email='".$_POST&#1111;'email']."' LIMIT 1");

if( $_POST&#1111;'inout']=="in" )
&#123;
     if( ! mysql_num_rows($result) > 0 )
     &#123;
         //Connects to the database

         $link = mysql_connect( $hostname, $username, $password );

         mysql_select_db( $db )
                      or die ( "Couldn't open $db: ".mysql_error() );

         //The query adding the data to the database

         $query = "INSERT INTO email_list ( `email` )
         VALUES( '".$_POST&#1111;'email']."' )";

         mysql_query( $query, $link )
                      or die (" Couldn't add data to "email_list" table: "
                      .mysql_error() );

         mysql_close( $link );
     &#125;
     else
     &#123;
         break;
     &#125;
&#125;
else if ( $_POST&#1111;'inout']=="out" )
&#123;
    //Connects to the database

    $link = mysql_connect( $hostname, $username, $password );

    mysql_select_db( $db )
                     or die ( "Couldn't open $db: ".mysql_error() );

    //The query deleting the data to the database

    $query = "DELETE FROM email_list WHERE email='".$_POST&#1111;'email']."' LIMIT 1";

    mysql_query( $query, $link )
                 or die (" Couldn't add data to "email" table: "
                 .mysql_error() );

    mysql_close( $link );
&#125;

header("Location: index.php");

?>
The error:

Code: Select all

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\Web Server\inc shop v2.0\email_list_inout.php on line 9
I really can't see anything wrong about it :?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

errr.....you need to connect to the database before mysql_num_rows, not after it.

Mark
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Damn! am I stupid or what 8O . Well, thanx a lot!
Post Reply