dousigan0499 2015-09-22 10:15
浏览 26
已采纳

使用Ajax查看数据库中是否已存在电子邮件

I have a form where a thank you message displays after you have submitted it, however, I want it to alert "invalid email" if the email you have submitted already exists in the database.

Right now it just proceeds to the thank you message (and not displaying the alert) even if the email already exists in the database. I am using ajax to send the form data to the database. What am I doing wrong in the code?

my ajax code:

$(document).ready(function(){
$("#submit").click(function(){

var name = $("#fullname2").val();
var email = $("#fullemail2").val();
var state = $("#selectstate").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'FullName='+ name + '&email='+ email + '&SovereignState='+ state;
if(name==''||email==''||state=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "demo.php",
data: dataString,
cache: false,
success: function( phpSays ){
    if ( phpSays == "OK"){
        alert("invalid email");
    }
    else {
        $('#sinatra2').hide();
        $('#thanks').show();
    }
}
});
}
return false;
});
});

and this is the php:

<?php

define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD','');
define('DB_HOST', '');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) ;

if (!$link) {
    die('Could not connect: ' . mysql_error ()) ;
}

$db_selected = mysql_select_db (DB_NAME, $link) ;

if (!$db_selected) {
    die('Cant use ' . DB_NAME . ': ' . mysql_error()) ;
    }

$new_email = $_POST ['email'];

$sql2 = mysql_query ("SELECT email from Data_2 WHERE email = '$new_email");
$row = mysql_fetch_row($sql2);

if(mysql_num_rows($row) > 0) {

echo 'OK' ;

}
else {

$value = $_POST ['FullName'];
$value2 = $_POST ['email'];
$value3 = $_POST ['SovereignState'];

$sql = "INSERT INTO Data_2 (FullName, email, SovereignState) VALUES     ('$value', '$value2', '$value3')";

if (!mysql_query ($sql)) {
    die('Error: ' . mysql_error());
}
}

mysql_close();
?>
  • 写回答

4条回答 默认 最新

  • douxia2053 2015-09-22 10:23
    关注

    You miss an end quote in your SQL. You need to escape the email to protect from SQL-injections. You use mysql_num_rows() wrong, it accepts the resource. Below is an example to fix all issues:

    $result = mysql_query ("SELECT email from Data_2 WHERE email = '".mysql_real_escape_string($new_email)."'");
    if (mysql_num_rows($result) > 0) {
        echo 'OK' ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用