简体   繁体   中英

php postgresql - error in my request when i'm trying to insert a string

I'm trying to customize my ERP which works with a PostgreSQL db with some php files. -When I'm displaying results with a select query, everything is running perfectly and I get results. -When I insert Default values or integer, I don't get errors and the insertion work well. However, when I try to insert string I always get an error.

display.php

<?php
$dbconn = pg_connect("host='x.X.x.X' port='x' 
dbname='x' user='x' password='x'") or 
die("unable to connect database");
$query = 'SELECT * FROM "OpenConcerto48"."CLIENT" ';
$result = pg_query($dbconn, $query);
if (!$result) {
echo null;
exit;
}
$resultArray = pg_fetch_all($result);
echo json_encode($resultArray);
pg_close ($dbconn);
?>    

insert.php

$dbconn = pg_connect("host='x.X.X.X' port='x' dbname='x' user='x' 
password='x'") or die("unable to connect database");

$a = pg_escape_string('sarl') ;

//Insertion in db work well when values are "default" or integer and i get this error :  ERREUR: la colonne « test » n'existe pas LINE 3: VALUES (DEFAULT,DEFAULT, "test", DEFAULT) ^
$query = 'INSERT INTO "OpenConcerto48"."CLIENT"
("ID","FORME_JURIDIQUE","NOM","CODE")
VALUES (DEFAULT,DEFAULT, 58, DEFAULT)';

//Insertion don't work when i try to insert a string
$query = 'INSERT INTO "OpenConcerto48"."CLIENT"
("ID","FORME_JURIDIQUE","NOM","CODE")
VALUES (DEFAULT,DEFAULT, "test", DEFAULT)';

if (pg_query($dbconn,$query))
{
echo "saved";
}
else
{
echo "error insering data  ";
echo pg_last_error($dbconn);
//echo pg_last_error($query);
}

The problem is when I insert a string, it considers it as a column and I get an error. ps : I have many schema in my database and I'm working on the schema openconcerto48 on table client.

Instead if

 VALUES (DEFAULT,DEFAULT, "test", DEFAULT)';

use single quotes:

 VALUES (DEFAULT,DEFAULT, 'test', DEFAULT)';

Also is weird in previous query you have

 VALUES (DEFAULT,DEFAULT, 58, DEFAULT)';

So if 58 is also a string you should use '58'

Try PHP DEMO :

<?php //php 7.0.8

    $query = 'INSERT INTO "OpenConcerto49"."CLIENT" 
("ID","FORME_JURIDIQUE","NOM","CODE") VALUES 
(DEFAULT,DEFAULT,\'test\', DEFAULT)';

    echo $query;

?>

@KarstenKoop, i tried :

$a = 'sarl';
$query = 'INSERT INTO "OpenConcerto49"."CLIENT" 
("ID","FORME_JURIDIQUE","NOM","CODE") VALUES 
(DEFAULT,DEFAULT,pg_escape_string('.$a.'), DEFAULT)';

got this error :

ERREUR: la colonne « sarl » n'existe pas LINE 3: VALUES(DEFAULT,DEFAULT,pg_escape_string(sarl), DEFAULT) ^ –

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