问题描述
我已经编写了一些代码,我需要登录的用户来更新其详细信息。当用户单击“更新”按钮时,只有一个字段正在更新数据,其余字段则没有。我有6个字段需要更新,一个字段运行良好,而其他5个字段却没有。我不确定下面的代码在哪里错误:
<?PHP
//all errors
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
// Initialize the session
session_start();
// Check if the user is logged in,otherwise redirect to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.PHP");
exit;
}
// Include config file
require_once "config.PHP";
// Define variables and initialize with empty values
$investment = $cleared = $balance = $refnumber = $refbalance = $monthgain = "";
$investment_err = $cleared_err = $balance_err = $refnumber_err = $refbalance_err = $monthgain_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate fields
if(empty(trim($_POST["investment"]))){
$investment_err = "Iv error";
}if(empty(trim($_POST["cleared"]))){
$cleared_err = "Cleared error";
}if(empty(trim($_POST["balance"]))){
$balance_err = "Balance error";
}if(empty(trim($_POST["refnumber"]))){
$refnumber_err = "Ref num error";
}if(empty(trim($_POST["refbalance"]))){
$refbalance_err = "Ref bal error";
}if(empty(trim($_POST["monthgain"]))){
$monthgain_err = "Monthgain error";
} else{
$investment = trim($_POST["investment"]);
$cleared = trim($_POST["cleared"]);
$balance = trim($_POST["balance"]);
$refnumber = trim($_POST["refnumber"]);
$refbalance = trim($_POST["refbalance"]);
$monthgain = trim($_POST["monthgain"]);
}
// Check input errors before updating the database
if(empty($investment_err) && empty($cleared_err) && empty($balance_err) && empty($refnumber_err) && empty($refbalance_err) && empty($monthgain_err)){
// Prepare an update statement
$sql = "UPDATE users SET investment = ? WHERE id = ?";
if($stmt = MysqLi_prepare($link,$sql)){
// Bind variables to the prepared statement as parameters
MysqLi_stmt_bind_param($stmt,"si",$param_investment,$param_id);
// Set parameters
$param_investment = $investment;
$param_cleared = $cleared;
$param_balance = $balance;
$param_refnumber = $refnumber;
$param_refbalance = $refbalance;
$param_monthgain = $monthgain;
$param_id = $_SESSION["id"];
// Attempt to execute the prepared statement
if(MysqLi_stmt_execute($stmt)){
// data updated successfully. Destroy the close page,and redirect to dashboard page
header("location: ../dashboard.PHP");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
MysqLi_stmt_close($stmt);
}
}
// Close connection
MysqLi_close($link);
}
?>
标有 X 的字段是我的问题! 我的代码有什么问题?
解决方法
尝试这样做
css-1dbjc4n
,
您将需要看起来像这样的SQL才能通过单个查询更新多个列。
UPDATE users
SET investment = ?,cleared = ?,whatever = ?,whatelse = ?
WHERE id = ?";
当然,请使用表中的实际列名。