Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
9.9 kB
1
Indexable
Never
<?php 

//zapnutie vypisu chyb
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
  
 //lokalny connector na databazu 
//$conn = new mysqli("25.54.36.74", "root", "", "dds_projekt");

$servername = "25.54.36.74"; //ip adresa druheho pocitaca
$username = "PC2"; //username druheho usera
$password = "";
$dbname = "dt_projekt";

//pokial sa neviem pripojit k sebe kod skonci
/* if ($conn->connect_errno) 
{
  die("connection failed: " . $conn->connect_error);
} */

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
  die("connection failed: " . mysqli_connect_error());
}

/*if ($conn) {
  echo("connection OK ");
}*/


if(isset($_POST["submit"])) 
{
	 $meno = $_POST["meno"];
	 $priezvisko = $_POST["priezvisko"];
	 $datum = $_POST["datum"];
	 	 
     $insert = "INSERT INTO formular (meno, priezvisko, datum) VALUES ('$meno','$priezvisko','$datum')";
	 
	 //vykonas insert k sebe do DB
	 mysqli_query($conn,$insert);
	 
	 
	 //zistis si ake ID dostal posladny insert, id v zmysle aky primarny kluc dostal
	 $id_posledne = $conn->insert_id;
	 
	 //zakodujes si dany insert, nie je to treba, ale vyzera to lepsie ked to mas v databaze zasifrovane
	 $insert_decode = base64_encode($insert);
	  
	 //vlozis svoj insert do fronty a dam mu status 1, lebo na riadku 30 som ho vykonal
	 $vlozenie = "INSERT INTO rada (id_formular,id_prihlasenia, query, stav) VALUES ('$id_posledne','-1','$insert_decode', 1)";
	 
	 //vykonam insert do fronty
	 $ret = mysqli_query($conn, $vlozenie);
	 
	  
	 if($ret)
	 {  
	 
	 //vytiahnem si udaje ku vsetkym vzdialenym uzlom
		 $q = "select * from prihlasenie";
		 
		 //vykonam select
		 $logins = mysqli_query($conn, $q);
		 
		 
		 //v cykle prechadzam tolko krat, kolko uzlov mam
		 while($row = mysqli_fetch_array($logins))
		 {
		 
		 //vyrobim si databazovy connector ku vzdialenemu uzlu
			$conn_remote = new mysqli($row['server'], $row['pouzivatel'], $row['heslo'], $row['nazov_db']);
			
			$id = $row['id_prihlasenia'];
			
			//insert do fronty pre vzdialeny uzol, so statusom 0 lebo este nie je vykonany $insert_decode = toto je ten prikaz co treba dostat do kazdeho uzla, z riadku 36
			$param = "INSERT INTO rada (id_formular, id_prihlasenia, query, stav) VALUES ('$id_posledne', '$id','$insert_decode', 0)";
			
		
			//vykonam do svojej tabulky fronta
			$fronta_log = mysqli_query($conn, $param);
			
			//preverujem ci existuje spojenie na vzdialeny uzol, pokial nie preskakujem iteraciu a cyklus robi ako keby i++
			if ($conn_remote->connect_error) 
			{
			  continue;
			}
			
			//vyberam vsetko z fronty pre dany uzol co este nie je vykonane
			//totok musis urobit k by ti tam stalo vac nevykonanych query, lebo potrebujes aj tie spatne stojace vykonat a v dobrom poradi
			$check_fronta = "SELECT * FROM rada WHERE stav = 0 and id_prihlasenia =" . $id . " ORDER BY `id_rada` ASC";
			
			$check_fronta_list = mysqli_query($conn, $check_fronta);
			
			//tutuok v cykle prechadzas vsetky tie nevykonane zaznamy
			 while($rows = mysqli_fetch_array($check_fronta_list))
			 {
			 
			 //dekodujes si ten insert z riadkum 27, teda zas je to prikaz
				 $decode = base64_decode($rows['query']);
				 
				 //vzyberes si posledny primarny kluc zo vzdialenej databazy 
				 $q = "select max(id_rada) as max_id from formular";
				 
				 $return = $conn_remote->query($q);
				 
				$id_remote_max_before =  $return->fetch_assoc();
				
				 //vykonas ten dekodovany insert 
				$ret =  mysqli_query($conn_remote, $decode);
				
				
				//zase si zistit maximalne id z cudzieho
				//VYSKUSAT BEZ
				 $q = "select max(id_rada) as max_id from formular"; //VYSKUSAT BEZ
				 //VYSKUSAT BEZ
				 
				 $return = $conn_remote->query($q);
				 
				$id_remote_max_after =  $return->fetch_assoc();
				
				//pouzivas logiku ze to id z riadku 106 odpocitas od id z riadku 95 a musi ti dat vysledok 1, teda urcite vies ze insert do vzdialenej db prebehol
				 if($ret and ($id_remote_max_after['max_id'] -  $id_remote_max_before['max_id']) == 1)
				 {
					 $idfronta = $rows['id_rada'];
					 
					 $idremote = $id_remote_max_after['max_id'];
					 
					 //tu uz vies ze inseret prebehol a len si u seba updatnes frontu na status = 1, lebo uz je to vykonane
					 $update = "update rada set stav = 1, id_remote = $idremote  where id_rada = " . $idfronta;
					 
					 mysqli_query($conn, $update);
					 
				 } 
			 }
		}
    }
	 
	
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<style>
table, th, td {
  border: 1px solid;
}
table {
  width: 100%;
  border-collapse: collapse;
}

.button {
  background-color: black;
  color: #e3dcd1 !important;
  transition-duration: 0.4s;
  cursor: pointer;
}

.button:hover {
  background-color: #f2eae1;
  color: black  !important;
}

.modal-backdrop
{
    opacity:0.5 !important;
}



</style>

  <title>Projekt DT</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <link type="image/png" sizes="16x16" rel="icon" href="write.png">
</head>
<body style="background-color:#f5f0e9;">


  <h2 style=" padding-top: 30px; justify-content: center; display: flex; align-items: center;">Distribuované technológie</h2>
  <p style="justify-content: center; display: flex; align-items: center;">Prihlasovanie študentov na skúšku.</p>

<div class="btn-toolbar" style="padding-left: 15px; padding-top: 30px; justify-content: center; display: flex; align-items: center;">

  </br>
<!--MODAL1-->

<button type="button" class="btn button col-sm" data-toggle="modal" data-target="#exampleModal2" style="border: 1px solid black">
Prihlásiť sa na skúšku
</button>

<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content" >
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
	  <p><b>Prihlásenie na skúšku </b></p>
        
		<div class="container">
  <form class="form-horizontal" action="test_projekt.php" method="POST">
    <div class="form-group">
      <label class="control-label col-sm-2" for="meno">Meno: </label>
      <div class="col-sm-4">
        <input type="meno" class="form-control" id="meno" placeholder="Zadajte meno" name="meno">
      </div>
    </div>
	<div class="form-group">
      <label class="control-label col-sm-2" for="priezvisko">Priezvisko: </label>
      <div class="col-sm-4">
        <input type="priezvisko" class="form-control" id="priezvisko" placeholder="Zadajte priezvisko" name="priezvisko">
      </div>
    </div>
	
	<div class="form-group">
      <label class="control-label col-sm-2" for="datum">Dátum: </label>
      <div class="col-sm-4">
		<select name="datum" id="datum" class="form-control" placeholder="Vyberte datum">
		  <option value="21.12.2022">21.12.2022</option>
		  <option value="3.1.2023">3.1.2023</option>
		  <option value="19.1.2023">19.1.2023</option>
		  <option value="27.1.2023">27.1.2023</option>
		</select>        
      </div>
    </div>
	
  <!--<button onclick="myFunction()">Potvrd datum</button>-->
  
	  <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <input type="submit" name="submit" onclick="myFunction"></input>
      </div>
    </div>		 
  </form>

</div>

<!--<p id="demo"></p>-->
	

	 <script> 
	  function myFunction() {
	  var vybrate = $("#datum option:selected").text();
	  ///document.write(datum)
	  document.getElementById("demo").innerHTML = vybrate;
	  }
	  </script> 

      </div>
    </div>
  </div>
</div>


<!--MODAL2-->


<button type="button" class="btn button col-sm" data-toggle="modal" data-target="#exampleModal" style="border: 1px solid black">
Zobraziť prihlásených študentov
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
	  <p><b>Prihlásení študenti</b></p>
        <?php

$result = mysqli_query($conn,"SELECT * FROM formular order by id_formular DESC");

if(!empty($result))
{
?>
<div class="container" style="display:table; width: 100%; height: 100%">
<table>
	  <tr>
	    <td>ID</td>
		<td>Meno</td>
		<td>Priezvisko</td>
		<td>Datum</td>

	  </tr>
			<?php
			$i=0;
			while($row = mysqli_fetch_array($result)) 
			{
			?>
	  <tr>
	    <td><?php echo $row["id_formular"]; ?></td>
		<td><?php echo $row["meno"]; ?></td>
		<td><?php echo $row["priezvisko"]; ?></td>
		<td><?php echo $row["datum"]; ?></td>
		<!--<td><a href="delete.php?idform=<?php //echo $row["idform"]; ?>">Delete</a></td>-->
      </tr>
			<?php
			$i++;
			}
			?>
</table>

</div>
<?php

	}
?>

      </div>
    </div>
  </div>
</div>


</div>
</body>
</html>