我正在学习PHP MySQL。我写了一个HTML表单,在mysql数据库中插入电子邮件和国家/地区名称。我成功了但是有一些问题..我如何检查重复数据并防止插入空数据?
这是我的HTML代码
<form action="add_entry.php" method="post" name="myForm" onsubmit="return validateEmail();">
<input type="email" name="email" class="email" required="required">
<input type="text" name="country">
<input type="submit" class="button" value="Continue" onclick="this.disabled=true; this.value='Loading...';" disabled>
</form>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type="text/javascript">
function validateEmail()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
<script>
$("form > *").change(function() {
var fields = $("form input").not("input[type='submit']");
var filledFields = fields.filter(function() {
return $(this).val().length > 0;
});
if (filledFields.length == fields.length) {
$("input[type='submit']").removeAttr("disabled");
} else {
$("input[type='submit']").attr("disabled", "disabled");
}
});
</script>
这是 add_entry.php 代码
<?php
$con = mysql_connect("localhost","dbusername","dbpassword");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$sql="INSERT INTO dbtablename (id,email, country) VALUES (null,'$_POST[email]','$_POST[country]')";
?>
<?php if (!mysql_query($sql,$con)) : ?>
die('Error: ' . mysql_error());
<?php else : ?>
<center>Success!</center>
<meta http-equiv="refresh" content="0; url=http://mywebsite.com/content.html" />
<?php endif; ?>
<?php
mysql_close($con)
?>
现在我不明白我在add_entry.php中写了两个代码... check duplicate&amp;防止插入空数据代码..
我在Prevent duplicate data being entered into mysql database&amp; StackOverFlow中的Preventing Empty Data from Inserting into database。我看到有人在&#34;防止重复数据代码&#34;是SQL Injectable ..
代码是
$query = mysqli_query($con, "SELECT * FROM emails WHERE email='".$email."'");
if(mysqli_num_rows($query) > 0){
echo "email already exists";
}else{
$sql="INSERT INTO emails (email) VALUES ('".$_POST[email]."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
现在请帮助我,我该如何解决这个问题..
我想要这个,如果有人尝试插入空数据或尝试直接访问 add_entry.php 页面,那么他会自动重定向到表单页面...并在可能的情况下显示警告& #34;你必须写下你的电子邮件才能继续......&#34;
如果找到重复的电子邮件,那么它将不会插入数据库,他会自动重定向到 content.html 页面。
成功插入数据后,他会自动重定向 content.html 页面..
如果专家帮我写这段代码,那将是他的伟大,我希望有人必须帮助我实际学习php / mysql。我也在努力..但我需要你的帮助才能继续学习。我想成为像你这样的专家,并希望帮助其他..请帮助我学习。非常感谢。
答案 0 :(得分:2)
Try it this code will work
<?php
$con = mysql_connect("localhost","dbusername","dbpassword");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$email=$_POST['email'];
$country=$_POST['country'];
$query = mysql_query("SELECT * FROM emails WHERE email='".$email."'",$con);
if(mysql_num_rows($query)==0){
$sql=mysql_query("INSERT INTO dbtablename (email, country) VALUES ('$email','$country')",$con)die('Error: ' . mysql_error());
}else{
echo "email already exists";
}
mysql_close($con)
?>