gpt4 book ai didi

php - 如何在 PHP 中更新用户帐户信息的多个列

转载 作者:行者123 更新时间:2023-12-04 17:34:57 25 4
gpt4 key购买 nike

我一直在尝试让我的用户更新他们的信息,所以第一个问题是,我不断收到此错误:

Notice: Undefined index: new_user_address in D:\xampp\htdocs\Ecommerce1\resetinfo.php on line 27

但我确保它已在我的代码中定义。下一个错误是这样的:

Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in D:\xampp\htdocs\Ecommerce1\resetinfo.php on line 70

我不确定这个错误是从哪里来的,但我认为它来自第一个错误。现在我将向您展示代码。

有一段时间我做了我的更新声明——而不是:

$sql = "UPDATE users SET 
user_address = '$new_user_address',
user_city = '$new_user_city',
user_country = '$new_user_country',
postal_code = '$new_postal_code'
WHERE id = ?";

我用过:

$sql = "UPDATE users SET 
user_address = ?,
user_city = ?,
user_country = ?,
postal_code = ?'
WHERE id = ?";

它正确地将我重定向到我的个人资料页面,但没有更新数据。

这是我的代码:

<?php
session_start();// Initialize the session

// Check if the user is logged in, if not then redirect to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}

// Include config file
require_once "dbconfig.php";
$query = "SELECT user_address, user_country, user_city, postal_code FROM users WHERE id ='".$_SESSION['id']."';";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$new_user_address = $row['user_address'];
$new_user_country = $row['user_country'];
$new_user_city = $row['user_city'];
$new_postal_code = $row['postal_code'];
}
$new_user_address_err = $new_user_city_err = $new_user_country_err = $new_postal_code_err = "";


// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Validate new password
if(empty(trim($_POST["new_user_address"]))){
$new_user_address_err = "City is empty.";
} else{
$new_user_address = trim($_POST["new_user_address"]);
}

if(empty(trim($_POST["new_user_city"]))){
$new_user_city_err = "City is empty.";
} else{
$new_user_city = trim($_POST["new_user_city"]);
}


if(empty(trim($_POST["new_user_country"]))){
$new_user_country_err = "Country is empty.";
} else{
$new_user_country = trim($_POST["new_user_country"]);
}

if(empty(trim($_POST["new_postal_code"]))){
$new_posta_code_err = "Postal Code is empty.";
}elseif(strlen(trim($_POST["new_postal_code"])) < 6){
$new_postal_code_err = "Postal Code must have 6 characters.";
} else{
$new_postal_code = trim($_POST["new_postal_code"]);
}






// Check input errors before updating the database
// Prepare an update statement
$sql = "UPDATE users SET
user_address = '$new_user_address',
user_city = '$new_user_city',
user_country = '$new_user_country',
postal_code = '$new_postal_code'
WHERE id = ?";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssi", $param_user_address, $param_user_city, $param_user_country, $param_postal_code, $param_id);

// Set parameters
$param_user_address = $new_user_address;
$param_user_city = $new_user_city;
$param_user_country = $new_user_country;
$param_postal_code = $param_postal_code;
$param_id = $_SESSION["id"];

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// After udpating everythin.
header("location: profile.php");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
// Close statement



// Close connection
mysqli_close($link);
}

?>
<br><br><br><br>
<div class="container text-white">
<div class="container px-5">
<h1 class="display-2 black-neon text-center" style="font-family: 'Bungee';">Rest your password!
</h1><br><br>



