gpt4 book ai didi

php - Jquery 验证检查数据库中是否存在值

转载 作者:行者123 更新时间:2023-11-29 12:30:29 25 4
gpt4 key购买 nike

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

<?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验证代码

<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 中的实际形式

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

<?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;

关于php - Jquery 验证检查数据库中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com