<repo_name>Thomas-roulland/CRUD-php<file_sep>/php/update.php
<?php 
	require_once "../BDD/db.php";
	require_once "function.php";
	include "../StructurePage/header.php";

	$page=$_GET['page'];

	if($page==1)
	{
		if(isset($_POST['id']))
		{
			$id = $_POST['id'];
			$sql=Update("Organization",$_POST);
			$db=connect('groups');
			if($st=$db->prepare($sql))
			{
				var_dump($st->execute($_POST));
				header('location:../index.php?page=1');
			}
		}

		$resultat = "";

		if (isset($_GET['id'])) 
		{
			$id = $_GET['id'];
			// write SQL to get user data
			//Execute the sql
			$resultat = connect('groups')->query("SELECT * FROM Organization WHERE id='$id'");
			$data = $resultat->fetch();
		}
?>
			<div class="text-light fs-1 text fw-bold text-center mb-5">
				CRUD 
			</div>
			<form action="update.php?id=<?php echo $id;?>&page=1" method="post" class="text-light">
			<fieldset>
				<legend>Mise à jour d'une organisation :</legend>
				<label for="exampleInputPassword1" class="form-label">Nom : *</label>
				<input type="text" class="form-control w-25 mb-2" name="name" value="<?php echo $data['name']; ?>">
				<input type="hidden" name="id" value="<?php echo $id; ?>">
				<label for="exampleInputPassword1" class="form-label">Domaine :</label>
				<input type="text" class="form-control w-25 mb-2" name="domain" value="<?php echo $data['domain']; ?>">
				<label for="exampleInputPassword1" class="form-label">Site :</label>
				<input type="text" class="form-control w-25 mb-2" name="aliases" value="<?php echo $data['aliases']; ?>">
				
				<br><br>
				<button class="btn btn-primary" type="submit">Mettre à jour</button>
			</fieldset>
			</form>
<?php
	}else
	{
		if(isset($_POST['id'])) 
		{
			$id = $_POST['id'];
			$sql=Update("User",$_POST);
			$db=connect('groups');
			if($st=$db->prepare($sql))
			{
				var_dump($st->execute($_POST));
				header('location:../index.php?page=2');
			}
		}
		if (isset($_GET['id'])) 
		{
			$id = $_GET['id'];
			// write SQL to get user data
			//Execute the sql
			$resultat = connect('groups')->query("SELECT * FROM User WHERE id='$id'");
			$data = $resultat->fetch();
		}
			?>
			<form action="update.php?id=<?php echo $id;?>&page=2" method="post" class="text-light">
			<fieldset>
				<legend>Mise à jour d'un User :</legend>
				<label for="exampleInputPassword1" class="form-label">Prénom : *</label>
				<input type="text" class="form-control w-25 mb-2" name="firstname" value="<?php echo $data['firstname']; ?>">
				<input type="hidden" name="id" value="<?php echo $id; ?>">
				<label for="exampleInputPassword1" class="form-label">Nom :</label>
				<input type="text" class="form-control w-25 mb-2" name="lastname" value="<?php echo $data['lastname']; ?>">
				<label for="exampleInputPassword1" class="form-label">Mot de passe :</label>
				<input type="text" class="form-control w-25 mb-2" name="password" value="<?php echo $data['password']; ?>">
				<label for="exampleInputPassword1" class="form-label">Email :</label>
				<input type="text" class="form-control w-25 mb-2" name="email" value="<?php echo $data['email']; ?>">
				<label for="exampleInputPassword1" class="form-label">Organization : *</label>
										<select class="form-select w-25" name="idOrganization">
											<?php 
											$organisation = connect('groups')->query("SELECT * FROM Organization");
											foreach($organisation as $row) { ?>
											<option value="<?php echo $row['id'] ?>"><?php echo $row['name'] ?></option>
											<?php } ?>
										</select>
				<br><br>
				<button class="btn btn-primary" type="submit">Mettre à jour</button>
			</fieldset>
			</form>
		<?php
	}
	
	

 include '../StructurePage/footer.php'; 
<file_sep>/README.md
# CRUD-main2
 
<file_sep>/BDD/db.php
<?php

