php - 无法更新我的 php 和 mysql 数据库数据

标签 php mysql phpmyadmin insert-update

我需要更新数据库中的数据。我已在 php 中成功创建了用户注册和登录表单,因此我需要更新和删除数据库中的数据。我收到错误消息更新功能。

有条件:id(主要)和电子邮件(唯一)和用户名(唯一)。

该程序中的错误消息:

"Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 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>

最佳答案

您尚未为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'])){

关于php - 无法更新我的 php 和 mysql 数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55393985/

相关文章:

php - OFFSET OR LIMIT 在 Mysql 中高效

php - 如何使用 MySQL 获得最高教育?

python - 将数据发布到 Sentiment140 进行情绪分析

mysql - 文件编码(UTF-8 无法正常工作)

php - Laravel:加载存储在 'storage' 文件夹中的图像

php - 没有特殊软件,使用php代码可以实现这种效果吗

php - 从格式错误的数据库中检索数据

php - SQL语法错误,PHP MYSQL

mysql - 使用 phpMyAdmin 查找和替换

php - SQL 语句在 PHP 中不起作用,但在 MySQL 中起作用