Не могу обновить мои базы данных php и mysql - PullRequest
0 голосов
/ 28 марта 2019

Мне нужно обновить мои данные в базе данных. Я успешно создал регистрационную форму пользователя и форму входа в php, поэтому мне нужно обновить и удалить данные в базе данных. И я получаю функцию обновления сообщения об ошибке.

Существует условие: идентификатор (основной) и адрес электронной почты (уникальный) и имя пользователя (уникальный).

Сообщение об ошибке в этой программе:

"Ошибка обновления записи: у вас есть ошибка в вашем синтаксисе SQL; проверьте руководство, соответствующее вашей версии сервера MariaDB, чтобы найти правильный синтаксис для использования рядом с '' в строке 1."

<?php
session_start();
require('connect.php');

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

$id = $_POST['id'];
$username = $_POST['username'];
$name = $_POST['name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$location = $_POST['location'];
$course = $_POST['course'];
$mobile = $_POST['mobile'];

 $sql = " UPDATE user SET username ='$username', name = '$name', email = '$email', password='$password', dob ='$dob', gender ='$gender',location ='$location', course = '$course', mobile = '$mobile' WHERE id= $id ";

    if (mysqli_query($mysqli, $sql)) {
      $smsg = "Record updated successfully";
   } else {
      $fmsg = "Error updating record: " . mysqli_error($mysqli);
   }
}

   ?>



<html>
<head>
<title>User Update PHP & MYSQL</title>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="main.css" >


<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="container">
    <a class= "float-right" href = "logout.php"> LOGOUT </a> 
      <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
      <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
        <form class="form-signin" method="POST">
        <h2 class="form-signin-heading">Edit Profile</h2>
        <div class="input-group">
        <input type="hidden" name="id" placeholder="ID">
        <input type="text" name="username" class="form-control" placeholder="username" required></div>

          <label for="inputname" class="sr-only">Full Name</label>
          <input type="text" name="name" id="inputname" class="form-control" placeholder="Full Name"required>

          <label for="inputEmail" class="sr-only">Email address</label>
          <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address"required>

          <label for="inputPassword" class="sr-only">Password</label>
          <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>

          <label for="inputDoB" class="sr-only">DOB</label>
          <input type="date" name="dob" id="inputDoB" class="form-control" placeholder="DOB"required>

          <td>Gender:</td>
          <input type="radio" name="gender" value="m">Male
          <input type="radio" name="gender" value="f">female
          </tr>
          <label for="inputlocation" class="sr-only">Location</label>
          <input type="text" name="location" id="inputlocation" class="form-control" placeholder="Location"required>

          <label for="inputcourse" class="sr-only">Course</label>
          <input type="text" name="course" id="inputcourse" class="form-control" placeholder="Course" required>

          <label for="inputmobile" class="sr-only">Mobile</label>
          <input type="text" name="mobile" id="inputmobile" class="form-control" placeholder="Mobile" required>

          <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Update</button>       
</div>
</body>
</head>
</html>

1 Ответ

1 голос
/ 28 марта 2019

Вы не добавили значение к id.

Это скрытая переменная, поэтому ее можно установить только через PHP или JS, а не с помощью пользовательского ввода.

Следовательно, вашзапрос после оператора where является пустым и вызывает ошибку.

Решение:

Назначьте id значение.

<input type="hidden" name="id" placeholder="ID" value="<?php echo $user_id;?>">

И, чтобы избежать этой ошибки, проверьте, если id опубликовано.

Изменить:

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

Кому:

if(isset($_POST['submit']) && ! empty($_POST['id'])){
...