· 5 years ago · May 14, 2020, 01:06 PM
1<html>
2
3<head>
4
5<title>STUDENT INFO</title>
6
7</head>
8
9<body>
10
11<div>
12
13<form action="index.php" method="POST" autocomplete="off">
14
15<h2>Students:</h2>
16
17<input class="button" name="showButton" type="submit" value="Show list"/>
18 <br>
19 <a href="/add.php"> Add user</a>
20 <br>
21 <b>Delete information about student by id:</b>
22 <br>
23 Id: <input class="number" name="deleteId"/>
24 <input class="button" name="deleteButton" type="submit" value="Delete information"/>
25 <br>
26 <br>
27
28 </div>
29
30 <?php
31 $connectionStr = new mysqli("172.20.0.3","root", "22222222", "students");
32
33 $creadtTable = "create table if not exists students (id int auto_increment primary key, name varchar(50) not null, subject varchar(50) not null, score int(10) not null)";
34 $connectionStr-> query($creadtTable);
35 if($connectionStr->connect_error) {
36 die(" Connection error! " . $connectionStr->connect_error);
37 }
38
39 function Show($connectionStr) {
40 $select = "SELECT id, name, subject, score FROM students";
41 $result = $connectionStr->query($select);
42 if($result->num_rows > 0) {
43 echo "<table>";
44
45 while($row = mysqli_fetch_array($result)) {
46 echo "<tr><td>" . "id:" . $row['id'] . "</td><td>" . "name:" . $row['name'] . "</td><td>" . "subject:" . $row['subject'] . "</td><td>" . "score:" . $row['score'] . "</td></tr>";
47 }
48
49 echo "</table>";
50 }
51 else {
52 echo "The is no information. You can add require information.";
53 }
54 }
55
56
57 function Delete($connectionStr) {
58 $id = $_POST['deleteId'];
59 $delete = "DELETE FROM students WHERE id='$id'";
60 if(mysqli_query($connectionStr, $delete)){
61 Show($connectionStr);
62 echo "<br><br> Deleted! ";
63
64 }
65 else
66 {
67 echo "Error: " .$delete. "<br>" . mysqli_error("connectionStr");
68 }
69
70 }
71 function Add($connectionStr){
72 $name = $_POST['inputName'];
73 $subject = $_POST['inputSubject'];
74 $score = $_POST['inputScore'];
75 $insert = "INSERT INTO students (name, subject, score) VALUES ('$name', '$subject', $score)";
76 if(mysqli_query($connectionStr, $insert)) {
77 Show($connectionStr);
78 echo "<br><br> Added! ";
79
80 }
81 else
82 {
83 echo "Error: " .$insert. "<br>" . mysqli_error("connectionStr");
84 }
85 }
86 if($_POST['showButton']) { Show($connectionStr);}
87 if($_POST['deleteButton']) { Delete($connectionStr); }
88 if($_POST['addButton']) { Add($connectionStr);}
89 $connectionStr->close();
90 ?>
91 </body>
92 </html>