diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-09-28 16:29:16 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-09-28 16:29:16 +0530 |
commit | 71a5768da1b297c807beee4e3c6a4942126c66e9 (patch) | |
tree | 6a21f670cebf2b8ec7bdebf9ab92e589589bdce2 /Lab06 | |
parent | c2f4b5442c065c2a7a6288ce46ed409aa054d8bb (diff) |
Lab06: Creating a MySQL Database with PHP and Storing HTML Form Data
Diffstat (limited to 'Lab06')
-rw-r--r-- | Lab06/create_db.php | 23 | ||||
-rw-r--r-- | Lab06/create_table.php | 37 | ||||
-rw-r--r-- | Lab06/form1.php | 36 | ||||
-rw-r--r-- | Lab06/form1_submit.php | 34 | ||||
-rwxr-xr-x | Lab06/test.php | 5 |
5 files changed, 135 insertions, 0 deletions
diff --git a/Lab06/create_db.php b/Lab06/create_db.php new file mode 100644 index 0000000..c220148 --- /dev/null +++ b/Lab06/create_db.php @@ -0,0 +1,23 @@ +<?php +$servername = "localhost"; +$username = "it"; +$password = "smit#1234"; + +//Create connection +$conn = new mysqli($servername, $username, $password); + +//Check connection +if ($conn->connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +//Create database +$sql = "CREATE DATABASE it_202000096"; +if ($conn->query($sql) === TRUE) { + echo "Database created successfully"; +} else { + echo "Error creating database: " . $conn->error; +} + +$conn->close(); +?> diff --git a/Lab06/create_table.php b/Lab06/create_table.php new file mode 100644 index 0000000..f11a89b --- /dev/null +++ b/Lab06/create_table.php @@ -0,0 +1,37 @@ +<?php +$servername = "localhost"; +$username = "it"; +$password = "smit#1234"; +$database = "it_202000096"; +$tableName = "studentdb"; + +//Create connection +$conn = new mysqli($servername, $username, $password); + +//Check connection +if ($conn->connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +//Select the database +$conn->select_db($database); + +//Create table +$sql = "CREATE TABLE `$tableName` ( + id INT AUTO_INCREMENT PRIMARY KEY, + fname VARCHAR(50) NOT NULL, + lname VARCHAR(50) NOT NULL, + age INT, + gender VARCHAR(10), + phone VARCHAR(15), + email VARCHAR(100) +)"; + +if ($conn->query($sql) === TRUE) { + echo "Table created successfully"; +} else { + echo "Error creating Table: " . $conn->error; +} + +$conn->close(); +?> diff --git a/Lab06/form1.php b/Lab06/form1.php new file mode 100644 index 0000000..5348baf --- /dev/null +++ b/Lab06/form1.php @@ -0,0 +1,36 @@ +<html> +<head> + <style> + select { + margin-bottom: 10px; + } + #submit { + margin-top: 10px; + } + </style> +</head> +<body> + <form action="form1_submit.php" method="post"> + <label for="fname">First Name:</label><br> + <input type="text" id="fname" name="fname"><br> + + <label for="lname">Last Name:</label><br> + <input type="text" id="lname" name="lname"><br> + + <label for="age">Age: </label><br> + <input type="number" id="age" name="age"><br> + + <label for="gender">Gender:</label><br> + <select id="gender" name="gender"> + <option value="Male">Male</option> + <option value="Female">Female</option> + <option value="Other">Other</option> + </select><br> + + <label for="email">Email Address:</label><br> + <input type="email" id="email" name="email"><br> + + <input type="submit" id="submit" value="Submit"> + </form> +</body> +</html> diff --git a/Lab06/form1_submit.php b/Lab06/form1_submit.php new file mode 100644 index 0000000..f1cb514 --- /dev/null +++ b/Lab06/form1_submit.php @@ -0,0 +1,34 @@ +<html> + +<body> + Welcome <?php echo $_POST["fname"]; ?><br> + Your email address is:<?php echo $_POST["email"]; ?><br> + <?php + echo "<pre>"; + print_r($_POST); + echo "</pre>"; + $servername = "localhost"; + $username = "it"; + $password = "smit#1234"; + $dbname = "it_202000096"; + //Create connection + $conn = mysqli_connect($servername, $username, $password,$dbname); + //Check connection + if ($conn->connect_error) { + die("Connection failed: " . $conn->connect_error); + } + $fname= $_POST['fname']; + $lname= $_POST['lname']; + $age= $_POST['age']; + $gender= $_POST['gender']; + $email= $_POST['email']; + $sql = "INSERT INTO student (fname, lname, age, gender,email) VALUES ('$fname','$lname','$age','$gender','$email')"; + if (mysqli_query($conn, $sql)) { + echo "New record created successfully"; + } else { + echo "Error: " . $sql . "<br>" . mysqli_error($conn); + } + mysqli_close($conn); + ?> +</body> +</html> diff --git a/Lab06/test.php b/Lab06/test.php new file mode 100755 index 0000000..61f68d9 --- /dev/null +++ b/Lab06/test.php @@ -0,0 +1,5 @@ +<?php +echo "Hello world"; + +?> + |