function connect(string $dbname){
    $dbo=null;
    try {
        $dbo = new PDO("mysql:host=127.0.0.1;dbname=$dbname;charset=UTF8", 'root', '');
        $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        return $dbo;
    }catch(\PDOException $e){
        return false;
    }
}<file_sep>/php/function.php
<?php


function create($table_name, $fieldvalues) {
    $cmd_keys = array_keys($fieldvalues);
    $cmd_values = array_values($fieldvalues);
    $res="INSERT INTO $table_name(";
    $res.= \implode(',', $cmd_keys);
    $res.= ") VALUES(";
    array_walk($fieldvalues, function(&$v){$v = "'$v'";});
    $res.= \implode(',', $fieldvalues);
    $res.= ");";
    return $res;
}


function Update ($table,$fieldvalues){

    $res="UPDATE `$table` SET";
    $parts=[];
    foreach ($fieldvalues as $key => $value){
        $parts[]="`$key`= :$key ";
    }
    $res.= \implode(',',$parts);
    return $res." WHERE id=:id";
    
}


function Delete ($table, $id){
    $res="DELETE FROM `$table`";
    return $res." WHERE id=$id";
    
}

function Getpage ($table){
    $res="SELECT * FROM `$table`";
    return $res;   
}
?><file_sep>/php/work.php
<?php 

require_once "../BDD/db.php";
require_once "function.php";



$cmd_connect = connect('groups');
$cmd = create("Organization", $_POST);
if(isset($_POST['name']) and isset($_POST['domain']) and isset($_POST['aliases']) and !empty($_POST['name'])) {
try {
    if(connect('groups')->exec($cmd) == 1) {
        ?> <h1 class='success'><i class='fas fa-check'></i> Organisation ajoutée <br></h1>
    <?php }
} catch(\PDOException $e) {
?>
    <h1 class='error'><i class='fas fa-times'></i> Impossible d'ajouter l'organisation</h1>
<?php }
}


$cmd_connect1 = connect('groups');
$cmd1 = create("User", $_POST);
if(isset($_POST['firstname']) and isset($_POST['lastname']) and isset($_POST['email']) and isset($_POST['password']) and isset($_POST['idOrganization']) and !empty($_POST['firstname'])) {
try {
    if(connect('groups')->exec($cmd1) == 1) {
        ?> <h1 class='success'><i class='fas fa-check'></i> User ajoutée <br></h1>
    <?php }
} catch(\PDOException $e) {
?>
    <h1 class='error'><i class='fas fa-times'></i> Impossible d'ajouter l'organisation</h1>
<?php }
}


if($_GET['id'])
$id=$_GET['id'];
$cmd_connect1 = connect('user');
$cmd1 = Delete("user", $id);

    try {
    if(connect('groups')->exec($cmd1) == 1) {
        ?> <h1 class='success'><i class='fas fa-check'></i> Suppression réussi <br></h1>
        <?php }
    }catch(\PDOException $e) {
    ?>
    <h1 class='error'><i class='fas fa-times'></i> Impossible de supprimer</h1>
    <?php }
  



if($_GET['id'])
$id=$_GET['id'];
$cmd_connect1 = connect('groups');
$cmd1 = Delete("Organization", $id);

    try {
    if(connect('groups')->exec($cmd1) == 1) {
        ?> <h1 class='success'><i class='fas fa-check'></i> Suppression réussi <br></h1>
        <?php }
    }catch(\PDOException $e) {
    ?>
    <h1 class='error'><i class='fas fa-times'></i> Impossible de supprimer</h1>
    <?php }


$page=$_GET['page'];
if($page==1){
    header('location:../index.php?page=1');
}else{
    header('location:../index.php?page=2');
}
    

<file_sep>/index.php
<?php

require_once "./BDD/db.php";

include "StructurePage/header.php";


