jQuery验证检查数据库中是否存在值 [英] Jquery validation checking if value exists in database

查看:116
本文介绍了jQuery验证检查数据库中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我正在尝试通过jquery验证插件检查Patch_No的值是否存在.我通读了许多其他类似的主题,这就是我弄清楚代码的方式,但是尽管没有错误,但似乎无法正常工作. 我很确定DBconnection.php可以正常工作,因此我正在另一个文件中使用它来输入数据.

As the title says I'm trying to check via jquery validation plugin if value for Patch_No exists. I read through many other similar topics and that's how I figured the code but it seems not to be working although there are no errors. I'm pretty sure DBconnection.php works hence I'm using it in another file to input data.

<?php
include 'DBconnection.php';
$patch = $_REQUEST['patch'];
$sql="SELECT * FROM champions WHERE Patch_No ='$patch'";
$conn->query($sql);
$num = mysql_num_rows($sql);
if($num>0){
    echo 'false';
}
else{
    echo 'true';
}
?>

这也是jquery验证码

Here is also the jquery validation code

<script>
    $(document).ready(function(){
        $('#first_form').validate({
            rules: {
                patch: {
                    required: true,
                    minlength: 4,
                    remote: {
                        url: "checkpatch.php",
                        type: "post"
                        }
                    }
                },
            messages: {
                patch: {
                    required: "Please enter patch version.",
                    remote: "This Patch already exists."
                    }

                }
        });
    });
</script>

最后但并非最不重要的一点是php中的实际形式

And last but not least the actual form in php

echo '<form id="first_form" action="index.php" method="POST" style="border: 0; margin: 0;">';
                echo '<input type="hidden" value="'.$formid.'" name="formid">';
                echo '<input type="text" name="patch" placeholder="PATCH e.g. 4.20" required autofocus><br/>';
                echo '<input placeholder="Number of Champions" value="1" type="number" name="champ_number" min="1" max="99" required><br/>';
                echo '<input type="submit" value="next">';
            echo '</form>';

编辑 DBconnection.php

EDIT DBconnection.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "patches";

$conn = new mysqli($servername, $username, $password, $db);
if($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}
?>

推荐答案

尝试如下:

$sql="SELECT * FROM champions WHERE Patch_No ='".$patch."'";
$result = $conn->query($sql);
$num = $result->num_rows;

这篇关于jQuery验证检查数据库中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