doutang7383 2019-02-19 10:12
浏览 55

显示从1.php到另一个变量的变量的问题

So, I have a problem when trying to modify only 1 user columns data in the database. The code I`ve made will only modify the data of the last user in the database and not the current user.

For example: I am logged on Asd account with the columns:
email username nume prenume tara
if i edit it,it will show the values on this one in the database but it will not show them on screen
if i have more users like asd abc abcd
it will show the values of abcd

<?php include('server.php');
$result = mysqli_query($db,"SELECT * FROM users");
while($row = mysqli_fetch_array($result))
{
   if($username=$row['username'])
   {
      $email=$row['email'];
      $user=$row['username'];
      $nume=$row['nume'];
      $prenume=$row['prenume'];
      $tara=$row['tara'];
      $oras=$row['oras'];
      $adresa=$row['adresa'];
      $numar=$row['numar'];
   }
}
?>

<form method="post" action="editprofil.php">
   <table class="table-fill">
      <thead>
         <tr>
            <th class="text-left" style="font-size:32px;padding-bottom:1em;" >Profil</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td class="text-left">Username</td>
            <td class="text-left"><?php echo $user ?></td>
         </tr>
         <tr>
            <td class="text-left">Nume</td>
            <td class="text-left"><input type="text" name="nume"   /></td>
         </tr>
         <tr>
            <td class="text-left">Prenume</td>
            <td class="text-left"><input type="text" name="prenume"  /></td>
         </tr>
         <tr>
            <td class="text-left" >Email </td>
            <td class="text-left">  <?php echo $email; ?></td>
         </tr>
         <tr>
            <td class="text-left">Tara</td>
            <td class="text-left"><input type="text" name="tara"  /></td>
         </tr>
         <tr>
            <td class="text-left">Oras</td>
            <td class="text-left"><input type="text" name="oras"    /></td>
         </tr>
         <tr>
            <td class="text-left">Adresa</td>
            <td class="text-left"><input type="text" name="adresa"/></td>
         </tr>
         <tr>
            <td class="text-left">Telefon mobil</td>
            <td class="text-left"><input type="text" name="telefon"   /></td>
         </tr>
         <tr>
            <td class="text-left">Data nasterii</td>
            <td class="text-left"><input type="text" name="varsta"  /></td>
         </tr>
      </tbody>
   </table>
   <div class="input-container"style="padding-top:1em;">
      <input type="username" name="username" id="#{label}" />
      <label for="#{label}">Confirm username</label>
      <div class="bar"></div>
   </div>
   <div class="input-container"style="padding-top:1em;">
      <input type="password" name="password" id="#{label}" />
      <label for="#{label}">Confirm password</label>
      <div class="bar"></div>
   </div>
   <div class="button-container">
      <button type="submit" class="btn" name="edit_user">Register</button>
   </div>
</form>

server.php

<?php
session_start();

$username = "";
$oras ="";
$nume ="";
$prenume ="";
$tara ="";
$adresa ="";
$telefon ="";
$varsta ="";
$email="";


$errors = array();

$db = mysqli_connect('localhost', 'root', '12345678', 'registration');

if (isset($_POST['reg_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (username, email, password)
              VALUES('$username', '$email', '$password')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['email'] = $email;
    $_SESSION['success'] = "You are now logged in";
    header('location: primapagina.php');
  }
}

if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['email'] = $email;
      $_SESSION['success'] = "You are now logged in";
      header('location: primapagina.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
}
if (isset($_POST['edit_user']))
{
$oras = mysqli_real_escape_string($db, $_POST['oras']);
$tara = mysqli_real_escape_string($db, $_POST['tara']);
$adresa = mysqli_real_escape_string($db, $_POST['adresa']);
$nume = mysqli_real_escape_string($db, $_POST['nume']);
$prenume = mysqli_real_escape_string($db, $_POST['prenume']);
$telefon = mysqli_real_escape_string($db, $_POST['telefon']);
$varsta = mysqli_real_escape_string($db, $_POST['varsta']);
$password = md5(mysqli_real_escape_string($db, $_POST['password']));
$username = mysqli_real_escape_string($db, $_POST['username']);
$sql = "UPDATE users SET tara='$tara', oras='$oras', nume='$nume', prenume='$prenume', tara='$tara', adresa='$adresa' WHERE password = '$password' and username='$username' ";
mysqli_query($db, $sql);

}

?>
  • 写回答

2条回答 默认 最新

  • 啊啊啊小孔 2019-02-19 10:23
    关注

    As I can see in your code, You are fetching all the records from the users table, at the last iteration all the variable like $email, $user.... have the last row information that's why the code is updating the last user.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比