phphtmlmysqlimport

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 
include('config.php');
if (isset($_POST['btn'])) {
if (isset($_POST['books'])) {
$b1 = implode(',' , $_POST['books']);



$sql = "INSERT INTO books(book_name) VALUES ('$b1)";
 if($conn->query($sql)=== TRUE){

   echo "your data is saved";
}else{
    echo"try again".$conn->error;
}



$conn->close();?>


<form action="index.php" method="post">
    <h3>Select your Books</h3>
    <input type="checkbox" name="books[]" value="book1">Book 1

    <input type="checkbox" name="books[]" value="book2">Book 2

    <input type="checkbox" name="books[]" value="book3">Book 3
    <input type="submit" name="btn">

</form>

i'm beginner in PHP i'm trying to insert data in db using check boxes but an error appear every time " have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''book1,book2,book3)' at line 1"


Solution

  • Change the line:-

    $sql = "INSERT INTO books(book_name) VALUES ('$b1)";
    -------------------------------------------------^
                 missing single quote around the value
    

    to:-

    $sql = "INSERT INTO books(book_name) VALUES ('$b1')";