skip to Main Content

please help me to solve this error.i tired from searching solution…
error: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ‘WHERE name=NULL’ at line 1

my database have 3 column=id(int),name(varchar),comment(varchar) and i want insert comment to it.

my php code :

<?php
include "./Config.php";
include './MyPDO.php';

  $response = array() ;
  $connect = MyPDO::getInstance();

  $name = $_REQUEST['name'];
  $comment=$_REQUEST['comment'];

        $query =  " INSERT INTO user "
                . " (comment) "
                . " VALUES "
                . " (:comment) "
                . " WHERE name=:name ";

        $stmt = $connect->prepare($query);
        $stmt->bindParam(":name",$name);
        $stmt->bindParam(":comment",$comment);
        try {
                $stmt->execute();
                $response['massage'] = "sucess";
                echo json_encode($response);
                exit;
        } catch (PDOException $ex) { 
                $response['massage'] = "error";
                $response['error']=$ex->getMessage();
                echo json_encode($response);
        }







2

Answers


  1. Looks like you mixed the syntax here. You seem to want to update an existing record. Use

    update user
    set comment = :comment
    where name = :name
    

    insert if for creating a new record.

    Login or Signup to reply.
  2. The insert into ... values() syntax does not take a where clause.

    If you want to insert, then:

    insert into user(name, comment) values(:name, :comment)
    

    But actually it looks like you might want an update:

    update users set comment = :comment where name = :name;
    

    The former creates a new record in the table, with the given name and comment.

    The latter modifies the already-existing record that has the same name and sets its comment value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search