-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_connection_code.php
68 lines (59 loc) · 1.87 KB
/
database_connection_code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
border-bottom: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
<?php
// Establishing a database connection
$servername = "127.0.0.1";
$username = "root";
$password = "kanak@05";
$dbname = "cynapto_assignment";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Processing the form data when submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieving the form data
$studentName = $_POST["name"];
$address = $_POST["address"];
$email = $_POST["email"];
$mobileNo = $_POST["phone"];
$gender = $_POST["gender"];
// Inserting the data into the database
$sql = "INSERT INTO student_data (name, address, email, phone, gender) VALUES ('$studentName', '$address', '$email', '$mobileNo', '$gender')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
$sql = "SELECT * FROM student_data";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Gender</th><th>Phone</th><th>Address</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["email"]. "</td><td>" . $row["gender"]. "</td><td>" . $row["phone"]. "</td><td>" . $row["address"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>