-1

I am trying to use PDO Insert statement in the following manner (it's not working though..)

$db = new PDO("mysql:host=localhost;dbname=XXXXX","XXXX","");
$query= "INSERT INTO tableS VALUES ("475","1111","XXXY","PREQ","XX");
$result= $db->query($query);
$result -> closeCursor();
$db=null;

What am I doing wrong? I've ensured the entries are correct, am quite new to PDO.

stretchr
  • 615
  • 2
  • 9
  • 24
  • 4
    Look at the highlighting in your question. It should be pretty clear. – andrewsi Feb 24 '14 at 17:00
  • 1
    [Turning on error reporting](http://stackoverflow.com/a/6575502/1438393) might help you figure out such syntax errors easily. – Amal Murali Feb 24 '14 at 17:01
  • You can't use the same kind of quotes inside a string as you use to delimit the string itself, unless you escape them. – Barmar Feb 24 '14 at 17:02
  • 2
    This has **NOTHING** to do with PDO at all. It's purely a PHP syntax error. – Marc B Feb 24 '14 at 17:03
  • @AmalMurali Turning on error reporting won't work, since PHP can't even parse the script. You need to look at the PHP error log to see parse errors. – Barmar Feb 24 '14 at 17:04
  • 3
    @Barmar: [Why not?](http://3v4l.org/m04Z5) – Amal Murali Feb 24 '14 at 17:07
  • Because if PHP can't parse the script, it will never execute the `error_reporting()` call. – Barmar Feb 24 '14 at 17:08
  • @AmalMurali: You brilliant man! I have a new tool for testing now :D Thanks – Magictallguy Feb 24 '14 at 17:08
  • I don't think this should be off-topic, I am facing a connection/insert issue that's likely more than typographical error and the problem can be reproduced albeit in a tedious manner. the problem could be related to my wamp version, or something else which I am not aware of at this point. – stretchr Feb 25 '14 at 23:47
  • @ChrisForrence - sure. I didn't get any answer here for hours, so I reposted as a new qn. http://stackoverflow.com/questions/22044858/pdo-not-working-in-php-on-wamp-server – stretchr Feb 26 '14 at 14:57

2 Answers2

2

Basically, you'll need to mix single- and double-quotes to properly enclose strings within a string. There was also a missing " at the end of the query, which meant that your string wasn't properly closed off. If you look at the code formatting in your question, you can see this from the red lines below $query; PHP thinks that those lines are part of the string!

$db = new PDO("mysql:host=localhost;dbname=XXXXXXX","XXXXXXX","XXXXXXX");

// Either mix single- and double-quotes...
$query = "INSERT INTO tableS VALUES ('475','1111','XXXY','PREQ','XX')";
// ...or escape your quotes.
// $query = "INSERT INTO tableS VALUES (\"475\",\"1111\",\"XXXY\",\"PREQ\",\"XX\")";
$result = $db->query($query);
$result->closeCursor();
$db = null;
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
  • thanks for the edit Chris, somehow it's still not inserting. – stretchr Feb 24 '14 at 17:18
  • @stretchr - Still not inserting? Then there are a few things to check. Does `tableS` exist, and does it have exactly five columns? You can also look into using [`PDO::errorCode`](http://www.php.net/manual/en/pdo.errorcode.php) and [`PDO::errorInfo`](http://www.php.net/manual/en/pdo.errorcode.php) to get any SQL errors. – Chris Forrence Feb 24 '14 at 17:22
  • yes have checked both details are correct (tableS exists, exactly 5 columns) .Trying the error checks now – stretchr Feb 24 '14 at 17:29
0

You have used " to open the $query variable but then used " again to assign one of the values. Use ' instead.

Albzi
  • 15,431
  • 6
  • 46
  • 63