繁体   English   中英

PostgreSQL自动增量插入PHP 7问题

[英]PostgreSQL Auto Increment Insert Into with PHP 7 Issue

直到去年下半年,我才刚刚开始在大学里学习数据库,并且一直在尝试自己学习更多。
建议我上周尝试PostgreSQL,并尝试将帐户ID自动增加到数据库遇到麻烦。

该系统旨在自动添加新的唯一ID到帐户表“ a_pid”中的行,同时以POST电子邮件,用户名和密码的形式添加。

当前错误信息

警告:pg_query():查询失败:错误:列“电子邮件”不存在第1行:... NTO帐户(a_pid,电子邮件,用户名,密码)值(电子邮件-3 @ ex ... ^提示:有表“帐户”中名为“电子邮件”的列,但无法在查询的此部分中引用。


DDL

create table account (
    a_pid serial primary key,
    email varchar (64),
    username varchar (16),
    pwd varchar (32)
);

DDL插入数据

Insert into account (a_pid,email,username,pwd) values (0,'email-0@example.com','Zero','12345678');
Insert into account (a_pid,email,username,pwd) values (1,'email-1@example.com','One','password123'); 
Insert into account (a_pid,email,username,pwd) values (2,'email-2@example.com','Two','53cureP@s5w0rd');

表格“ index.php”

<form name="signup" action="sign-up.php" method="post">
    Email <input name="email" type="text" maxlength="64" value="Please enter your email" onfocus="(this.value == 'Please enter your email') && (this.value = '')"   onblur="(this.value == '') && (this.value = 'Please enter your email')"><br/>
    Verify Email <input name="vf_email" type="text" maxlength="64" value="Please enter your email" onfocus="(this.value == 'Please enter your email') && (this.value = '')" onblur="(this.value == '') && (this.value = 'Please enter your email')"><br/>
    Username: <input name="username" type="text" maxlength="16" value="username" onfocus="(this.value == 'username) && (this.value = '')"   onblur="(this.value == '') && (this.value = 'username')"><br/>
    Password: <input name="pwd" type="password" minlength="8" maxlength="32"><br/>
    Password: <input name="vf_pwd" type="password" minlength="8" maxlength="32"><br/>

    <input type="submit" value="Sign up">
</form> 

PHP插入“ sign-up.php”

<?php 
$conn = "host=localhost port=5432 dbname=sql_account user=postgres password=databaseconnect";
$dbconn = pg_connect($conn);

$email = pg_escape_string($_POST['email']); 
$username = pg_escape_string($_POST['username']); 
$pwd = pg_escape_string($_POST['pwd']); 

$query = "INSERT INTO account (a_pid, email, username, pwd) VALUES (". $email .", ". $username .", ". $pwd .");
SELECT currval(pg_get_serial_sequence('account','a_pid'))";

$result = pg_query($query); 
?> 

查看您收到的错误消息-这与您自动递增的序列ID无关。 正如@ titan-studios所说,您的数据库结构中似乎缺少email字段。

除此之外,您没有在SQL的VALUES部分中为a_pid字段指定实际值, a_pid指定a_pid, email, username, pwd字段,而仅给出$email, $username, $pwd值。

为什么不让Postgres为您完成工作,而根本不指定id字段呢? 将字段定义为serial将自动设置序列的下一个值的默认值。

https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

另外,我强烈建议您查看参数化的预准备查询,而不是字符串连接: http : //php.net/manual/zh/function.pg-prepare.php

$query = "INSERT INTO account (email, username, pwd) VALUES ($1, $2, $3);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM