1 year ago
#386646
Salvatore Albano
Problem in Insert Row to mysql remote Database
Good Morning I'm trying to insert a new record in a remote mysql database using PDO. The code is pretty simple, but the behaviour of the script look very strange.
This is the code:
```
<?php
class InsertDataDemo{
const DB_HOST = $host;
const DB_NAME = $db_name;
const DB_USER = $user;
const DB_PASSWORD = $pass;
private $conn = null;
/**
* Open the database connection
*/
public function __construct(){
// open database connection
$connectionString = sprintf("mysql:host=%s;dbname=%s",
InsertDataDemo::DB_HOST,
InsertDataDemo::DB_NAME);
try {
$this->conn = new PDO($connectionString,
InsertDataDemo::DB_USER,
InsertDataDemo::DB_PASSWORD);
} catch (PDOException $pe) {
die($pe->getMessage());
}
}
/**
* insert a new task into the tasks table
* @param string $subject
* @param string $description
* @param string $startDate
* @param string $endDate
* @return mixed returns false on failure
*/
function insertSingleRow($id_anagrafica, $cognome, $nome, $sesso, $dt_nascita, $lg_nascita, $cd_fiscale, $indirizzo, $lg_residenza, $cap, $tel_cellulare, $email, $origine, $note_anag, $medico_cur, $dt_cancel) {
$task = array(':id_anagrafica' => $id_anagrafica,
':cognome' => $cognome,
':nome' => $nome,
':sesso' => $sesso,
':dt_nascita' => $dt_nascita,
':lg_nascita' => $lg_nascita,
':cd_fiscale' => $cd_fiscale,
':indirizzo' => $indirizzo,
':lg_residenza' => $lg_residenza,
':cap' => $cap,
':tel_cellulare' => $tel_cellulare,
':email' => $email,
':origine' => $origine,
':note_anag' => $note_anag,
':medico_cur' => $medico_cur,
':dt_cancel' => $dt_cancel,
);
$sql = 'INSERT INTO t_anagra_ext(id_anagrafica, cognome, nome, sesso, dt_nascita, lg_nascita, cd_fiscale, indirizzo, lg_residenza, cap, tel_cellulare, email, origine, note_anag, medico_cur, dt_cancel)
VALUES(:id_anagrafica,:cognome,:nome,:sesso,:dt_nascita,:lg_nascita,:cd_fiscale,:indirizzo,:lg_residenza,:cap,:tel_cellulare,:email,:origine,:note_anag,:medico_cur,:dt_cancel)';
$q = $this->conn->prepare($sql);
return $q->execute($task);
}
function readRows(){
$sql = 'SELECT * FROM t_anagra_ext ORDER BY id_anagrafica ASC';
foreach ($this->conn->query($sql) as $row) {
print $row['id_anagrafica'] . "\t";
print $row['cognome'] . "\t";
print $row['nome'] . "</br>";
}
}
/**
* close the database connection
*/
public function __destruct() {
// close the database connection
$this->conn = null;
}
}
$obj = new InsertDataDemo();
echo "Read Rows before insert"."</br></br>";
$obj->ReadRows();
echo "</br></br>";
echo "Do insert"."</br></br>";
if($obj->insertSingleRow('7270','Maignan','Mike','F','2022-03-
02','A001','ZLMRJQ33R52I752N','via san lorenzo
18','A001','00100','3283653934','alekseys85@gmail.com','ciao','ciao','ciao',NULL)
!== false)
echo 'A new task has been added successfully'."</br></br>";
else
echo 'Error adding the task'."</br></br>";
echo "Read Rows after insert"."</br></br>";
$obj->ReadRows();
```
When I execute the code I See:
Read Rows before insert
823 Albano Salvatore
109201 SIMONE MILETTA
109202 PROVA grant
109203 PROVA due
Do insert
A new task has been added successfully
Read Rows after insert
823 Albano Salvatore
7270 Maignan Mike
109201 SIMONE MILETTA
109202 PROVA grant
109203 PROVA due
but nothing changes in DB
where I'm going wrong?
If I execute the query in mysql workbench it works. thank you
php
mysql
database
pdo
insert
0 Answers
Your Answer