1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?php
/* -------------------------------------------------BACK END EN PDO------------------------------------------ */
include('connexionConfig.php');
header( 'content-type: text/html; charset=utf-8' );
error_reporting(E_ALL ^ E_NOTICE); // important pour ne pas afficher les notice PHP sans cela le script ne marche pas.
/** Switch Case pour récupérer la l'action demandée par le controleur Angular **/
switch($_GET['action']) {
case 'get_stations' : get_stations();
break;
case 'insert_station' : insert_station();
break;
}
/* --------------------------------------------------CRUD ------------------------------------------*/
function get_stations() {
try
{
$DB = connection();
$data = $DB->query('SELECT * FROM stations');
/* Convertit en JSON */
print_r(json_encode($data->fetchAll(PDO::FETCH_ASSOC)));
/* ferme la connexion ? */
$DB=null;
}
catch(PDOException $e)
{
file_put_contents('PDOErreurs.txt', $e->getMessage(), FILE_APPEND);
}
}
function insert_station() {
/* Récupération des données POST Provenant du Front end*/
$data = json_decode(file_get_contents("php://input"));
/* Insertion en Bdd avec PDO */
try {
$DB = connection();
$req = $DB->prepare("INSERT INTO stations VALUES (?,?,?,?,?,?,?)");
$req->execute(array(null,$data->nom,$data->type,$data->image,$data->telephone,$data->email,$data->taille));
$DB=null;
} catch (PDOException $e) {
file_put_contents('PDOErreurs.txt', $e->getMessage(), FILE_APPEND);
die();
}
} |
Partager