doutan8775 2018-08-25 04:36
浏览 82
已采纳

我试图在这个PHP代码中进行“用户采取”验证,它给了我这个错误:

this is the code that i used:

<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "1234";
$dbname = "admin";

// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);


$dbunames = mysqli_query("SELECT * FROM entity WHERE username='$username'");



if (mysqli_num_rows($dbunames) > 0 ) { //check if there is already an entry 
for that username
    echo "Already taken";
  }



 else{
 $sql = "INSERT INTO entity (username, password)
 values ('$username','$password')";
 if ($conn->query($sql)){
  echo "New record is inserted sucessfully";
 }
 else{
  echo "Error: ". $sql ."
 ". $conn->error;
}
$conn->close();
}
}
else{
 echo "Password should not be empty";
die();
}
}
 else{
 echo "Username should not be empty";
 die();
 }


 ?>

and this is the error(in the web):

 Warning: mysqli_query() expects at least 2 parameters, 1 given in 
 C:\xampp\htdocs\form\connect.php on line 15

 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null 
 given in C:\xampp\htdocs\form\connect.php on line 19
 New record is inserted sucessfully

i know "the record is inserted sucessfully" but the username is already taken validation is not working. i'm not good at php so i would be pleased if you tell me how to fix this code simply.

please help me!!!

  • 写回答

3条回答 默认 最新

  • doupa8922 2018-08-25 04:41
    关注
    $dbunames = mysqli_query("SELECT * FROM entity WHERE username='$username'");
    

    needs to use the $conn variable like so:

    $dbunames = mysqli_query($conn, "SELECT * FROM entity WHERE username='$username'");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c