0

Here's the table definition in the database, I can insert rows no problem with PG Admin

CREATE TABLE contact
(
  id serial NOT NULL,
  user_name character varying(50) NOT NULL DEFAULT ''::character varying,
  first_name character varying(50),
  last_name character varying(50),
  address character varying(100),
  phone character varying(25),
  email character varying(50),
  CONSTRAINT contact_pkey PRIMARY KEY (id),
  CONSTRAINT contact_user_name_key UNIQUE (user_name)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE contact
  OWNER TO postgres;

I try to connect with this, and its always relation << contact >> doesn't exist, I have tried with other tables and the results were the same, relation << contact >> doesn't exist

    $username = $_POST['user_name'];
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    $host = "host=127.0.0.1";
    $port = "port=5432";
    $dbname = "dbname=testdb";
    $credentials = "user=postgres password=postgres";
    $db = pg_connect( "$host $port $dbnam $credentials" );

    if(!$db) {
        printMessage ("Can't connect to the database");
        die();
    }

    $sql =<<<EOF
        INSERT INTO contact (user_name, first_name, last_name, address, phone, email)
        VALUES ('$username', '$firstname', '$lastname', '$address', '$phone', '$email' );

        INSERT INTO user_login (id, password)
        VALUES((SELECT id FROM contact where user_name = '$username'), '$password');
EOF;

    $ret = pg_query($db, $sql);
    if(!$ret){
        echo pg_last_error($db);
    } 
    else {
        printMessage("Registered successfully");
    }

    pg_close($db);

I'm using Bitnami

Mideel
  • 17
  • 6
  • If your table is created in a schema different from `public`, then you have to prefix the relation name with the schema like `my_schema.contact`. – greg Mar 14 '16 at 09:17
  • I've checked it again, with this code although I used dbname=testdb, I kept using the postgres database instead. So if I have database name which has the sane name as the username, I will end up using that database ??? is it like that ??? – Mideel Mar 14 '16 at 12:36
  • The schema is public, I just ended up in postgres database – Mideel Mar 14 '16 at 12:37
  • In that case you may not be reaching the database you think. Try with a psql command line with the same parameters from the same server as the web server. – greg Mar 14 '16 at 14:49
  • Ah, sorry, the mistake was $dbnam instead of $dbname, and without database specified ( cause $dbname was new variable so its considered empty string ), I guess pg_connect used the same name for database with the name specified in user=postgres – Mideel Mar 15 '16 at 12:12

0 Answers0