如何在PHP中更新用户帐户信息的多列
我一直在尝试让我的用户更新他们的信息,因此第一个问题是,我不断遇到以下错误: 注意:未定义索引:第27行D:\xampp\htdocs\Ecommerce1\resetinfo.php中的新用户地址 但我确保它是在我的代码中定义的。下一个错误是: 警告:mysqli_stmt_bind_param():变量数量与第70行D:\xampp\htdocs\Ecommerce1\resetinfo.php中准备语句中的参数数量不匹配 我不确定这个错误是从哪里来的,但我假设它来自第一个错误。现在我将向您展示代码 曾经有一段时间,我发表了我的更新声明,而不是:如何在PHP中更新用户帐户信息的多列,php,sql,Php,Sql,我一直在尝试让我的用户更新他们的信息,因此第一个问题是,我不断遇到以下错误: 注意:未定义索引:第27行D:\xampp\htdocs\Ecommerce1\resetinfo.php中的新用户地址 但我确保它是在我的代码中定义的。下一个错误是: 警告:mysqli_stmt_bind_param():变量数量与第70行D:\xampp\htdocs\Ecommerce1\resetinfo.php中准备语句中的参数数量不匹配 我不确定这个错误是从哪里来的,但我假设它来自第一个错误。现在我将向您
$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");
?>
试试这个:
$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);
当我使用:$sql=“UPDATE users SET user_address=?,user_city=?,user_country=?,postal_code=?”,其中id=?”;城市和国家得到更新,而地址和邮政编码没有得到更新,关于“未定义索引”错误:我相信你会得到这个错误,因为你在检查值是否为空之前尝试trim()
值。分配值并将其设置为绑定sql时的顺序很重要。它工作正常,我的新邮政编码不断更新为-Empty字段(例如,我在myphpadmin:postall\u code-3322SS中设置,但当我使用resetinfo.php时,它会给出一个空字段),您能帮我找到错误的位置吗?其他字段正在升级。这只是邮政编码。编辑:我发现了错误,这是邮政编码$param,谢谢您的帮助,先生!!!
// 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);