Trying to understand PHP and SQL right now and I’m getting this error here, being honest I don’t really get what is trying to tell me or what I should do, have been looking for a good while trying to solve the issue but I don’t really know what to do, would appreciate a hand from somebody with a keen eye for this
(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 '[id]' at line 1 in C:xampphtdocsTEGeditcliente.php:30 Stack trace: #0 C:xampphtdocsTEGeditcliente.php(30): mysqli->query('SELECT * FROM c...') #1 {main} thrown in C:xampphtdocsTEGeditcliente.php on line 30)
all with this code of right here
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "totalmk_db";
$connection = new mysqli($servername, $username, $password, $database);
$id = "";
$name = "";
$lastname = "";
$idnumber = "";
$email = "";
$phone = "";
$errorMessage = "";
$successMessage = "";
if ( $_SERVER['REQUEST_METHOD'] == 'GET') {
if ( !isset($_GET["id"]) ) {
header("location: clientes.php");
exit;
}
$id = $_GET["id"];
$sql = "SELECT * FROM clients WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
header("location: clientes.php");
exit;
}
$name = $row["name"];
$lastname = $row["lastname"];
$idnumber = $row["idnumber"];
$email = $row["email"];
$phone = $row["phone"];
} else {
$name = $_POST["name"];
$lastname = $_POST["lastname"];
$idnumber = $_POST["idnumber"];
$email = $_POST["email"];
$phone = $_POST["phone"];
do {
if ( empty($name) || empty($lastname) || empty($idnumber) || empty($email) || empty($phone) ) {
$errorMessage = "Se requiere que llene todos los campos";
break;
}
$sql = "UPDATE clients" .
"SET name = '$name', lastname = '$lastname', idnumber = '$idnumber', email = '$email', phone = '$phone' " .
"WHERE id = $id";
$result = $connection->query($sql);
if (!$result) {
$errorMessage = "Invalid query: " . $connection->error;
break;
}
$successMessage = "Cliente editado correctamente!";
} while (false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Añadir cliente</title>
<link rel="stylesheet" href="css/clientes.css">
</head>
<body>
<div class="container">
<div class="lista-clientes">
<h2>Añadir Cliente</h2>
<?php
if ( !empty($errorMessage) ) {
echo "
<div>
<p class='error'>$errorMessage</p>
</div>
";
}
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<label>Nombre</label>
<input type="text" class="form-control" name="name" value="<?php echo $name; ?>">
<label>Apellido</label>
<input type="text" class="form-control" name="lastname" value="<?php echo $lastname; ?>">
<label>Cédula</label>
<input type="text" class="form-control" name="idnumber" value="<?php echo $idnumber; ?>">
<label>Email</label>
<input type="text" class="form-control" name="email" value="<?php echo $email; ?>">
<label>Número de Teléfono</label>
<input type="text" class="form-control" name="phone" value="<?php echo $phone; ?>">
<?php
if ( !empty($successMessage) ) {
echo "
<div class='alert' role='alert'>
<p class='success'>$successMessage</p>
</div>
";
}
?>
<div class="">
<button type="submit" class="submit">Aceptar</button>
<a class="cancel" href="clientes.php">Volver</a>
</div>
</form>
</div>
</div>
</body>
</html>