我知道有很多关于这个话题的问题。但遗憾的是,当我输入不同的输入并按下更新按钮时,我的userprofile只是不想更新。我需要在今天完成这个学校的练习,我真的没有其他人然后堆叠问。希望你们能帮助我。
我的问题:
我总是得到输出update not successful
,它永远不会更新我的数据库中的任何记录。而且我不确定该做些什么,因为我真的尝试了几乎所有的东西。
的welcome.php:
<?php
include("../php/session.php");
?>
<div>
<div class="col-md-9">
<div class="imagepageback">
<div class="card">
<div class="profilimage"></div>
<hr class="verticalline">
<form class="form" action="" method="post">
<input id="username" class="username" type="text" name="username" value="<?php echo $username; ?>" readonly>
<input id="email" class="email" type="text" name="email" value="<?php echo $email; ?>" readonly>
<input id="firstname" class="firstname" type="text" name="firstname" value="<?php echo $firstname; ?>" readonly>
<input id="lastname" class="lastname" type="text" name="lastname" value="<?php echo $lastname; ?>" readonly>
<input id="birthdate" class="birthdate" type="text" name="birthdate" value="<?php echo $birthdate; ?>" readonly>
<div>
<input id="street" class="street" type="text" name="street" value="<?php echo $street; ?>" readonly>
<input id="nr" class="nr" type="text" name="streetnr" value="<?php echo $streetnr; ?>" readonly>
</div>
<div>
<input id="city" class="city" type="text" name="city" value="<?php echo $city; ?>" readonly>
<input id="plzz" class="plzz" type="text" name="plzz" value="<?php echo $plzz; ?>" readonly>
</div>
<a href="#" class="editprofilelink" id="editprofilelink" onclick="editable(); showbt()"; >Edit Profile</a>
<a href="changepd.html" class="editpasswordlink">Change Password</a>
<!----------PHP Skript----------->
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$email = mysqli_real_escape_string($db,$_POST['email']);
$username = mysqli_real_escape_string($db,$_POST['username']);
$firstname = mysqli_real_escape_string($db,$_POST['firstname']);
$lastname = mysqli_real_escape_string($db,$_POST['lastname']);
$birthdate = mysqli_real_escape_string($db,$_POST['birthdate']);
$street = mysqli_real_escape_string($db,$_POST['street']);
$streetnr = mysqli_real_escape_string($db,$_POST['streetnr']);
$city = mysqli_real_escape_string($db,$_POST['city']);
$plzz = mysqli_real_escape_string($db,$_POST['plzz']);
$sql = "UPDATE clients SET " .
"email = '$email', " .
"username = '$username', " .
"firstname = '$firstname', " .
"lastname = '$lastname', " .
"birthdate = '$birthdate', " .
"street = '$street', " .
"streetnr = '$streetnr', " .
"city = '$city', " .
"plzz = '$plzz' " .
"WHERE username = '$username'";
$result = mysqli_query($db,$sql);
if($result) {
$_POST["email"] = $email;
$_POST["username"] = $username;
$_POST["firstname"] = $firstname;
$_POST["lastname"] = $lastname;
$_POST["birthdate"] = $birthdate;
$_POST["street"] = $street;
$_POST["streetnr"] = $streetnr;
$_POST["city"] = $city;
$_POST["plzz"] = $plzz;
header("location:http://localhost:81/Left_over_youth_website/php/logout.php");
}else {
echo '<p id="error">Update was not sucessful</p>';
}
}
?>
<input hidden id="btupdate" type="submit" name="btupdate" value="Update">
</form>
</div>
</div>
</div>
</div>
session.php文件:
<?php
include('connection.php');
session_start();
$user_check = $_SESSION['login_user'];
if(!isset($_SESSION['login_user'])){
header("location:http://localhost:81/Left_over_youth_website/pages/login.php");
}
?>
connection.php:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'leftoveryouth');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
数据库:
CREATE TABLE `clients` (
`id` int(11) NOT NULL,
`firstname` varchar(50) DEFAULT NULL,
`lastname` varchar(50) DEFAULT NULL,
`birthdate` date DEFAULT NULL,
`street` varchar(50) DEFAULT NULL,
`streetnr` varchar(50) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`plzz` varchar(50) DEFAULT NULL,
`username` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
答案 0 :(得分:0)
在服务器端,使用像
这样的后全局数组来表示您的表单数据$name = $_POST['name'];
然后是sql
$sql = "UPDATE profile SET name = '".$name.'" WHERE userid = '".$_SESSION['userid].'";
$update = $connection->query($sql);
if($update){
$_SESSION['name']=$name;
}
如果您更新了登录人员的当前信息,假设您保留他们的ID或其他内容来唯一标识他们
答案 1 :(得分:0)
代码中有几个变量名称错误。例如,一个input
被命名为streett
,但在从$_SESSION[]
阅读时,它会被视为street
。
$_SESSION[]
无法读取POST数据。您需要改为使用$_POST[]
。
根据您提供的CREATE语句,SQL查询中的列名称是错误的。此外,在查询的最后部分,您没有为变量$
添加$plzz
,根据您的代码,该变量也拼写错误。
当事情无效时,请务必检查变量是否存在拼写和案例错误。
此外,在SQL中,执行更新的正确格式是
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
正如@Karlo在评论中所提到的,删除and
关键字和逗号分隔您正在进行的更新。
最后,您必须使用WHERE
语句来限制更新哪些行。否则,您将更新数据库中的所有行。
旧查询:(我创建了多行,因此可以更容易地看到更改)
$sql = "UPDATE clients SET " .
"emaill = '$email' and " .
"usernamee = '$username' and " .
"firstnamee = '$firstname' and " .
"lastnamee = '$lastname' and " .
"birthdatee = '$birthdate' and " .
"street = '$street' and " .
"nrr = '$streetnr' and " .
"city = '$city' and " .
"plzz = 'plz'";
基于提供的列的新查询:
$sql = "UPDATE clients SET " .
"email = '$email', " .
"username = '$username', " .
"firstname = '$firstname', " .
"lastname = '$lastname', " .
"birthdate = '$birthdate', " .
"street = '$street', " .
"streetnr = '$streetnr', " .
"city = '$city', " .
"plzz = '$plzz' " .
"WHERE username = '$username'";