正在检查是否已存在用户名mysql、php

u3r8eeie  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(488)

我无法检查数据库中是否已经存在用户名。我浏览了这里回答的现有问题。他们都没有解决我的问题。当我执行时,它显示“cannotselectusername fromtable”,这是我在die块中给出的。代码如下。

  1. <?php
  2. $username = $_POST['user_name'];
  3. $password = $_POST['pass_word'];
  4. $host = "localhost";
  5. $db_username = "root";
  6. $db_password = "";
  7. $db_name = "my_db";
  8. //create connection
  9. $conn = @new mysqli($host, $db_username, $db_password, $db_name);
  10. if (isset($_POST["submit"]))
  11. {
  12. # code...
  13. //check connection established or not
  14. if ($conn->connect_error)
  15. {
  16. die("Not Connected to DB");
  17. }
  18. else
  19. {
  20. $query = "SELECT 'usernamedb' FROM 'registration' WHERE usernamedb='$username'";
  21. $result = mysqli_query($conn, $query) or die('Cannot select username from table');
  22. if (mysqli_num_rows($result)>0)
  23. {
  24. $msg.="This username already exist. try Another !!";
  25. }
  26. else
  27. {
  28. $insert = "INSERT INTO 'registration'('id', 'usernamedb', 'password') VALUES ([$username],[$password])";
  29. $insert_result = mysqli_query($conn,$insert) or die('INSERTION ERROR');
  30. }
  31. }
  32. $conn->close();
  33. }
  34. ?>

希望有人能回答我。

cbeh67ev

cbeh67ev1#

首先,您不应该使用那些未转义的查询。
但是关于你的问题,你的查询有一个sql错误。您引用了表名。““从‘注册’”应该是“从‘注册’”。

相关问题