Projekt_na_JavaScript_MG_2022/action.php
2022-02-16 23:26:38 +01:00

114 lines
2.0 KiB
PHP

<?php
//action.php
$connect = new PDO("mysql:host=localhost;dbname=vuejs", "root", "");
$received_data = json_decode(file_get_contents("php://input"));
$data = array();
if($received_data->action == 'fetchall'){
$query = "
SELECT * FROM tbl_sample
ORDER BY id DESC
";
$statement = $connect->prepare($query);
$statement->execute();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}
if($received_data->action == 'insert'){
$data = array(
':tytul' => $received_data->tytul,
':autor' => $received_data->autor,
':rok' => $received_data->rok
);
$query = "
INSERT INTO tbl_sample
(tytul, autor, rok)
VALUES (:tytul, :autor, :rok)
";
$statement = $connect->prepare($query);
$statement->execute($data);
$output = array(
'message' => 'Data Inserted'
);
echo json_encode($output);
}
if($received_data->action == 'fetchSingle'){
$query = "
SELECT * FROM tbl_sample
WHERE id = '".$received_data->id."'
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$data['id'] = $row['id'];
$data['tytul'] = $row['tytul'];
$data['autor'] = $row['autor'];
$data['rok'] = $row['rok'];
}
echo json_encode($data);
}
if($received_data->action == 'update'){
$data = array(
':tytul' => $received_data->tytul,
':autor' => $received_data->autor,
':rok' => $received_data->rok,
':id' => $received_data->hiddenId
);
$query = "
UPDATE tbl_sample
SET tytul = :tytul,
autor = :autor,
rok = :rok
WHERE id = :id
";
$statement = $connect->prepare($query);
$statement->execute($data);
$output = array(
'message' => 'Data Updated'
);
echo json_encode($output);
}
if($received_data->action == 'delete'){
$query = "
DELETE FROM tbl_sample
WHERE id = '".$received_data->id."'
";
$statement = $connect->prepare($query);
$statement->execute();
$output = array(
'message' => 'Data Deleted'
);
echo json_encode($output);
}
?>