bon ça marche pas mais c'est le début

This commit is contained in:
thaaoblues 2023-10-22 20:01:47 +02:00
parent 394055b19b
commit 8158f4f3fa
3 changed files with 51 additions and 19 deletions

View file

@ -12,7 +12,6 @@
*/
include("session_verif.php");
include("test_creds.php");
include("bdd.php");
@ -62,9 +61,15 @@
if($_SERVER['REQUEST_METHOD'] === 'POST'){
switch(array_shift($url_parts)){
switch(array_pop($url_parts)){
case "aj_doc":
try{
ajouter_doc($_POST);
}catch(Exception $e){
echo( json_encode(["status"=> "0","msg"=> $e->getMessage() ]) );
}
break;
default:
echo(json_encode(["status"=> "2","msg"=> "Opération inconnue."]));

34
bdd.php
View file

@ -1,26 +1,33 @@
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
include("annales/test_creds.php");
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "archivinsa";
function ajouter_doc($request){
$conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO ensemble (commentaire_auteur) VALUES(\"\")";
function ajouter_doc($request){
saveFilesFromPost($request);
if (isset($request['files']) && is_array($request['files'])) {
foreach ($request['files'] as $file) {
$sql="INSERT INTO ";
}
try{
$conn->execute_query($sql,array("",));
saveFilesFromPost($request,mysqli_insert_id($conn),$conn);
}catch(Exception $e){
echo(json_encode(["status"=>"0","msg"=>$e]));
}
}
function saveFilesFromPost($postData) {
function saveFilesFromPost($postData,$id_ensemble,$conn) {
// Check if the $_POST variable is set and contains files
if (isset($postData['files']) && is_array($postData['files'])) {
// Directory to save the files
@ -47,6 +54,15 @@ function saveFilesFromPost($postData) {
} else {
echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"]));
}
//update the database
$safe_titre = htmlspecialchars($postData['titre']);
$safe_type = htmlspecialchars($postData['type']);
// pour tester, pas implémenté les commentaires globaux ni les themes
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
$conn->execute_query($sql, array("titre"=> $safe_titre,"type"=>$safe_type,"upload_path"=> $uploadDir,"commentaire_auteur"=>"","ensemble_id"=>$id_ensemble));
}
} else {
echo(json_encode(["status"=>"2","msg"=>"No files in the POST data."]));

View file

@ -14,6 +14,14 @@
<!-- Button to open the camera -->
<button onclick="openCamera()">Open Camera</button>
<input type="text" placeholder="titre" id="titre"></input>
<select id="select_type">
<option value="annale">annale</option>
<option value="fiche_revision">fiche_revision</option>
</select>
<script>
function uploadFiles() {
const fileInput = document.getElementById('fileInput');
@ -21,13 +29,16 @@ function uploadFiles() {
// Create FormData object to append files
const formData = new FormData();
formData.append("type",document.getElementById("select_type").getAttribute("value"));
formData.append("titre",document.getElementById("titre").getAttribute("value"));
// Append each selected file to the FormData
for (const file of fileInput.files) {
formData.append('files[]', file);
}
// Make a POST request using Fetch API
fetch('annales/api.php/aj_doc.php', {
fetch('api.php/aj_doc', {
method: 'POST',
body: formData
})
@ -68,7 +79,7 @@ function openCamera() {
mediaStream.getTracks().forEach(track => track.stop());
// Make a POST request to upload the image
fetch('annales/api.php/aj_doc', {
fetch('api.php/aj_doc', {
method: 'POST',
headers: {
'Content-Type': 'application/json',