Apart from the obvious syntax error and the SQL injection/escaping problem, your not validating user input or error handling.
Unfortunately as it stands with this no protection approach; your find your database quickly fills with viagra, cheap watches, and clothing keywords and XSS codes in no time.
Here is a port/example using PDO prepared query's using much safer code, to make any form even safer you should add a captcha:
<?php
session_start();
try {
$db = new PDO('mysql:host=127.0.0.1;dbname=email_database','root','****');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->exec("SET CHARACTER SET utf8");
} catch(PDOException $e) {
echo 'Database connection error :' . $e->getMessage();
}
if($_SERVER['REQUEST_METHOD'] === 'POST'){
//csrf token
if(!isset($_SESSION['token']) || !isset($_POST['token']) || $_SESSION['token'] != $_POST['token'])
exit('Invalid request token');
//Validate input values
$error = array();
//Name
if(empty($_POST['name'])){
$error['name'] = "Your name is required";
}
//Email
if(!empty($_POST['email'])){
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$error['email'] = "A Valid email is required";
}
}else{
$error['email'] = "Your email is required";
}
//ok no error lets insert
if(empty($error)){
$sql = 'INSERT INTO client_data (name, email) VALUES (:name, :email)';
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->execute();
}
//Token used for this request, remove
unset($_SESSION['token']);
}
//set csrf token
$_SESSION['token'] = hash('sha256', uniqid());
?>
<form action="" method="POST">
<input type="hidden" name="token" value="<?php echo $_SESSION['token'];?>"/>
Name *: <input type="text" name="name"/> <?php echo isset($error['name']) ? $error['name'] : null;?><br/>
Email *: <input type="text" name="email"/> <?php echo isset($error['email']) ? $error['email'] : null;?><br/>
<input type="submit" value="Submit"/>
</form>
Don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.