简体   繁体   中英

PHP and Postgresql, DEFAULT not working

I'm trying to do an insert where I use the value DEFAULT because I have the table setup with a sequence for ID (INTEGER), so it would auto-increment.

If I execute the query manually, it works perfectly:

INSERT INTO myTable VALUES (DEFAULT, 'Name T', 35, 9, 0);

However, when I execute it from PHP, it does not work:

$query = "INSERT INTO myTable VALUES (DEFAULT, '$name', $age, $type, 0)";
pg_query($query);

I know the problem it's with DEFAULT, because if I replace it with a number, it will insert fine from PHP as well.

Any idea?

Just don't insert any value for that field at all and let the sequence's default value be populated for you. Of course that means you need to specifically name your fields in your insert query.

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