Page 1 of 1

Inserting into a Database

Posted: Thu Dec 19, 2002 3:52 am
by jay_kay8
I am attempting to Insert a value into a Foreign Key column in a postgresql table. I am in the databases forum with this one too! I was wondering if there is anything in PHP I am doing wrong when passing the variables in, this is my code:

include("classes/dbConnector.class.php");

//create an object of the dbConnector class
$db = new dbConnector();

$conn_string = "host=127.0.0.1 port=5432 dbname=filemanager user=pgsql password=postgres";
$connect = $db->dbConnect($conn_string);

if (!$connect)
{
die("Could not open connection to database server");
}

$query = "INSERT INTO directories (directoryname,datecreated,uid) VALUES ('$newdir',now(),'$uid')";

$result = $db->dbExecQuery($connect, $query) or die("Error in query: $query . " . pg_last_error($connect));

//close database connection
$db->dbClose($connect);

This is the Error I am recieving:

Warning: pg_last_error(): 1 is not a valid PostgreSQL link resource in /usr/local/www/data-dist/filemanager/makedir.php on line 81
Error in query: INSERT INTO directories (directoryname,datecreated,uid) VALUES ('hello',now(),'1') .


Can anyone help?

Posted: Thu Dec 19, 2002 5:02 am
by f1nutter
Exactly what is on line 81? The link id reffers to the connection to the db, I guess your connection string is correct. Can you retrieve info from the db, just a simple SELECT * FROM db will do.

Also notice the now() in the error message.
INSERT INTO directories (directoryname,datecreated,uid) VALUES ('hello',now(),'1') .
That should be a time/date value. Escape the string to have it use the value, rather than the string.

Code: Select all

$query = "INSERT INTO directories (directoryname,datecreated,uid) "
          .= "VALUES ('$newdir', " .now(). ",'$uid')";

Posted: Thu Dec 19, 2002 5:14 am
by jay_kay8
Thanks for that.
I have tried that, with no joy.
This is the new Error:

Fatal error: Call to undefined function: now() in /usr/local/www/data-dist/filemanager/makedir.php on line 81

I thought to myself, can I replicate the Postgresql datetime using the following:

$today = date("Y-m-d H:i:s");

And ALTER the directories table 'datecreated' field to 'varchar'?
So I tried that, but got this error again:

Warning: pg_last_error(): 1 is not a valid PostgreSQL link resource in /usr/local/www/data-dist/filemanager/makedir.php on line 83
Error in query: INSERT INTO directories (directoryname,datecreated,uid) VALUES ('hello', '2002-12-19 12:17:02','1') .


This is line 83:

$result = $db->dbExecQuery($connect, $query) or die("Error in query: $query . " . pg_last_error($connect));

Is there any flaws?
Help, this is starting to ruin my Christmas.....

Posted: Thu Dec 19, 2002 5:29 am
by f1nutter
OK, there is no function called now(), it seemed right and is used in other languages, but what you have done looks all right.

Your database column should be type date instead of varchar.

I see you have made you own connection class. What is the code you have written for dbExecQuery?

PHP Bugs

Posted: Thu Dec 19, 2002 5:31 am
by jay_kay8
Have found an answer to my problems:

http://p2p.wrox.com/archive/pro_php/2000-12/11.asp

Might be worth a read if you're interested in PHP bugs