Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 ''categories' ('category_title') values ('Beauty Sleep')' at line 1 in C:\xampp\htdocs\Ecommerce Website\admin_area\insert_categories.php:7 Stack trace: #0 C:\xampp\htdocs\Ecommerce Website\admin_area\insert_categories.php(7): mysqli_query(Object(mysqli), 'insert into 'ca...') #1 C:\xampp\htdocs\Ecommerce Website\admin_area\index.php(88): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\Ecommerce Website\admin_area\insert_categories.php on line 7


What I have tried:

if(isset($_POST['insert_cat']))
     {
       $category_title=$_POST['cat_title'];
       $insert_query="insert into 'categories' ('category_title') values ('$category_title')";
       $result=mysqli_query($con,$insert_query);
       if($result){
        echo "<script>alert('Category has been inserted successfully')</script>";
       }
     }
Posted
Updated 21-Jul-22 0:36am

Try removing the quotes from the table and column names:
PHP
$insert_query="insert into categories (category_title) values ('$category_title')";

See A MariaDB Primer - MariaDB Knowledge Base[^]
 
Share this answer
 
Before someone posts the obligatory comment about not using string interpolation for running SQL queries, the issue here is simply that table and column names are not meant to be surrounded by single quotes ('). You can either omit the quotations around the table/column names, or use the backtick symbol (`) instead:
insert into `categories` (`category_title`) values ('$category_title')

Single quotes are only meant to be used for string values, so you can keep it for the values () part. But yes, don't insert the variables directly into the query, use something like parameter binding: PHP: mysqli_stmt::bind_param - Manual[^] This will prevent someone from providing you with dodgy data which could seriously damage your database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900