gpt4 book ai didi

PHP 代码不更新数据库,没有 sql 错误,我的 IDE 没有返回任何错误,但是数据库没有更新

转载 作者:行者123 更新时间:2023-11-29 06:42:15 25 4
gpt4 key购买 nike

很抱歉问这么狭隘的问题,但我有这段 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>

任何帮助都会很棒!

最佳答案

试试这个 - 它也有助于抵御 SQL 注入(inject)攻击:

$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();
}

关于PHP 代码不更新数据库,没有 sql 错误,我的 IDE 没有返回任何错误,但是数据库没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com