Ciao a tutti.
Nella parte di sito che sto preparando ora, devo permettere agli utenti di registrarsi.
Di conseguenza devo fare in modo che i dati da loro inseriti finiscano in un database.
Il database lo sto gestendo tramite phpPgAdmin.
Ma il mio problema riguarda il fatto che non sicura di come si faccia a connettere il database e a inserire dati tramite query.
Per adesso ho creato un file pgconnect.php
Codice PHP:
<?php
class db{
function db_open() {
$this->host = $host = 'localhost';
$this->user = $user = 'administrator';
$this->password = $password = 'mikmik';
$this->dbname = $dbname = 'postgres';
$this->port = $port = 5432;
}
function db_connect(){
$this->DB_conn = pg_connect("host=".$this->host." port=".$this->port." dbname=".$this->dbname." user=".$this->user." password=".$this->password);
}
function db_close() {
pg_close ($this->db_conn);
}
function query($query){
$result = pg_query ($this->db_conn,$query);
return $result;
//return pg_send_query ($this->db_conn,$query);
}
function query_numrows($query){
$result = pg_query ($this->db_conn,$query);
$numrows = pg_num_rows($result);
return $numrows;
}
function query_arrayresult($query){
$result = pg_send_query ($this->db_conn,$query);
$res = pg_get_result($this->db_conn);
if($result){
$arrayresult=Array();
while ($risultati = pg_fetch_array ($res)){
array_push($arrayresult,$risultati) ;
}
return $arrayresult;
}else{
$err = pg_result_error_field($res,PGSQL_DIAG_MESSAGE_DETAIL);
return $err;
}
}
/*cambia l'utente che si può connettere al database
function set_user($user,$pass){
$this->user = $user;
$this->password = $pass;
}*/
}
?>
I dati vengono memorizzati in variabili $, tramite il metodo post.
A questo punto io avevo fatto:
Codice PHP:
<?php
#DATABASE E QUERY###########################################################
$db1 = new db_open();
$db1->db_connect();
$result1 = $db1->query("INSERT INTO utente (login, pw, tipo_utente) VALUES ('.$login.', '.$password.', 'privato')");
$db2 = new db_open();
$db2->db_connect();
$result2 = $db2->query("INSERT INTO privato (codf, nome, cognome, email, tel, autorizzato, login) VALUES ('.$codfiscale.', '.$nome.', '.$email.', '.$tel.', false, '.$login.')");
if (!$result1 && !$result2) {
echo "An error occured.\n" . pg_last_error();
exit;
}else{
echo "Utente ".$usr." inserito! Ora loggati alla pagina iniziale";
header('Location: index.php');
}
?>
Quando però provo a registrarmi e do l'invio, mi esce una schermata bianca e nel db non vengono memorizzati i dati.
Ho fatto solo errori di sintassi o ho proprio sbagliato concettualmente?