dongmi19720408 2015-07-12 03:59
浏览 57
已采纳

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

I'm trying to display an error message next to the username input when there is a duplicate username. It will search through the database when user start to key in a string. If there isn't any duplicate username, it will show a tick beside the username input.

Now the problem is it didnt show the image but show this message beside the input 'Champ 'test' inconnu dans where clause'

How can i fix this? Here's my code.

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条回答 默认 最新

  • doucou1892 2015-07-12 13:12
    关注

    For me also it looks like you have a syntax error in your query:

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

    I guess you are using a string type like CHAR or VARCHAR for your username column in your userinfo table. You need to qoute your strings in SQL queries:

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

    And as Rasclatt mentioned you should bind your variables in sql because of the risk of sql injection its explained here:

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

    i suggest you to use mysqli prepared statements:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化