2017-02-06 5 views
0

私は最近、昨年末の大学のデータベースについて学び始めたばかりで、私自身の時間にもう少し学びたいと思っています。
私は先週PostgreSQLを試し、データベースにアカウントIDを自動的にインクリメントしようとするとレンガの壁にぶつかったことをお勧めしました。PostgreSQL自動インクリメントPHP 7で挿入問題

システムは、フォームのPOSTの電子メール、ユーザー名とパスワードに加えながら、自動的に行のアカウントテーブルに「a_pid」を新たにユニークなIDを追加するためのものです。

現在のエラーメッセージ

Warning: pg_query(): Query failed: ERROR: column "email" does not exist LINE 1: ...NTO account (a_pid, email, username, pwd) VALUES ([email protected]^HINT: There is a column named "email" in table "account", but it cannot be referenced from this part of the query.


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 protected]','Zero','12345678'); 
Insert into account (a_pid,email,username,pwd) values (1,'[email protected]','One','password123'); 
Insert into account (a_pid,email,username,pwd) values (2,'[email protected]','Two','[email protected]'); 

フォームあなたが取得しているエラーメッセージで "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を挿入し、 "サイン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); 
?> 
+0

がメッセージを確認し、それが何か「)」の近くに間違っていると言い、そして 'Three、123、);では' 123 'の後にカンマが1つ追加されていることがわかりますが、新しい値はありません。それを削除して、もう一度やり直してください。 – Acapulco

+1

(1)postgresqlでは、文字列リテラルを引用符で囲む必要があると思いますが、 '' email-0 @ example.com ''のように引用していますが、コードには '' VALUES( "。 "、..."はあなたの '(a_pid、email、username、pwd)'列に 'a_pid'を指定しますが、あなたの値を削除するか、あなたの電子メールの前に 'null 'を追加する - >' VALUES(null、' "。$ email。" '、... '。(3) 。pg_query()::(4)私は( 'pg_queryを疑う)あなたは' SELECTを削除する必要がありますので、 '、マルチクエリを可能に...' – Sean

+0

@Acapulcoは1つのエラーを取り除くには、別の... 警告を得たクエリに失敗しました:ERRORを:column "email"は存在しませんLINE 1:... NTOアカウント(a_pid、email、username、pwd)VALUES(email-3 @ ex ...^ヒント: "account"という表に "email"という名前の列がありますが、この部分から照会することはできません。 –

答えて

0

ルック - これは何もありませんあなたの自動インクリメントされたシーケンスIDと関係がある。 @ titan-studiosによると、emailフィールドはDB構造に存在しないようです。あなたはa_pid, email, username, pwdフィールドを指定するだけ$email, $username, $pwd値を与える - はさておき、あなたのSQLのVALUES一部であなたのa_pidフィールドの実際の値を指定していない

Postgresにあなたの仕事をさせて、idフィールドをまったく指定しないのはなぜですか? serialというフィールドを定義すると、シーケンスの次の値のデフォルト値が自動的に設定されます。

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

また、私は強く代わりに、文字列の連結のパラメータ化準備のクエリを見てお勧めをしたい:http://php.net/manual/en/function.pg-prepare.php

$query = "INSERT INTO account (email, username, pwd) VALUES ($1, $2, $3); 
関連する問題