简体   繁体   English

PHP的更新不起作用

[英]php UPDATE doesn't work

My php code UPDATE code doesn't work. 我的PHP代码更新代码无法正常工作。 There are no error messages it doesn't update. 没有错误消息,它不会更新。 Here is the code: 这是代码:

 <?php require 'connections/connections.php'; ?>
 <?php
 session_start();
 if(isset($_SESSION["UserIdent"])){
}else{
header('Location: login.php');  
}
?>
<?php
$User = $_SESSION["UserIdent"];
$result = $con->query("select * from user where UserID='$User'");
$row = $result->fetch_array(MYSQLI_BOTH);

$_SESSION["FirstName"] = $row['Fname'];
$_SESSION["LastName"] = $row['Lname'];
$_SESSION["Username"] = $row['Username'];
$_SESSION["Email"] = $row['Email'];
$_SESSION["Password"] = $row['Password'];
?>
<?php
if(isset($_POST['UpdateUser'])){
    $UpdateFname = $_POST['FirstName'];
    $UpdateLname = $_POST['LastName'];
    $UpdateUsername = $_POST['Username'];
    $UpdateEmail = $_POST['Email'];
    $UpdatePassword = $_POST['Password'];
    $sql = $con->query("UPDATE user SET Fname = '{$UpdateFname}',
    Lname = '{$UpdateLname}',
    Email = '{$UpdateEmail}',
    Username = '{$UpdateUsername}',
    Password = '{$UpdatePassword}',
    where UserID= $User");


    header('Location: UpdateAccount.php');
}

?>

The HTML code is here: HTML代码在这里:

<!doctype html>
 <html>
 <head>
 <link href="css/Master.css" rel="stylesheet" type="text/css">
 <link href="css/Menu.css" rel="stylesheet" type="text/css">
 <meta charset="utf-8">
 <title>UpdateAccount</title>
 </head>
 <body>

 <div id="holder">
  <div id="header"></div>
   <div id="NavBar">
    <nav>
    <ul>
        <li><a href="account.php">Account</a></li>
        <li><a href="Logout.php">Logout</a></li>
    </ul>
  </nav>
   </div>
  <div id="Content"></div>
 <div id="ContentLeft"></div>

 <div id="ContentRight"><form> <h6>
<label for="FirstName">First Name:<br>
</label>
<input name="FirstName" type="text" required="required" 
class="StyleTxtField" id="FirstName" value="<?php echo 
 $_SESSION["FirstName"]; ?>">
</h6>
<h6>
<label for="LastName">Last Name:<br>
</label>
<input name="LastName" type="text" required="required" class="StyleTxtField"
id="LastName" value="<?php echo $_SESSION["LastName"]; ?>">
</h6>
<h6>
<label for="Username">Username:<br>
</label>
<input name="Username" type="text" class="StyleTxtField" id="Username" 
value="<?php echo $_SESSION["Username"]; ?>">
</h6>
<h6>
<label for="Email">Email:<br>
</label>
<input name="Email" type="text" required="required" class="StyleTxtField" 
 id="Email" value="<?php echo $_SESSION["Email"]; ?>"> 
 </h6>
<h6>
<label for="Password">Password:<br>
</label>
<input name="Password" type="password" class="StyleTxtField" id="Password"
  value="<?php echo $_SESSION["Password"]; ?>">
</h6>

<p>
<input name="UpdateUser" type="submit" class="StyleTxtField" id="UpdateUser"
  value="UpdateUser">
 </p>
 </form></div>
 <div id="Footer"></div>
 </body>
 </html>

The Problem is that I can't figure out why it isn't updating...Any Help I would be grateful. 问题是我不知道为什么它没有更新...任何帮助,我将不胜感激。 There are no errors so it should work fine but it doesn't.I just learning Mysqli so any security issues I am not worried about yet, all I want is help on how to get this code to work. 没有错误,所以它应该可以正常运行,但是不能。我只是学习Mysqli,所以我不担心的任何安全问题,我想要的只是如何使此代码起作用的帮助。 Thanks! 谢谢!

In your code there is no method defined in <form> 您的代码中没有在<form>定义的方法

You are using $_POST super global for fetching values. 您正在使用$_POST super global来获取值。 So your form method must be post as like: 因此,您的form方法必须像这样发布:

<form method="post">

UPDATE 1 更新1

As my other friend Fred-ii suggest please make sure you are using session_start() in other files aswell where you need the information from $_SESSION specially in this file where you redirect the user 正如我的另一个朋友Fred-ii建议的那样,请确保您在其他文件中也使用了session_start()在其中您需要$_SESSION的信息,特别是在该文件中,您将用户重定向

UpdateAccount.php

Most important issues: 最重要的问题:

  • why are you saving password in session? 为什么在会话中保存密码? And if its simple text no any encrypted function than its another issue. 而且,如果其简单的文本没有其他功能,则没有任何加密功能。 You need to save password in encrypted format. 您需要以加密格式保存密码。

  • you need to prevent your code from SQL Injection. 您需要防止代码被SQL注入。

  • one more issue identified by @fred-ii remove ending comma from this line @ fred-ii标识的另一个问题从此行中删除结尾的逗号

    Password = '{$UpdatePassword}', 密码='{$ UpdatePassword}',

Side note: 边注:

Always active your error_reporting on development side not on production. 始终在开发方面而不是生产方面激活您的error_reporting You will get all the errors and save your time. 您将得到所有错误并节省您的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM