简体   繁体   中英

Incorrect SQL Syntax Error Message

How do I fix the following error? I think the issue is with the INSERT INTO part of my SQL query.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

Code:

<?php

if  (isset($_POST['submitted'])){
    include('../mysqli_connect.php');


if (isset($_POST['$pn, $pc,$ps,$pd'])); 

        if(!empty($_POST['product_name'])){
        echo'<p> Successful entry.</p>';
        $pn = mysqli_real_escape_string ($dbc, trim($_POST['product_name']));
        $pn=$_POST['product_name'];
        echo "You chose:".$pn."<br>";}
        else{
            echo '<p>You forgot to enter the product name.</p>';}


        if(!empty($_POST['product_category'])){
        echo'<p> Successful entry.</p>';
        $pc = mysqli_real_escape_string ($dbc, trim($_POST['product_category']));
        $pc=$_POST['product_category'];
        echo "You chose:".$pc."<br>";}
        else{
            echo '<p>You forgot to enter the product category.</p>';}


        if(!empty($_POST['product_supplier'])){
        echo'<p> Successful entry.</p>';
        $ps = mysqli_real_escape_string ($dbc, trim($_POST['product_supplier']));
        $ps=$_POST['product_supplier'];
        echo "You chose:".$ps."<br>";}
        else{
            echo '<p>You forgot to enter the product category.</p>';}   

    if(!empty($_POST['product_description'])){
        echo'<p>Successful entry.</p>';
        $pd=mysqli_real_escape_string($dbc, trim($_POST['product_description']));
        $pd=$_POST['product_description'];
        echo"You chose:".$pd."<br>";}
        else{
            echo'<p>You forgot to enter the product_description.</p>';}

//query: Insert into database

$query = "INSERT INTO `products_catalog` (product_name, product_category, product_supplier, product_description) VALUES ('$pn', '$pc', '$ps','$pd')";

$r=@mysqli_query ($dbc,$query);
if($r){
    echo"<h2> You entered the product name:".$pn."</h2>";   
    echo"<h2>You entered the product category:".$pc."</h2>";
    echo"<h2>You entered the product supplier:".$ps."</h2>";
    echo"<h2>You entered the product description:".$pd."</h2>";}
    else{
        echo'<h1>System Error</h1>
        <p>The product name, category, supplier and description could not be entered. We apologize for any inconvenience. Please try again. If error continues, please contact tech support.</p>';

//Debugging message:
echo '<p>'.mysqli_error($dbc).'<br/><br/> Query:'.$query.'</p>';}

//End of if($r) IF.

mysqli_close($dbc); }

?>      
$query = "INSERT INTO products_catalog (product_name, product_category, product_supplier, product_description) VALUES ('".$pn."', '".$pc."', '".$ps."','".$pd."')";

It'll probably fix the error.

:)

Like this

<?php

if  (isset($_POST['submitted'])){
    include('../mysqli_connect.php');


if (isset($_POST['$pn, $pc,$ps,$pd'])); 

        if(!empty($_POST['product_name'])){
        echo'<p> Successful entry.</p>';
        $pn = mysqli_real_escape_string ($dbc, trim($_POST['product_name']));
        //$pn=$_POST['product_name'];  // THIS LINE NOT REQUIRED
        echo "You chose:".$pn."<br>";}
        else{
            echo '<p>You forgot to enter the product name.</p>';}


        if(!empty($_POST['product_category'])){
        echo'<p> Successful entry.</p>';
        $pc = mysqli_real_escape_string ($dbc, trim($_POST['product_category']));
        //$pc=$_POST['product_category'];  // THIS LINE NOT REQUIRED
        echo "You chose:".$pc."<br>";}
        else{
            echo '<p>You forgot to enter the product category.</p>';}


        if(!empty($_POST['product_supplier'])){
        echo'<p> Successful entry.</p>';
        $ps = mysqli_real_escape_string ($dbc, trim($_POST['product_supplier']));
        //$ps=$_POST['product_supplier'];  // THIS LINE NOT REQUIRED
        echo "You chose:".$ps."<br>";}
        else{
            echo '<p>You forgot to enter the product category.</p>';}   

    if(!empty($_POST['product_description'])){
        echo'<p>Successful entry.</p>';
        $pd=mysqli_real_escape_string($dbc, trim($_POST['product_description']));
        //$pd=$_POST['product_description'];  // THIS LINE NOT REQUIRED
        echo"You chose:".$pd."<br>";}
        else{
            echo'<p>You forgot to enter the product_description.</p>';}

//query: Insert into database

$query = "INSERT INTO `products_catalog` (product_name, product_category, product_supplier, product_description) VALUES ('$pn', '$pc', '$ps','$pd')";

$r=@mysqli_query ($dbc,$query);
if($r){
    echo"<h2> You entered the product name:".$pn."</h2>";   
    echo"<h2>You entered the product category:".$pc."</h2>";
    echo"<h2>You entered the product supplier:".$ps."</h2>";
    echo"<h2>You entered the product description:".$pd."</h2>";}
    else{
        echo'<h1>System Error</h1>
        <p>The product name, category, supplier and description could not be entered. We apologize for any inconvenience. Please try again. If error continues, please contact tech support.</p>';

//Debugging message:
echo '<p>'.mysqli_error($dbc).'<br/><br/> Query:'.$query.'</p>';}

//End of if($r) IF.

mysqli_close($dbc); }

?>      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM