使用Ajax和PHP搜索重复的用户并将图像发送到页面

时间:2015-07-12 03:59:24

标签: php jquery mysql ajax

当用户名重复时,我正在尝试在用户名输入旁边显示错误消息。当用户开始键入字符串时,它将搜索数据库。如果没有任何重复的用户名,则会在用户名输入旁边显示一个勾号。

现在的问题是它没有显示图像,但在输入'Champ'测试旁边显示此消息'inconnu dans where clause'

我该如何解决这个问题?这是我的代码。

register.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video Game Rental - Sign up</title>
<link href="css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>

<SCRIPT type="text/javascript">

pic1 = new Image(16, 16); 
pic1.src = "pic/loader.gif";

$(document).ready(function(){

$("#username").change(function() { 

var usr = $("#username").val();

if(usr.length >= 1)
{
$("#status").html('<img src="pic/loader.gif" align="absmiddle">');

    $.ajax({  
    type: "POST",  
    url: "check.php",  
    data: "username="+ usr,  
    success: function(msg){  

   $("#status").ajaxComplete(function(event, request, settings){ 

    if(msg == 'OK')
    { 
        $("#username").removeClass('object_error'); // if necessary
        $("#username").addClass("object_ok");
        $(this).html('&nbsp;<img src="pic/tick.gif" align="absmiddle">');
    }  
    else  
    {  
        $("#username").removeClass('object_ok'); // if necessary
        $("#username").addClass("object_error");
        $(this).html(msg);
    }  

   });

 } 

  }); 

}

});

});

//-->
</SCRIPT>


</head>

<body>
<div id="whitebox">
<form action="record.php" method="post" enctype="multipart/form-data" onSubmit="return myFunction(this);">

<table>
    <tr>
        <th colspan="3">CREATE AN ACCOUNT</th>
    </tr>
    <tr>
        <td width="200"><div align="right">&nbsp;</div></td>
        <td width="100"><input name="username" type="text" id="username" placeholder="Username" size="20"></td>
        <td width="400" align="left"><div id="status"></div></td>
    </tr> 

    <tr>
        <td width="200"><div align="right">&nbsp;</div></td>
        <td width="100"><input name="password" type="text" id="password" placeholder="Password" size="20"></td>
        <td width="400" align="left"><div id="status"></div></td>
    </tr> 

    <tr>
        <td width="200"><div align="right">&nbsp;</div></td>
        <td width="100"><input name="cpassword" type="text" id="cpassword" placeholder="Confirm Password" size="20"></td>
        <td width="400" align="left"><div id="status"></div></td>
    </tr> 
    <tr>
       <td colspan="3"><br> Profile picture:<br>
       <input type="file" name="fileToUpload" id="fileToUpload"> </td>         
    </tr>
</table>

<script>
    function myFunction() {
        var pass1 = document.getElementById("password").value;
        var pass2 = document.getElementById("cpassword").value;
        var ok = true;
        if(pass1 != pass2)
        {
            document.getElementById("password").style.borderColor = "#E34234";
            document.getElementById("cpassword").style.borderColor = "#E34234";
            ok = false; 
        }
        return ok;
    }

    function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}    
</script>
<input name="register" type="submit" class="register" id="register" value="Register">
</form>
<form action="index.php">
    <input name="back" type="submit" class="register" id="back" value="Back">
</form>


</div>
</body>
</html>

check.php

<?php
if(isSet($_POST['username']))
{
$username = $_POST['username'];

$conn = mysqli_connect("localhost", "root", "", "videorental");
$query ="SELECT * FROM userinfo WHERE username = $username";
$sql_check = mysqli_query($conn, $query) or die(mysqli_error($conn));

if(mysqli_num_rows($conn, $sql_check))
{
echo '<br><font color="red">The nickname <STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}

?>

1 个答案:

答案 0 :(得分:1)

对我来说,您的查询中似乎也有语法错误:

$query ="SELECT * FROM userinfo WHERE username = $username";

我猜你在userinfo表中使用像CHAR或VARCHAR这样的字符串类型作为用户名列。您需要在SQL查询中输入字符串:

$query ="SELECT * FROM userinfo WHERE username = '$username'";

正如Rasclatt所说,你应该在sql中绑定你的变量,因为这里有解释sql的风险:

http://php.net/manual/en/security.database.sql-injection.php

我建议你使用mysqli预备语句:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php