我有这段代码,允许用户从网址链接重置帐户
<?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = " ";
$code = $_GET['code'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT com_code FROM user WHERE com_code = ".$_GET['code'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action='reset.php?code=" . $row["com_code"]. "' method='post'>Enter New Password: <input type='text' name='new_password' placeholder='New Password'><br><input type='submit' value='Submit'></form>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = " ";
$pword = $_POST['new_password'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
}
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
?>
我一直收到错误:
警告:mysqli :: query():空查询 第47行/home/u590953899/public_html/notify/reset.php错误 更新记录:
按下提交按钮时,假设更新com_code = $ GET url的数据库
的 BUT
会发生什么是它只重新加载页面,我该如何解决这个问题呢?
答案 0 :(得分:0)
您应该将代码更改为内部isset:
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
答案 1 :(得分:0)
在您的代码中进行以下更改:
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code IS NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
答案 2 :(得分:0)
我们使用IS NULL
来检查mysql中的NULL
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code IS NULL WHERE com_code = $code";
}
在MYSQL中阅读NULL Values
答案 3 :(得分:0)
$_POST['Submit']
,则永远不会设置 name="submit"
。仅拥有type="submit"
或value="submit"
或id="submit"
将无法执行此操作。您需要name
属性。
答案 4 :(得分:0)
首先检查您的输入类型是否为name="Submit"
,如果没有添加它。
在那之后回显您的查询,
if (isset($_POST['Submit'])) {
echo "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
所有代码,即查询执行和消息都应该在同一个if语句中(if(isset($ _ POST [&#39; Submit&#39;])))。
我希望这适合你。