2017-04-20 53 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(); 

?>