I am getting the following error when I try to update tables in a db using PHP.
QUERY FAILED 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
(line one is where my query begins)
I've concluded that it has something to do with the "id"
but I have no idea what. If I hard code it as "WHERE id = 1"
it works, but it really doesn't seem to read the id.
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$id = $_POST['id'];
$query = "UPDATE users SET ";
$query .= "username = '$username', ";
$query .= "password = '$password' ";
$query .= "WHERE id = $id ";
$result = mysqli_query($connection, $query);
if(!$result) {
die("QUERY FAILED" . mysqli_error($connection));
}
}
Here is the HTHML I'm using for the ID:
<form action="login_update.php" method="post">
<div class="form-group">
<select name="id" id="">
<?php
global $connection;
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
if(!$result){ //om result inte är TRUE then die()
die('Query failed ' .mysqli_error());
}
while($row = mysqli_fetch_assoc($result)){
$id = $row['id'];
echo "<option value=''>$id</option>";
?>
</select>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="Update">
</form>
I'm running this on localhost so no need for any hashing and the connection to the db works fine.