Reputation: 1
I need to update my data in database. I am successfully created the user registration and login form in php, so i need to update and delete data in database. And I am getting error message update function.
There is condition: id(primary) and email(unique) & username(unique).
Error message in this program:
"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>
Upvotes: 0
Views: 2073
Reputation: 23958
You have not added value to id
.
It is a hidden variable, so, it can only be set through PHP or JS not with user input.
Therefore, your query after where clause is blank and causing error.
Solution:
Assign id
a value.
<input type="hidden" name="id" placeholder="ID" value="<?php echo $user_id;?>">
And to avoid this error, check if id
is posted.
Modify:
if(isset($_POST['submit'])) {
To:
if(isset($_POST['submit']) && ! empty($_POST['id'])){
Upvotes: 1