2017-04-20 57 views
-1

我正在使用php將從html web讀取的值添加到postgreSQL表中,並且我不知道爲什麼讀取的值不會將其自身添加到表中。我正在與pgAdminIV合作。這是我的代碼,希望有人能幫助我。無法將值添加到PostgreSQL

<?php 

    $mysqli=pg_connect("host=XXXXX.cim0cltex61z.us-west-2.rds.amazonaws.com port=5432 dbname=footplsseat user=footplsseat passsword=XXXXXXXX"); 
    $email= $_POST['email']; 

    if (!$mysqli){ 
    echo "Connection failed"; 
    } 
    else{ 
    echo "Succesfully connected"; 
    } 

    $sql = "CREATE TABLE IF NOT EXISTS testportal (
    id INT, 
    email char(50) 
)"; 

$query = "INSERT INTO testportal (email) VALUES ('$email')"; 

?> 

編輯:更改ID INT(6)UNSIGNED AUTO_INCREMENT PRIMARY KEY,爲id整數。

+1

'AUTO_INCREMENT'不是Postgres的快捷 –

+0

你怎麼pgAdmin的IV .. –

+0

運行PHP我在WAMP的服務器運行它,我想看結果在圖形界面 –

回答

0

我解決了我的問題,我混合了mySQL和postgresSQL代碼。這裏是我的解決方案:

<?php 

$mysqli=pg_connect("host=XXXXX.cim0cltex61z.us-west-2.rds.amazonaws.com port=5432 dbname=footplsseat user=footplsseat password=XXXXXX"); 
$email = pg_escape_string($_POST['email']); 
$query = "INSERT INTO testportal(email) VALUES('" . $email . "')"; 
$result = pg_query($query); 
if (!$result) { 
    $errormessage = pg_last_error(); 
    echo "Error with query: " . $errormessage; 
    exit(); 
} 
printf ("These values were inserted into the database - %s", $email); 
pg_close(); 

?>