Hy我试图在注册表单中验证电子邮件地址。我想检查数据库中是否存在该地址。这是我第一次使用php,我不知道如何操作。我试图添加我的代码和错误消息,如果电子邮件有坏字符。
<?php
$dbhost = "localhost";
$dbusername = "test";
$dbpassword = "tester";
$database_name = "dbtest";
$link = mysql_connect("$dbhost", "$dbusername", "$dbpassword");
if(!$link){ die('Could not connect: ' . mysql_error());}
$db_selected = mysql_select_db("$database_name",$link);
if(!$db_selected){ die('Can\'t use foo: ' . mysql_error());}
$email = strip_tags($_POST['email']);
$verzija = strip_tags($_POST['mydropdown2']);
$model = strip_tags($_POST['mydropdown']);
$sql = mysql_query("INSERT INTO novice (naslov, podnaslov, vsebina, objavljeno)
VALUES ('$email', '$verzija', '$model', now())
");
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
?>
&#13;
<form action="insert.php" method="post">
<label for="model">Phone model:</label>
<select required aria-required="true" id="model" name="mydropdown">
<option value="">Select...</option>
<option value="x">x</option>
<option value="xx">xx</option>
</select>
<label for="version">IOS version:</label>
<select required aria-required="true" id="version" name="mydropdown2">
<option value="">Select...</option>
<option value="ax">ax</option>
<option value="bx">bx</option>
<option value="cx">cx</option>
</select>
<p>
<br>
<label for="emailAddress">Email Address:</label>
<input type="text" id="emailAddress" placeholder="Your email..." name="email" required> </p>
<input type="submit" value="Submit"> </form>
&#13;
制作它。
答案 0 :(得分:5)
现在您正在尝试插入而不是验证!
使用(使用PDO):
$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
$query = $db -> prepare ("SELECT * FROM test WHERE email = :email");
$query -> execute (array (":email" => $email));
$count = $query -> rowCount();
if($count > "0")
{
echo "email already exist";
}
你可以用这个:
filter_var($email, FILTER_VALIDATE_EMAIL);
验证电子邮件格式。