?>

         <div class="text-light fs-1 text fw-bold text-center mb-5 ">
             CRUD 
         </div>

        <!-- Button trigger modal -->
   
       


    <?php 
        

        if($_GET['page']==1){
            $statement=connect('groups')->query("SELECT * FROM Organization");
            ?>
             <div class="d-flex justify-content-end mx-5">
        <button type="button" class="btn btn-primary mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">
            Ajoutez une organisation
        </button>
        </div>
            <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form action="php/work.php?page=1" method="POST">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Ajoutez une organisation :</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">Nom : *</label>
                                <input type="text" class="form-control align-middle" name="name" required>
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">Domaine :</label>
                                <input type="text" class="form-control" name="domain">
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">Site :</label>
                                <input type="text" class="form-control" name="aliases">
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
                            <button type="submit" class="btn btn-primary">Ajouter</button>
                        </div>
                    </form>
                    
                </div>
            </div>
        </div>

    
            <button class="btn btn-warning" onclick="window.location.href ='index.php?page=2'">
                Table User
            </button>
        

        <table class="table text-light p-5  mb-5" >
            <legend class="text-light fw-bold text-decoration-underline d-flex justify-content-left mt-5"> Données de la table Organisation :</legend>
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Nom</th>
                        <th>Domain</th>
                        <th>User</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
            <tbody>
            <?php
             foreach($statement as $row) {
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['domain']; ?></td>
                <td><?php echo $row['aliases']; ?></td>
                <td class="text-center">
                    <a class="btn btn-info mx-2" href="./php/update.php?id=<?php echo $row['id'];?>&page=1">Edit</a>
                
                    <a class="btn btn-danger mx-2" href="./php/work.php?id=<?php echo $row['id'];?>&page=1">Delete</a>
                </td>
            </tr>
    <?php 
    }
    ?>
    </tbody>
    </table>
        <?php 
        } else {
        $statement=connect('groups')->query("SELECT * FROM User");
        $organisation = connect('groups')->query("SELECT * FROM Organization");
        ?>
<div class="d-flex justify-content-end mx-5">
        <button type="button" class="btn btn-primary mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">
            Ajoutez un User
        </button>
        </div>
         <!-- Modal -->
         <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form action="php/work.php?page=2" method="POST">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Ajoutez un user :</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">Prénom : *</label>
                                <input type="text" class="form-control align-middle" name="firstname" required>
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">nom : *</label>
                                <input type="text" class="form-control align-middle" name="lastname" required>
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">email : *</label>
                                <input type="email" class="form-control align-middle" name="email" required>
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">password : *</label>
                                <input type="password" class="form-control align-middle" name="password" required>
                            </div>

                            <div class="mb-3 w-75">
                                <label for="exampleInputPassword1" class="form-label">Organization : *</label>
                                <select class="form-select" name="idOrganization">
                                    <?php foreach($organisation as $row) { ?>
                                    <option value="<?php echo $row['id'] ?>"><?php echo $row['name'] ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
                            <button type="submit" class="btn btn-primary">Ajouter</button>
                        </div>
                    </form>
                    
                </div>
            </div>
        </div>

        
            <button class="btn btn-warning" onclick="window.location.href ='index.php?page=1'">
                Table Organisation
            </button>
    
        <table class="table text-light p-5 w-100 mb-5" >
            <legend class="text-light fw-bold text-decoration-underline d-flex justify-content-left mt-5"> Données de la table User :</legend>
                <thead>
                    <tr>
                        <th>id</th>
                        <th>Prénom</th>
                        <th>Nom</th>
                        <th>email</th>
                        <th>password</th>
                        <th>Suspended</th>
                        <th>IdOrganization</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
            <tbody>
            <?php
             foreach($statement as $row) {
            ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['firstname']; ?></td>
                    <td><?php echo $row['lastname']; ?></td>
                    <td><?php echo $row['email']; ?></td>
                    <td><?php echo $row['password']; ?></td>
                    <td><?php echo $row['suspended']; ?></td>
                    <td><?php echo $row['idOrganization']; ?></td>
                    <td class="text-center">
                        <a class="btn btn-info mx-2" href="./php/update.php?id=<?php echo $row['id']; ?>&page=2">Edit</a>
                    
                        <a class="btn btn-danger mx-2" href="./php/work.php?id=<?php echo $row['id']; ?>&page=2">Delete</a>
                    </td>
                </tr>
            <?php 
            }
        } ?>
            </tbody>
    </table>
<?php include 'StructurePage/footer.php'; ?>
