vincent I
vincent I

Reputation: 5

query failedSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

Query failed:

SQLSTATE[42000]: Syntax error or access violation: 1064 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 ':c2 , add=:c3 , mob=:c4 WHERE id=:c1' at line 1

I had a syntax error in this program,I can't update "cus" table in database.Please anyone can solve this bug.

Thankyou

My PHP codings are below:

if(isset($_POST['submit']))
{
    $c1=$_POST['cid1'];
    $c2=$_POST['cname1'];
    $c3=$_POST['cadd1'];
    $c4=$_POST['cmob1'];

    $sql1 = "UPDATE 'cus' set 'name'=':c2' , 'add'=':c3' , 'mob'=':c4'  WHERE 'id'=':c1'";

    $st1=$conn->prepare($sql1);
    $st1->bindParam(":c2",$c2,PDO::PARAM_STR);
    $st1->bindParam(":c3",$c3,PDO::PARAM_STR);
    $st1->bindParam(":c4",$c4,PDO::PARAM_STR);
    $st1->bindParam(":c1",$c1,PDO::PARAM_INT);
    $st1->execute();
    header("location:frm.php");


}

Upvotes: 1

Views: 374

Answers (2)

Barmar
Barmar

Reputation: 781726

You need to use backticks around table and column names, or just leave the quotes out entirely if the names are not reserved words. Single quotes are for strings. You also must NOT put placeholders inside quotes.

$sql1 = "UPDATE `cus` set `name`=:c2 , `add`=:c3 , `mob`=:c4  WHERE `id`=:c1";

Upvotes: 2

Kevin
Kevin

Reputation: 41873

Identifier quotes are not single quotes. They are supposed to be backticks. Just ditch them instead.

Placeholders doesn't need quotes wrapping them:

$sql1 = "UPDATE cus SET name = :c2 , `add` = :c3 , mob = :c4  WHERE id = :c1";

EDIT:

With the exception of the reserved word ADD. That requires backticks

if(isset($_POST['submit'])) {
    $c1 = $_POST['cid1'];
    $c2 = $_POST['cname1'];
    $c3 = $_POST['cadd1'];
    $c4 = $_POST['cmob1'];

    $sql1 = "UPDATE cus SET name = :c2 , `add` = :c3 , mob = :c4  WHERE id = :c1";

    $st1 = $conn->prepare($sql1);
    $st1->bindParam(":c2",$c2,PDO::PARAM_STR);
    $st1->bindParam(":c3",$c3,PDO::PARAM_STR);
    $st1->bindParam(":c4",$c4,PDO::PARAM_STR);
    $st1->bindParam(":c1",$c1,PDO::PARAM_INT);
    $st1->execute();
    header('Location: frm.php');
}

Upvotes: 1

Related Questions