我很抱歉提出这么一个狭隘的问题,但我在PHP中有这个代码,它应该更新用户的帐户。没有返回错误,我的IDE也无法识别问题。现在的问题是代码没有更新数据库。我希望我能就这个问题得到一些帮助。
这是我的PHP代码:
<?php
session_start();
$con = mysqli_connect("mysql.serversfree.com", "u190182631_embo", "17011998embo", "u190182631_login");
$username = $_POST['user_name'];
$last = $_POST['lname'];
$first = $_POST['fname'];
$address = $_POST['address'];
$email = $_POST['email'];
$year = $_POST['year'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE users SET last_name = '$last'
WHERE user_name = $_SESSION[user_name]");
mysqli_close($con);
}
?>
如果需要,我的任何HTML表单:
<form method="post" action="update.php">
Username: <input type="text" name="user_name" value="<?php echo $_SESSION['user_name']?>"><br><br>
Email: <input type="text" name="email" value="<?php echo $_SESSION['user_email']?>"><br><br>
Last Name: <input type="text" name="lname" value="<?php echo $_SESSION['last_name']?>"><br><br>
First Name: <input type="text" name="fname" value="<?php echo $_SESSION['first_name']?>"><br><br>
Street Address: <input type="text" name="address" value="<?php echo $_SESSION['address']?>"><br><br>
Year Graduated: <input type="text" name="year" value="<?php echo $_SESSION['year']?>"><br><br>
<input type="submit" value="Update Information"><br>
</form>
<form method="link" action="manage.php">
<input type = "submit" value = "Cancel"><br>
</form>
任何帮助都会很棒!
答案 0 :(得分:1)
试试这个 - 它也有助于防止SQL注入攻击:
$db = new mysqli("mysql.serversfree.com", "u190182631_embo", "17011998embo", "u190182631_login");
$username = $_POST['user_name'];
$last = $_POST['lname'];
$first = $_POST['fname'];
$address = $_POST['address'];
$email = $_POST['email'];
$year = $_POST['year'];
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $db->prepare("UPDATE users SET last_name = ? AND WHERE user_name = ?;");
$stmt->bind_param("ss", $last, $_SESSION['user_name']);
$stmt->execute();
$stmt->close();
}
答案 1 :(得分:1)
这里的一个大问题是你不知道如何自己调试问题,也不知道在请求帮助中包含哪些信息。
没有返回错误
你怎么知道的?您不检查查询中的任何错误。考虑:
$upd="UPDATE users SET last_name = '$last'
WHERE user_name = $_SESSION[user_name]";
if (!mysqli_query($con,$upd)) {
print "query failed: $upd \n\n<br />" . mysqli_error();
}
您已经展示了用于生成表单的代码片段 - 但不是实际发送到浏览器的代码,
正如Fred -ii-所说,你的SQL中没有引用$ _SESSION [user_name]似乎很奇怪。
答案 2 :(得分:0)
试试这个
mysqli_query($con,"UPDATE users SET last_name = '$last' WHERE user_name = {$_SESSION['user_name']}");
答案 3 :(得分:0)
更新此行代码:
mysqli_query($con,"UPDATE users SET last_name = '$last'
WHERE user_name = $_SESSION[user_name]");
使用新的:
mysqli_query($con,"UPDATE users SET last_name = '$last'
WHERE user_name = $_SESSION['user_name']");
希望它能奏效!