Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax;

  Kiến thức lập trình

This is the error i received when executing my update button

Fatal error: Uncaught mysqli_sql_exception: 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 in D:xampphtdocscrudfunctions.php:34 Stack trace: #0 D:xampphtdocscrudfunctions.php(34): mysqli_query(Object(mysqli), ‘UPDATE users SE…’) #1 D:xampphtdocscrudindex.php(5): editUser(Object(mysqli)) #2 {main} thrown in D:xampphtdocscrudfunctions.php on line 34

this is the code which contains the line 34

<?php 
function addUser($connection) {
    if(isset($_POST['add'])) {

        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $address = $_POST['address'];
        $password = $_POST['password'];
        $gender = $_POST['gender'];

        $sql = "INSERT INTO users(firstname,lastname,address,password,gender) VALUES ('$fname', '$lname', '$address','$password','$gender')";
        $result = mysqli_query($connection,$sql);
        if(!$result) {
            echo "ERROR";
        } else {
            echo "User added successfully";
        }

    }
}



function editUser($connection){
    if(isset($_POST['edit'])) {
        
        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $address = $_POST['address'];
        $gender = $_POST['gender'];
        $id = $_POST['id'];
        
        $sql = "UPDATE users SET firstname = '$fname', lastname = '$lname', address = '$address',       gender = '$gender' WHERE id = $id";
 LINE 34 -     $result = mysqli_query($connection,$sql);
        if(!$result) {
            echo "ERROR";
        } else {
            echo "Record updated successfully";
        }

        
    }

I tried changing this part of the code
$sql = “UPDATE users SET firstname = ‘$fname’, lastname = ‘$lname’, address = ‘$address’, gender = ‘$gender’ WHERE id = $id”;

I changed the WHERE id = 7″;
and it successfully edited my code but when i type it back to WHERE id = $id”;
it errors again

any suggestions please to fix this

New contributor

Jose Eduardo Jamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT