我正在尝试使用php和mysql创建一个登录和注册页面,但是遇到了一些问题。
我引用了this video,下面的代码(该代码不完整,我只是为注册而做)。
所以问题是当我使用注册方提交条目时,数据库显示空白记录。我尝试了类似的方法
$reg = "INSERT INTO usertable (user,pwd) values ('".$user."','".$pwd."')";
但是它不起作用,当我这样做时:
$reg = "INSERT INTO usertable (user,pwd) values ('ABC','1234')";
有效。我应该怎么做才能使用输入文本插入条目?
<?php
session_start();
$conn = mysqli_connect('localhost', 'root', '1234');
mysqli_select_db($conn,'registeration');
$user = $_POST["user"];
$pwd = $_POST["pwd"];
$s = "select * from usertable where user = '$user'";
$result = mysqli_query($conn,$s);
$num = mysqli_num_rows($result);
if($num == 1){
echo"Username Already Taken";
}
else{
$reg = "INSERT INTO usertable (user,pwd) values ('$user','$pwd')";
mysqli_query($conn,$reg);
}
?>
<h2> Register Here </h2>
<form action="index.html" method="post">
<label>Username</label>
<input type="text" name="user" required>
<label>Password</label>
<input type="text" name="pwd" required>
<br><button type="submit"> Login </button>
</form>
答案 0 :(得分:0)
一种简单的方法来了解程序或您编写的完整SQL查询字符串。 $ reg =“在用户可用的(user,pwd)值中插入('$ user','$ pwd')值”; 回声$ reg
最好使用{}代替。将变量插入字符串。 $ reg =“在用户可用(user,pwd)值中插入('{$ user}','{$ pwd}')值”;
为确保POST / GET操作目标正确, 对于您当前的代码,我不起诉index.html可以处理它,可能应该是$ _SERVER [“ PHP_SELF”]
要了解PHP字符串运算符,请参阅 https://www.php.net/manual/zh/language.operators.string.php
答案 1 :(得分:0)
我发现了我的错误。我写了我的php和register表(一起在registration.php中),但是我的表单将我重定向到index.html。我将php复制并粘贴到index.php中(更改了名称),并且可以正常工作。
感谢那些帮助过我的人!
<h2> Register Here </h2>
<form action="index.php" method="post">
<label>Username</label>
<input type="text" name="user" required>
<label>Password</label>
<input type="text" name="pwd" required>
<br><button type="submit"> Login </button>
</form>