找不到PHP用户配置文件更新数据

时间:2016-09-04 12:17:49

标签: php mysqli user-profile

你好我正在制作一个带有个人资料更新页面的用户注册系统。我有一个echo所有数据字段的配置文件。现在已经创建了 edit-profile.php 页面来编辑和更新个人资料。

但是我一直收到错误找不到此用户的数据那是因为抛出异常。有些时候没有发现错误表执行但没有更新。

这是我的代码

    ///// start session
    session_start();

    if(!isset($_SESSION['user_type'])){
      header('Location: index.php');
    }

    // include file
    require_once('include/connection.php');

    //////////////// page title
    $title = "Edit your Profile"; 

    /////////////// define user session variable
    $userId = $_SESSION['user_id'];
    $userName = $_SESSION['user_name'];

    ///////////////// If user id isset, GET id and execute query
    if(isset($_GET['id']))
    {
      $userId = $_GET['id'];

      if(isset($_POST['update'])){      

            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $email = $_POST['email'];
            $user_name = $_POST['user_name'];
            $profession = $_POST['profession'];
            $address = $_POST['address'];
            $bio = $_POST['bio'];  
            $dob = $_POST['dob'];
            $gender = $_POST['gender'];
            $country = $_POST['country'];


              $updated = "UPDATE user SET firstname='$firstname',lastname='$lastname',profession='$profession',address='$address',email='$email',user_name='$user_name',bio='$bio',dob='$dob',gender='$gender',country='$country' WHERE id = '$userId'";
              $result =  mysqli_query($con, $updated)or die(mysqli_error($con));
               if($result){

                      $_SESSION['main_notice'] = "Successfully Updated!";
                      header('Location: profile.php');
                      exit;

                  }else{
                      $_SESSION['main_notice'] = "Some error, try again";
                      header('Location: '.$_SERVER['PHP_SELF']);
                  }

       }
     }

    mysqli_free_result($result);
    mysqli_close($con);

    ?>
    <!---------------------include header layout------------------------------>

    <?php>
      //include header layout
        require_once('include/header.php');
      ?>

    <!--------------------------- Redirect based on user type ----------------------->
      <?php
          if($_SESSION['user_type'] == 'admin' || $_SESSION['user_type'] == 'leader'){

                // include admin header layout
              require_once('include/admin-header.php');

              }elseif($_SESSION['user_type'] == 'member'){

               // include  member header layout
              include_once('include/member-header.php');

              }else{    

                session_destroy();
                header('Location: index.php');
              }
    ?>
    <!--  check if userId is found, if not throw error ---->
      <?php 

          $getUserQuery = "select * from user where id = $userId";
          $resultData =     mysqli_query($con,$getUserQuery);
                if(!mysqli_num_rows($resultData)){
                  echo 'No Data Found for this user';
                }else{
                  $row = mysqli_fetch_array($resultData);
                }
      ?>

这里是我的DIV

<div class="user-details">

                <!----------------------- EDIT CODE ------------------------------>
            <?php 
              if(isset($_GET['id']))
              {
              $userId = $_GET['id'];

                $getselect = "SELECT * user WHERE id = $userId";
                $result = mysqli_query($con, $getselect)or die(mysqli_error($con));    
                while ($row = mysqli_fetch_array($result)) {

                    $firstname = $row['firstname'];
                    $lastname = $row['lastname'];
                    $email = $row['email'];
                    $user_name = $row['user_name'];
                    $profession = $row['profession'];
                    $address = $row['address'];
                    $bio = $row['bio'];  
                    $dob = $row['dob'];
                    $gender = $row['gender'];
                    $country = $row['country'];

                   }
              }  

            mysqli_free_result($result);
            mysqli_close($con);
                ?>          
                    <form name="update-user" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return formSubmit();">
                      <table>
                      <tr>
                        <td></td>
                        <td></td>
                        <td>Update</td>
                      </tr>
                      <tr>
                      <td><label for = "">First Name</label></td>
                      <td><input type="text" id="firstname" name="firstname" placeholder ="<?php echo $row['firstname']; ?>"></td>
                      </tr>

                      <tr>
                      <td><label for = "">Last Name</label></td>
                      <td><input type="text" id="lastname" name="lastname"  placeholder ="<?php echo $row['lastname']; ?>"></td>
                      </tr>

                      <tr>
                      <td><label for = "">User Name</label></td>
                      <td><input type="email" id="user_name" name="user_name" placeholder ="<?php echo $row['user_name']; ?>"></td>
                      </tr>

                      <tr>
                      <td><label for = "">Profession Name</label></td>
                      <td><input type="text" id="profession" name="profession" placeholder ="<?php echo $row['profession']; ?>"></td>
                      </tr>

                       <tr>
                      <td><label for = "">Email</label></td>
                      <td><input type="text" id="email" name="email" placeholder ="<?php echo $row['email']; ?>"></td>
                      </tr>

                      <tr>
                      <td><label for = "">Gender</label></td>
                      <td><input type="text" id="gender" name="gender" placeholder ="<?php echo $row['gender']; ?>"></td>
                      </tr>

                      <tr>
                      <td><label for = "">Date Of Birth</label></td>
                      <td><input type="text" id="dob" name="dob" placeholder ="<?php echo $row['dob']; ?>"></td>
                      </tr>


                      <tr>
                      <td><label for = "">Addres</label></td>
                      <td><input type="text" id="address" name="address" placeholder ="<?php echo $row['address']; ?>"></td>
                      </tr>


                      <tr>
                      <td><label for = "">Country</label></td>
                      <td><input type="text" id="country" name="country" placeholder ="<?php echo $row['country']; ?>"></td>
                      </tr>  


                      <tr>
                      <td><label for = "">Bio</label></td>
                      <td><input type="text" id="bio" name="bio" placeholder ="<?php echo $row['bio']; ?>"></td>
                      </tr>

                      <tr>
                      <td></td>             
                      <td><input type="submit" id="update" name="update" value="Update"></td>
                      </tr>
                      </table>
                    </form>     
                </div>



            <?php 
            require_once('include/footer.php');
            ?>

我不确定这里缺少什么。请任何人帮忙

0 个答案:

没有答案