Sinisterly
sql insert where statment need help 0.o??? - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: sql insert where statment need help 0.o??? (/Thread-sql-insert-where-statment-need-help-0-o)



sql insert where statment need help 0.o??? - Num5kull - 06-02-2017

Trying to use the following statement  in php using pdo and sqlite
keep getting errors
The separate vars can be echo'd out, so there is no problem with the inputs.
Have no problem doing a simple insert (for new row any column) and select  whatever from table column where row = 1
Looks so simple i just can't quite get i right
Code:
INSERT INTO table  ('stringstore')
VALUES ('string')
WHERE ('id') = ('100')

errorcode :
near "WHERE": syntax error:
php code
Code:
$id = "100";

if(!empty($astring))
{
   $string = $astring;
   $db = new PDO('sqlite:sqlite.db');
   $result = $db->prepare("INSERT INTO table ('stringstore') VALUES ('$string') WHERE id = ('$id')");
#debuging
if (!$result)
{
   echo "\nPDO::errorInfo():\n";
   print_r($db->errorInfo());
}
   $result->execute();
   
}

error:
Call to a member function execute() on boolean

cheers

Biggrin

Num5kull


RE: sql insert where statment need help 0.o??? - mothered - 06-02-2017

With your SQL INSERT INTO Statement, you've enclosed the id column In single quotes and you haven't ended your SQL query In a semicolon, hence the syntax error  near "WHERE".

The value of '100' Is fine (single quotes), but given the id column Is only one word (not separated) you can either enclose It In "Back Ticks", or leave It as Is, but NOT single quotes.

Example:
Column In Back Ticks
Code:
 WHERE (`id`) = ('100') ;

No Back Ticks
Code:
WHERE (id) = ('100') ;

Either of the two will work. Don't forget to use the semicolon at the end of the statement.