<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($new_user_address_err)) ? 'has-error' : ''; ?>">
<label>Address</label>
<div class="bg-white d-flex">
<input type="text" class="form-control form-control-lg" name="new_user_city" value="<?php echo $new_user_address; ?>">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $new_user_address_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($new_user_city_err)) ? 'has-error' : ''; ?>">
<label>City</label>
<div class="bg-white d-flex">
<input type="text" class="form-control form-control-lg" name="new_user_city" value="<?php echo $new_user_city; ?>">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $new_user_city_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($new_user_country_err)) ? 'has-error' : ''; ?>">
<label>Country</label>
<div class="bg-white d-flex">
<input type="text" class="form-control form-control-lg" name="new_user_country" value="<?php echo $new_user_country; ?>">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $new_user_country_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($new_postal_code_err)) ? 'has-error' : ''; ?>">
<label>Postal Code</label>
<div class="bg-white d-flex">
<input type="text" class="form-control form-control-lg" name="new_postal_code" value="<?php echo $new_postal_code; ?>">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $new_postal_code_err; ?></span>
</div>

<input class="btn btn-outline-success btn-lg" type="submit" value="reset">

</form>
</div>
</div>
<br><br><br>
<?php
include_once ("footer.php");
?>

这段代码实际上是这段代码的编辑——

<?php
// Initialize the session
require_once ("header.php");

// Check if the user is logged in, if not then redirect to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}

// Include config file
require_once "dbconfig.php";

// Define variables and initialize with empty values
$new_password = $confirm_password = "";
$new_password_err = $confirm_password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Validate new password
if(empty(trim($_POST["new_password"]))){
$new_password_err = "Please enter the new password.";
} elseif(strlen(trim($_POST["new_password"])) < 6){
$new_password_err = "Password must have atleast 6 characters.";
} else{
$new_password = trim($_POST["new_password"]);
}

// Validate confirm password
if(empty(trim($_POST["confirm_password"]))){
$confirm_password_err = "Please confirm the password.";
} else{
$confirm_password = trim($_POST["confirm_password"]);
if(empty($new_password_err) && ($new_password != $confirm_password)){
$confirm_password_err = "Password did not match.";
}
}

// Check input errors before updating the database
if(empty($new_password_err) && empty($confirm_password_err)){
// Prepare an update statement
$sql = "UPDATE users SET password = ? WHERE id = ?";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);

// Set parameters
$param_password = password_hash($new_password, PASSWORD_DEFAULT);
$param_id = $_SESSION["id"];

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Password updated successfully. Destroy the session, and redirect to login page
session_destroy();
header("location: login.php");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);
}

// Close connection
mysqli_close($link);
}
?>
<br><br><br><br>
<div class="container text-white">
<div class="container px-5">
<h1 class="display-2 black-neon text-center" style="font-family: 'Bungee';">Rest your password!
</h1><br><br>



<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($new_password_err)) ? 'has-error' : ''; ?>">

<label>New Password</label>
<div class="bg-white d-flex">
<input type="password" class="form-control form-control-lg" name="new_password" value="<?php echo $new_password; ?>">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $new_password_err; ?></span>
</div>


<div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
<label>Confirm Password</label>
<div class="bg-white d-flex">
<input class="form-control form-control-lg" type="password" name="confirm_password">
<div class="align-items-end"><a class="btn btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">
<span class="oi oi-person" ></span></a></div></div>
<span class="form-text"><?php echo $confirm_password_err; ?></span>
</div>
<input class="btn btn-outline-success btn-lg" type="submit" value="Login">

</form>
</div>
</div>
<br><br><br>
<?php
include_once ("footer.php");
?>

我希望在我的 sql 更新数据时被重定向到配置文件页面。请帮助我!

最佳答案

试试这个:

$sql = "UPDATE users SET 
user_address = ?,
user_city = ?,
user_country = ?,
postal_code = ?'
WHERE id = ?";

把操作顺序改成这样:

// Set parameters
$param_user_address = $new_user_address;
$param_user_city = $new_user_city;
$param_user_country = $new_user_country;
$param_postal_code = $param_postal_code;
$param_id = $_SESSION["id"];
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssi", $param_user_address, $param_user_city, $param_user_country, $param_postal_code, $param_id);

关于php - 如何在 PHP 中更新用户帐户信息的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57066594/

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