0% found this document useful (0 votes)
61 views5 pages

BD PHP

The document describes a PHP web application that allows users to add, search for, and delete student records from a database. The application consists of several PHP files: - index.php: Contains the main form to add new students or search/view all students - elevi_db.php: Handles form submissions to add new records or perform searches - sterge.php: Handles deleting student records based on an ID - style.css: Contains styling for the form and tables The files use PHP Data Objects (PDO) to connect to the MySQL database and perform operations like inserts, searches, and deletes in a way that catches any database errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views5 pages

BD PHP

The document describes a PHP web application that allows users to add, search for, and delete student records from a database. The application consists of several PHP files: - index.php: Contains the main form to add new students or search/view all students - elevi_db.php: Handles form submissions to add new records or perform searches - sterge.php: Handles deleting student records based on an ID - style.css: Contains styling for the form and tables The files use PHP Data Objects (PDO) to connect to the MySQL database and perform operations like inserts, searches, and deletes in a way that catches any database errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Fișierul principal index.

php:

<?php
session_start();
?>
<!DOCTYPE HTML>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Introducerea unui elev</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="div1">
<form action="elevi_db.php" method="post">
<div class="">
<p for="cod">Codul buletinului</p>
<p for="nume">Nume</p>
<p for="prenume">Prenume</p>
<p for="data_n">Data nasterii</p>
<p for="sex">Sex</p>
<p for="nota_med">Nota medie</p>
<p for="strada">Strada</p>
<p for="numar">nr.</p>
<p for="apartament">Apartamentul</p>
</div>
<div class="">
<input type="text" name="cod" value=""><br>
<input type="text" name="nume" value=""><br>
<input type="text" name="prenume" value=""><br>
<input type="date" name="data_n" value=""><br>
<select class="" name="sex">
<option value="true">Masculin</option>
<option value="false">Feminin</option>
</select><br>
<input type="text" name="nota_med" value=""><br>
<input type="text" name="strada" value=""><br>
<input type="text" name="numar" value=""><br>
<input type="text" name="apartament" value=""><br>
</div>
<br>
<input type="submit" class="btns" name="comanda" value="Adauga">
<input type="submit" class="btns" name="comanda" value="Cauta">
<input type="submit" class="btns" name="afiseaza" value="Afiseaza toate">
<input type="reset" class="btns" name="" value="Reseteaza">
</form>
</div><div class="div2">
<?php
$servername = "127.0.0.1:3306";
$username = "profy";
$password = "M0bD6N7Cyt6Y6FRO";
$dbname = "Elevi";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `I-1544`";
if(isset($_SESSION["SQL_ST"])){
$sql = $_SESSION["SQL_ST"];
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Cod buletin</th><th>Nume</th><th>Prenume</th><th>Data
n.</th><th>Sex</th><th>Nota med.</th><th>Strada</th><th>Numar</th><th>Apartament</th></tr>";

while($row = $result->fetch_assoc()) {
echo "<tr><form action='sterge.php' method='post'>";
echo "<td>".$row["Cod_Buletin"]."</td>";
echo "<input hidden type='text' name='Codul' value='".$row["Cod_Buletin"]."'>";
echo "<td>".$row["Nume"]."</td>";
echo "<td>".$row["Prenume"]."</td>";
echo "<td>".$row["Data_N"]."</td>";
if ($row["Sex"]=="1") {
echo "<td>Masculin</td>";
} else {
echo "<td>Feminin</td>";
}
echo "<td>".$row["Nota_med"]."</td>";
echo "<td>".$row["Strada"]."</td>";
echo "<td>".$row["Numarul"]."</td>";
echo "<td>".$row["Apartamentul"]."</td>";
echo "<td><input class='del' type='submit' value='Sterge'></td>";
echo "</form></tr>" . "\n";
}
echo "</table>";
} else {
if ($_SESSION["SQL_ST"]!="SELECT * FROM `I-1544`") {
echo "Nu a fost gasita nici o persoana.";
} else{
echo "Nu exista inregistrari.";
}
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
session_destroy();
?>
</div>
</body>
</html>

Fișierul pentru adaugarea înregistrarilor și operațiile de cautarea și afișare, elevi_db.php:

<?php
session_start();
$servername = "127.0.0.1:3306";
$username = "profy";
$password = "M0bD6N7Cyt6Y6FRO";
$dbname = "Elevi";

$cod = $_POST["cod"];
$nume = $_POST["nume"];
$prenume = $_POST["prenume"];
$data_n = $_POST["data_n"];
$sex_P = $_POST["sex"];
$nota_med = $_POST["nota_med"];
$strada = $_POST["strada"];
$numar = $_POST["numar"];
$apartament = $_POST["apartament"];
$sex = true;

if($_POST["comanda"]=="Adauga"){
if($sex_P=="Feminin"){
$sex = false;
}
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `I-1544`(`Cod_Buletin`, `Nume`, `Prenume`, `Data_N`, `Sex`, `Nota_med`, `Strada`,
`Numarul`, `Apartamentul`) VALUES
($cod,'$nume','$prenume','$data_n',$sex,$nota_med,'$strada',$numar,$apartament)";
$conn->exec($sql);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
} elseif ($_POST["comanda"]=="Cauta") {
$_SESSION["SQL_ST"]="SELECT * FROM `I-1544` WHERE `Nume`='".$nume."' OR
`Prenume`='".$prenume."'";
} elseif ($_POST["comanda"]=="Afiseaza toate") {
$_SESSION["SQL_ST"]="SELECT * FROM `I-1544`";
}

header("Location: http:index.php");
die();
?>

Fișierul pentru procesul de ștergere sterge.php:

<?php
$servername = "127.0.0.1:3306";
$username = "profy";
$password = "M0bD6N7Cyt6Y6FRO";
$dbname = "Elevi";

$cod = $_POST["Codul"];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM `I-1544` WHERE `Cod_Buletin`=$cod";
$conn->exec($sql);
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn=null;
header("Location: http:elevi_db.php");
die();
?>

Conectarea la baza de date în fișierele elev_db.php și sterge.php este efectuată prin metoda PDO (PHP
Data Objects), pentru ca în cazul unei erori în baza de date, la momentul inserării sau ștergerii datelor, să fie
posibilă afișarea erorii returnate. Astfel se întrerupe redirecționarea la pagina principală pntru a putea analiza
eroarea.

Fișierul pentru stiluri style.css:


form>div{
display: inline-block;
padding: 20px 10px 20px 10px;
}
p{
margin: 6px;
font-size: 20px;
}
input{
height: 20px;
margin-bottom: 3px;
width: 141px;
}
.btns{
margin-bottom: 10px;
height: 30px;
margin-left: 19px;
width: 100px;
}
.del{
height: 25px;
width: 80px;
}
td{
width:150px;
border:1px solid black;
font-size: 15px;
}
select{
height: 26px;
width: 147px;
margin-bottom: 3px;
}
body>.div1{
width: 35%;
height: 100%;
float: left;
}
body>.div2{
width: 65%;
height: 100%;
float: right;
margin-top: 20px;
}
body{
height: 100%;
}

You might also like