编辑行数据

时间:2018-11-20 05:23:31

标签: php mysql

<?php
//$id = $_GET['id'];
$GLOBALS['a'] = isset($_GET['id']) ? $_GET['id'] : '';
echo "$a";

$db = mysqli_connect('localhost', 'root', '', 'ems');
if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}
$first_name = "";
$last_name = "";
//$mobile = "";
$email  = "";
$gender = "";
$designation_id = "";
$address = "";
$errors = array(); 

if (isset($_POST['reg_user'])) {
  $first_name = mysqli_real_escape_string($db, $_POST['first_name']);
  $last_name = mysqli_real_escape_string($db, $_POST['last_name']);
  //$mobile = mysqli_real_escape_string($db, $_POST['mobile']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $gender = mysqli_real_escape_string($db, $_POST['gender']);
  $designation_id = mysqli_real_escape_string($db, $_POST['designation_id']);
  $address = mysqli_real_escape_string($db, $_POST['address']);
  $user_check_query = "SELECT * FROM users WHERE email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists

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

     //  echo "$a";

          $sql = "UPDATE `users` SET `first_name` = '$first_name' ,`last_name`='$last_name', `password`='$password',
            `email`='$email', `gender`='$gender', `designation_id`='$designation_id',`address`='$address' WHERE `users`.`id` = '$a'"; 

        if (mysqli_query($db, $sql)) {
            mysqli_close($db);
            header('Location: index.php');
             echo "$a";
            exit;
        } else {
            echo "Error updating  record";
        }
}

?>

尝试通过表单更新该行时,它会重定向到索引页面而不更新该行。...没有错误。.pls帮助我。

尝试通过表单更新该行时,它会重定向到索引页面而不更新该行。...没有错误。.pls帮助我。

1 个答案:

答案 0 :(得分:0)

请尝试使用此更新的代码。可能是问题原因是值分配不正确。以便将其移至其他部分。现在,我已将更新查询移至POST Check值内。

<?php
//$id = $_GET['id'];
$id = isset($_GET['id']) ? $_GET['id'] : '';
$db = mysqli_connect('localhost', 'root', '', 'ems');
if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}
$first_name = "";
$last_name = "";
//$mobile = "";
$email  = "";
$gender = "";
$designation_id = "";
$address = "";
$errors = array(); 

if (isset($_POST['reg_user']) && $id!='') {
  $first_name = mysqli_real_escape_string($db, $_POST['first_name']);
  $last_name = mysqli_real_escape_string($db, $_POST['last_name']);
  //$mobile = mysqli_real_escape_string($db, $_POST['mobile']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $gender = mysqli_real_escape_string($db, $_POST['gender']);
  $designation_id = mysqli_real_escape_string($db, $_POST['designation_id']);
  $address = mysqli_real_escape_string($db, $_POST['address']);
  $user_check_query = "SELECT * FROM users WHERE email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists

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

  else
  {

          $sql = "UPDATE `users` SET `first_name` = '$first_name' ,'last_name'='$last_name', 'password'='$password',
            'email'='$email', 'gender'='$gender', 'designation_id'='$designation_id','address'='$address' WHERE `users`.`id` = '$id'"; 

        if (mysqli_query($db, $sql)) {
            mysqli_close($db);
            header('Location: index.php');
            exit;
        } else {
            echo "Error deleting record";
        }
  } 

}


?>