Inserting into a Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jay_kay8
Forum Newbie
Posts: 6
Joined: Wed Dec 18, 2002 10:55 am
Location: London, UK

Inserting into a Database

Post 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?
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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')";
jay_kay8
Forum Newbie
Posts: 6
Joined: Wed Dec 18, 2002 10:55 am
Location: London, UK

Post 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.....
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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?
Last edited by f1nutter on Thu Dec 19, 2002 5:32 am, edited 1 time in total.
jay_kay8
Forum Newbie
Posts: 6
Joined: Wed Dec 18, 2002 10:55 am
Location: London, UK

PHP Bugs

Post 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
Post Reply