Моя форма обновления не обновляет мою базу данных, когда я нажимаю кнопку обновления.
Это веб-сайт управления пользователями, который я создаю, где пользователям необходимо обновить свой профиль после просмотра его на другой странице. .
Когда пользователь нажимает на ссылку обновления, должно произойти следующее:
- Переменная
id
загружается в URL, который перенаправляет на страницу обновления. - Затем, основываясь на этом, веб-страница обновляет данные пользователя в базе данных.
<?php
require('config.php');
}
// Define variables and initialize with empty values
$username = $email = $fullname = $phonenumber = $age = $city = $area
= $dateofbirth = $nationality = $about = $qualifications =
$interests = "";
if(isset($_POST["id"]) && !empty($_POST["id"])){
$id = $_POST["id"];
$username = trim($_POST["username"]);
if(empty($input_name)){
$name_err = "Please enter a name.";
} else{
$name = $input_name;
}
$email = trim($_POST["email"]);
if(empty($input_email)){
$email_err = "Please enter a email.";
} else{
$email = $input_email;
}
$fullname = trim($_POST["fullname"]);
if(empty($input_fullname)){
$fullname_err = "Please enter your fullname.";
} else{
$fullname = $input_fullname;
}
$phonenumber = trim($_POST["phonenumber"]);
if(empty($input_phonenumber)){
$phonenumber_err = "Please enter you mobile or landline number.";
} else{
$phonenumber = $input_phonenumber;
}
$age = trim($_POST["age"]);
if(empty($input_age)){
$age_err = "Please enter your age.";
} else{
$age = $input_age;
}
$city = trim($_POST["city"]);
if(empty($input_city)){
$city_err = "Please enter the name of emirate.";
} else{
$city = $input_city;
}
$area = trim($_POST["area"]);
if(empty($input_area)){
$area_err = "Please enter the area where your live.";
} else{
$area = $input_area;
}
$dateofbirth = trim($_POST["dateofbirth"]);
if(empty($input_dateofbirth)){
$dateof_err = "Please enter your dateof birth.";
} else{
$dateofbirth = $input_dateofbirth;
}
$nationality = trim($_POST["nationality"]);
if(empty($input_nationality)){
$nationality_err = "Please enter your nationality.";
} else{
$nationality = $input_nationality;
}
$about = trim($_POST["about"]);
if(empty($input_about)){
$about_err = "Please write a little about yourself.";
} else{
$about = $input_about;
}
$qualifications = trim($_POST["qualifications"]);
if(empty($input_qualifications)){
$qualifications_err = "Please enter your qualifications.";
} else{
$qualifications = $input_qualifications;
}
$interests = trim($_POST["interests"]);
if(empty($input_interests)){
$interests_err = "Please some of your interests.";
} else{
$interests = $input_interests;
}
if(empty($username_err) && empty($email_err) && empty($fullname_err) &&
empty($phonenumber_err) && empty($age_err) && empty($city_err) &&
empty($area_err) && empty($dateofbirth_err) && empty($nationality_err)
&& empty($about_err) && empty($qualifications_err) &&
empty($interests_err) ){
}
$dateofbirth = trim($_POST["dateofbirth"]);
$nationality = trim($_POST["nationality"]);
$about = trim($_POST["about"]);
$qualifications = trim($_POST["qualifications"]);
$interests = trim($_POST["interests"]);
$sql = "UPDATE accounts SET username = :username, email = :email ,
fullname = :fullname , phonenumber = :phonenumber , age = :age , city =
:city , area = :area, dateofbirth = :dateofbirth , nationality =
:nationality , about = :about , qualifications = :qualifications ,
interests = :interests WHERE id = :id ";
// In this case we can use the account ID to get the account info.
if($stmt = $con->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
$stmt->bindParam(":email", $param_email, PDO::PARAM_STR);
$stmt->bindParam(":fullname", $param_fullname, PDO::PARAM_INT);
$stmt->bindParam(":phonenumber", $param_phonenumber,
PDO::PARAM_INT);
$stmt->bindParam(":age", $param_age, PDO::PARAM_STR);
$stmt->bindParam(":city", $param_city, PDO::PARAM_STR);
$stmt->bindParam(":area", $param_area, PDO::PARAM_STR);
$stmt->bindParam(":dateofbirth", $param_dateofbirth,
PDO::PARAM_STR);
$stmt->bindParam(":nationality", $param_nationality,
PDO::PARAM_STR);
$stmt->bindParam(":about", $param_about, PDO::PARAM_STR);
$stmt->bindParam(":qualifications", $param_qualifications,
PDO::PARAM_STR);
$stmt->bindParam(":interests", $param_interests, PDO::PARAM_STR);
$stmt->bindParam(":id", $param_id, PDO::PARAM_INT);
// Set parameters
$param_username = $username;
$param_fullname = $fullname;
$param_phonenumber = $phonenumber;
$param_age = $age;
$param_city = $city;
$param_area = $area;
$param_dateofbirth = $dateofbirth;
$param_nationality = $nationality;
$param_about = $about;
$param_qualifications = $qualifications;
$param_interests = $interests;
$param_id = $_SESSION["id"];
// Attempt to execute the prepared statement
if($stmt->execute()){
// Password updated successfully. Destroy the session, and
redirect to login page
session_destroy();
header("location: profilepage.php");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
}
// Close connection
unset($con);
}else{
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Get URL parameter
$id = trim($_GET["id"]);
// Prepare a select statement
$sql = "SELECT * FROM accounts WHERE id = :id";
if($stmt = $con->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":id", $param_id);
// Set parameters
$param_id = $id;
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() == 1){
/* Fetch result row as an associative array. Since the
result set contains only one row, we don't need to use while loop
*/
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Retrieve individual field value
$username = $row["username"];
$fullname = $row["fullname"];
$phonenumber = $row["phonenumber"];
$age = $row["age"];
$city = $row["city"];
$area = $row["area"];
$dateofbirth = $row["dateofbirth"];
$nationality = $row["nationality"];
$about = $row["about"];
$qualifications = $row["qualifications"];
$interests = $row["interests"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
// Close connection
unset($con);
} else {
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>USER DETAILS</title>
<link href="https://fonts.googleapis.com/css?family=Skranji" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Rubik" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Hanalei+Fill" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Indie+Flower" rel="stylesheet">
<link rel="stylesheet" href="Non-profit opportunities2.css?v=<?php
echo time(); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="grid-container">
<!--Contains the Main heading-->
<div class = "heading">
<h1>BeHelp</h1>
</div>
<div class="grid-1">
<!--Sub-Heading-->
<div class="head">
<h3>''''</h3>
</div>
<H5>Update Details</H5>
<br><br>
<form action="<?php echo
htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group">Username: <input type="text" name="username" value="<?php echo $username ; ?>" ></div><br><br>
<div class="form-group">Email:<input type="email" name="email" value="<?php echo $email ; ?>" ></div><br><br>
<div class="form-group">Fullname:<input type="text" name="fullname" value="<?php echo $fullname ; ?>" ></div><br><br>
<div class="form-group">Age:<input type="number" name="age" value="<?php echo $age ;?>" ></div><br><br>
<div class="form-group">Phone Number:<input type="number" name="phonenumber" value="<?php echo $phonenumber ; ?>" ></div><br><br>
<div class="form-group">City:<input type="text" name="city" value="<?php echo $city ; ?>" ></div><br><br>
<div class="form-group">Area:<input type="text" name="area" value="<?php echo $area ; ?>" ></div><br><br>
<div class="form-group">Date of birth:<input type="date" name="dateofbirth" value="<?php echo $dateofbirth ; ?>" required></div><br><br>
<div class="form-group">Nationality:<input type="text" name="nationality" value="<?php echo $nationality ;?>" required></div><br><br>
<div class="form-group">About:<input type="text" name="about" value="<?php echo $about ; ?>" required></div><br><br>
<div class="form-group">Qualifications:<input type="text" name="qualifications" value="<?php echo $qualifications ;?>" required></div><br><br>
<div class="form-group">Interests:<input type="text" name="interests" value=" <?php echo $interests ;?>" required></div><br><br>
<div class="form-group"><input type="submit" name="update" value="Update Data"></div><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>
</form>
</div>
</div>
</body>
</html>
Однако детали так и не были обновлены после стольких попыток. После нажатия кнопки обновления страница загружается, но никакие детали не обновляются при проверке страницы профиля.
Я редактировал приведенный выше код, когда пытался выяснить ответ, но база данных по-прежнему не обновляется. .
Код не читает параметр ID в URL, поэтому он не может вносить какие-либо изменения в базу данных.
Я добавил исключение, и я до сих пор не получил никаких синтаксических ошибок. Я пытался понять это целый день. Я добавил HTML-код в случае необходимости ссылки. Но главная ошибка лежит в PHP-коде. Я хочу, чтобы кто-нибудь сказал мне, как код должен успешно прочитать идентификатор каждого пользователя, чтобы база данных могла быть обновлена.