Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head><title>insert form</title></head>
- <body bgcolor="1e9dae" text="white">
- <font face="roboto" >
- <p> FORM INSERTION</p><br>
- <form action="add.php" method="post">
- <table>
- <tr><td>name :<td><input type="text" name="name"><td><tr>
- <br>
- <tr><td>email :<td><input type="text" name="email"><td><tr>
- <br>
- <tr><td>age :<td><input type="int" name="age"><td><tr>
- <br>
- <tr><td>adress :<td><textarea rows="4" cols="20" name="address"></textarea><td><tr>
- <br>
- </table>
- gender :<br>
- <input type="radio" name="gender" value="1" checked> Male<br>
- <input type="radio" name="gender" value="2"> Female<br>
- <input type="radio" name="gender" value="3"> Other <br>
- marital status :
- <select name="marstatus" id="marstatus">
- <option value=""> -select- </option>
- <option value="1">married</option>
- <option value="2">unmarried</option>
- </select>
- <br>
- <input type="Submit" name="Submit">Submit</input>
- </form>
- </font>
- </body>
- </html>
- <html>
- <head>
- <title>Add Data</title>
- </head>
- <body>
- <?php
- //including the database connection file
- require_once("config.php");
- if(isset($_POST['Submit'])) {
- $name = mysqli_real_escape_string($mysqli, $_POST['name']);
- $age = mysqli_real_escape_string($mysqli, $_POST['age']);
- $email = mysqli_real_escape_string($mysqli, $_POST['email']);
- $address = mysqli_real_escape_string($mysqli, $_POST['address']);
- if(isset($_POST['gender']) && !empty($_POST['gender'])){
- $gender = mysqli_real_escape_string($mysqli, $_POST['gender']);
- }else{
- $gender = '';
- }
- $marital_status = mysqli_real_escape_string($mysqli, $_POST['marstatus']);
- // checking empty fields
- if(empty($name) || empty($age) || empty($email) || empty($address) || empty($gender) || empty($marital_status)) {
- if(empty($name)) {
- echo "<font color='red'>Name field is empty.</font><br/>";
- }
- if(empty($age)) {
- echo "<font color='red'>Age field is empty.</font><br/>";
- }
- if(empty($email)) {
- echo "<font color='red'>Email field is empty.</font><br/>";
- }
- if(empty($address)) {
- echo "<font color='red'>Address field is empty.</font><br/>";
- }
- if(empty($gender)) {
- echo "<font color='red'>Gender field is empty.</font><br/>";
- }
- if(empty($marital_status)) {
- echo "<font color='red'>Marital Status field is empty.</font><br/>";
- }
- //link to the previous page
- echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
- } else {
- // if all the fields are filled (not empty)
- $date = date('Y-m-d H:i:s');
- $query = "INSERT INTO USERS1(NAME,AGE,EMAIL,ADRESS,GENDER,MARITAL_STATUS,DATE) VALUES('$name','$age','$email','$address','$gender','$marital_status','$date');";
- //insert data to database
- $result = mysqli_query($mysqli,$query);
- //display success message
- echo "<font color='green'>Data added successfully.";
- echo "<br/><a href='index.php'>View Result</a>";
- }
- }
- ?>
- </body>
- </html>
- <?php
- /*
- // mysql_connect("database-host", "username", "password")
- $conn = mysql_connect("localhost","root","root")
- or die("cannot connected");
- // mysql_select_db("database-name", "connection-link-identifier")
- @mysql_select_db("test",$conn);
- */
- /**
- * mysql_connect is deprecated
- * using mysqli_connect instead
- */
- $databaseHost = 'localhost';
- $databaseName = 'TESTDB';
- $databaseUsername = 'root';
- $databasePassword = 'LAB2';
- $mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
- if (mysqli_connect_errno()) {
- die("Connection failed: " . mysqli_connect_errno());
- }
- ?>
Add Comment
Please, Sign In to add comment