I have a website www.rupertbj.co.uk and i am just learning PHP and MySQL - the question is this:
I have a database and users have to add their e-mail address to this database to view my site. I set up the database initially with phpMyAdmin. I want to be able write the code so that if the database exists to entry is added, and if it doesn't exist, a new database would be created and the entry added.
Thank you in advance for your help - current code as follows:
<?php
if ((!ereg(".+@.+..+", $user)) || (!ereg("[a-zA-Z0-9_@.-]+$", $user)))
{
print "
<body bgcolor=\"#8080FF\">
<b>Invalid Email address! Please <a href=/index.html>register</a> properly!!!</b>
";
exit;
}
else
{
setcookie( "user", $user, "0" );
$db = "register";
$link = mysql_connect( "localhost:/users/rupertbj/domains/rupertbj.co.uk/sql/mysql.sock", "root" );
if ( !$link ) die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link ) or die( "Couldn't open $db: ".mysql_error() );
$sql = "INSERT INTO user VALUES ( '$user' )";
if ( mysql_query( $sql, $link ) )
{
echo "";
}
else
{
echo "";
}
mysql_close( $link );
}
?>