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!
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:
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 );
}
}
<?php
include("config.php");
$result = mysql_query("SELECT FROM email_list WHERE email='".$_POSTї'email']."' LIMIT 1");
if( $_POSTї'inout']=="in" )
{
if( ! mysql_num_rows($result) > 0 )
{
//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 );
}
else
{
break;
}
}
else if ( $_POSTї'inout']=="out" )
{
//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ї'email']."' LIMIT 1";
mysql_query( $query, $link )
or die (" Couldn't add data to "email" table: "
.mysql_error() );
mysql_close( $link );
}
header("Location: index.php");
?>