2013-07-13 37 views
1

我寫一個基本的PHP註冊腳本與以下數據庫:PHP腳本沒有插入PG數據庫

DROP TABLE IF EXISTS authentication; 
CREATE TABLE authentication (
    userID SERIAL UNIQUE REFERENCES users(userID), 
    eMail TEXT PRIMARY KEY REFERENCES users(eMail), 
    passwordSalt TEXT, 
    hashedSalt TEXT, 
    saltText TEXT, 
    securityAnswer1 TEXT, 
    securityAnswer2 TEXT, 
    securityAnswer3 TEXT 
); 

和下面的註冊腳本:

<?php 
    include 'https.php'; 

    //Create a connection to the database. If the connection fails, kill the 
    //process and issue an error message 
    $conn = pg_connect("host=localhost user=myusername password=mypassword dbname=mydatabase"); 
    if (!$conn) { 
    die("Failed to connect to database."); 
    } 

    //If the register button is pressed.. 
    if (isset($_POST['submit'])) { 
    //Check to make sure password+confirmation match. If not, error out. 
    if ($_POST['password'] != $_POST['confirm-password']) { 
     echo 'ERROR: passwords do not match<br />'; 
     echo 'Return to <a href="registration.php">registration</a> page.<br />'; 
     break; 
    } 

    //Then check to make sure the username doesn't already exist in the DB. 
    $result = pg_prepare($conn, "duplicateUser", 'SELECT username FROM mydatabase.users WHERE username = $1'); 
    $result = pg_execute($conn, "duplicateUser", array($_POST['username'])); 
    $row = pg_fetch_assoc($result); 

    //If it already exists, set match to true. 
    foreach ($row as $res) { 
     if ($res == $_POST['username']) 
     $match = true; 
    } 

    //If match is true, error out. 
    if ($match == true) { 
     echo 'ERROR: username already in use<br />'; 
     echo 'Return to <a href="registration.php">registration</a> page.<br />'; 
     $match = false; 
     break; 
    } 
    } 
?> 

<html> 
<head> 
<title>Registration page</title> 
</head> 
<body> 
<form method='POST' action='registration.php'> 
Username (e-mail): <input type='text' name='username' /><br /> 
Password: <input type='password' name='password' /><br /> 
Confirm password: <input type='password' name='confirm-password' /><br /> 
<input type='submit' name='submit' value='Register' /><br /> 
</form> 
Return to <a href='index.php'>login</a> page.<br /> 

</body> 
</html> 

<?php 

    //If the registration button is pressed.. 
    if (isset($_POST['submit'])) { 
    //Take the username and password, salt the password using an SHA1 
    //encryption of a random number, concatenate the password to the 
    //beginning of the salt, then save the SHA1 of that concatenation 
    //as the hashed and salted password. 
    $username = $_POST['username']; 
    $confirmusername = $_POST['confirm-username']; 
    $password = $_POST['password']; 
    $confirmpassword = $_POST['confirm-password']; 
    if ($username != $confirmusername) 
     echo "Please make sure your username is consistent."; 
    if ($password != $confirmpassword) 
     echo "Please make sure your passwords match."; 

    $salt = sha1(rand()); 
    $saltedPass = $password.$salt; 
    $hashedSalt = sha1($saltedPass); 

    //Store the username, hashed+salted password, and salt in the 
    //authentication table. 
    $result = pg_prepare($conn, "authentication", 'INSERT INTO mydatabase.authentication VALUES ($1, $2, $3)'); 
    $result = pg_execute($conn, "authentication", array($username, $salt, $hashedSalt)); 

    //Start the session, set the $_SESSION variables loggedin to true and 
    //username to the supplied username, then redirect to index.php 
    session_start(); 
    $_SESSION['loggedin'] = true; 
    $_SESSION['username'] = $username; 
    header("Location: index.php"); 
    } 
?> 

但是,當我得到phpPgAdmin的,行尚未被插入到數據庫中。有任何想法嗎?

謝謝。

+2

您需要爲要插入的列命名。類似於'INSERT INTO mydatabase.authentication(col1,col2,col3)VALUES($ 1,$ 2,$ 3)' –

回答

1

首先想到的是您需要顯式提交事務。 This answer似乎證實了Postgres沒有自動提交。

+0

我在pg_prepare語句中顯式聲明瞭列(email,hashedSalt,saltText),但它仍未發佈到數據庫。 如何使用pg_prepare和pg_execute提交事務?我認爲pg_execute照顧了通信? – neilthawani

+0

'pg_query('COMMIT')'。我想'pg_execute('COMMIT')'也可以。關於是否有任何方法自動提交,文檔不是很清楚,但似乎並不像其中的任何方法。 –

+0

這是postgres中不存在的自動提交的概念。無論是你開始一個事務,你必須提交它,或者你不開始一個事務,顯然你不必提交它。上面的php代碼不會啓動任何事務,所以不需要提交。 –

1

代碼應測試pg_preparepg_execute的結果,並顯示或記錄pg_last_error()錯誤。否則,你會遇到任何問題。

另外,由於它連接到一個名爲mydatabase的數據庫並引用表爲mydatabase.tablename,因此可以合理地懷疑您正在應用這種將數據庫名添加到表名中的MySQL事物。它不適用於Postgres。 MySQL的數據庫相當於PostgreSQL中的模式。

所以,除非你明確地創建了一個名爲mydatabase一個Postgres模式,嘗試不使用任何前綴:

INSERT INTO authentication VALUES ($1, $2, $3) 

這也適用於以前的SELECT這可能失敗出於同樣的原因,也沒有錯誤檢查。

+0

是的,那也行不通。我的問題是,這對我在課堂上提交的家庭作業很順利...... – neilthawani