Question

In: Computer Science

Using PHP and MySQL Create a simple web form with at least 5 fields Include validation...

Using PHP and MySQL

  1. Create a simple web form with at least 5 fields
  2. Include validation and error messages
  3. Create a MySQL Database
  4. Create a table to store submissions from the form
  5. Only insert new data into the database when all validation is passed
  6. Use PHP to create an HTML table showing all the content of the database  

New submissions should appear in table

Solutions

Expert Solution

Create DataBase:

Create DATABASE myDB

// Create table

CREATE TABLE users(name varchar(30) not null , email varchar(50) not null,
 phone int(10) not null, birthday varchar(25) , gender varchar(1) not null);

Form.php:


<!DOCTYPE HTML>  
<html>
<head>
</head>
<body>  

<?php
// define variables and set to empty values
$name = $email = $gender = $birthday = $phone = "";
$updateDB=false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $updateDB=true;
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
        $updateDB=false;
      } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
          $nameErr = "Only letters and white space allowed";
        }
      }
      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        $updateDB=false;
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Invalid email format";
        }
      }
  $phone = test_input($_POST["phone"]);
  $birthday = test_input($_POST["birthday"]);
  $gender = test_input($_POST["gender"]);
  
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


?>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
table.center {
  margin-left:auto; 
  margin-right:auto;
}
</style>

<h2>PHP Form Validation </h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="email" name="email">
  <br><br>
  phone: <input type="tel" name="phone">
  <br><br>
  birthday: <input name="birthday" type='date'></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="F">Female
  <input type="radio" name="gender" value="M">Male
  <input type="radio" name="gender" value="O">Other
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>
<p><h3>List of users</h3></p>
<table style="width:100%; margin-left: auto;
            margin-right: auto; ">
                <tr>
                    <th>Name</th>
                    <th>Email</th> 
                    <th>Phone</th>
                    <th>Birthday</th>
                    <th>Gender</th>
                </tr>
<?php
//Database Connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
if($updateDB){

            $sql = "INSERT INTO users (name, email, phone, birthday, gender)
            VALUES ('$name', '$email',  $phone,'$birthday', '$gender' )";

            if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
            } 
            else 
            {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
        $sql = "SELECT * FROM users";
        $result = mysqli_query($conn, $sql);
        
        if (mysqli_num_rows($result) > 0) {
          // output data of each row
          while($row = mysqli_fetch_assoc($result)) {
            //echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>";

            echo('
            
                <tr>
                    <td>'. $row["name"].'</td>
                    <td>'. $row["email"].'</td>
                    <td>'. $row["phone"].'</td>
                    <td>'. $row["birthday"].'</td>
                    <td>'. $row["gender"].'</td>
                </tr>

            
            ');


          }
        } else {
          echo "0 results";
        }

$conn->close();
?>
</table>
</body>
</html>


Related Solutions

Using PHP and MYSQL and with a simple customer database, how can I create a simple...
Using PHP and MYSQL and with a simple customer database, how can I create a simple log in and registration system for an ecommerce site
using PDO, MYSQL, and Html, how can i create a simple registration and login form for...
using PDO, MYSQL, and Html, how can i create a simple registration and login form for cPanel?
Create a web page using PHP that allows the users to create an account (a username...
Create a web page using PHP that allows the users to create an account (a username and a password). There should be a login page that allows users to create an account by entering their preferred username and password. The same page should also let users login if they already have an account. If a user is logged in successfully , they should be able to enter a comment and also read the comments entered by others previously.
Using PHP, Create a form that uses the method POST to send the information. The form...
Using PHP, Create a form that uses the method POST to send the information. The form should capture the distance the package needs to travel with the one field for the distance in miles. All fields should have the REQUIRED attribute. The form should have a submit button and reset button. The form should look nice. All the labels should line up horizontally and all the INPUTS/SELECT should line up horizontally.
Create a simple web page with two(2) AJAX interactions with a mySQL Database AJAX 1 should...
Create a simple web page with two(2) AJAX interactions with a mySQL Database AJAX 1 should return HTML content for use on the page AJAX 2 should return JSON content for use on the page Use a JS library, such as jQuery, to ensure the AJAX works on all browsers
Create a web page using PHP and HTML that contains a list of movie names available...
Create a web page using PHP and HTML that contains a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required. 2) The rental price for each movie ranges between $1.99 to $7.99. • Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental...
- Create an html form (bank form) and the PHP code with 2 accounts with options...
- Create an html form (bank form) and the PHP code with 2 accounts with options to transfer, deposit or withdraw money.
PHP The database should have at least two tables with, at minimum, the following fields: Table...
PHP The database should have at least two tables with, at minimum, the following fields: Table customers: Fields: Table billing: Fields: customer_ID customer_ID customer_L_Name customer_L_Name customer_F_Name service customer_Title (Mr, Ms, Dr. etc,) customer_bill street_Address amt_paid city_State_Zip bill_date customer_Phone date_paid customer_Email Create a PHP page that will extract a customer’s bill amount and the amount paid. Then it will calculate the amount due. If the amount due is greater than 0, an email should be generated and sent to the customer...
Create a very simple temperature converter form having two text fields. The first one is where...
Create a very simple temperature converter form having two text fields. The first one is where the user will enter temperature. The second field is where the computed temperature value is displayed depending on the unit (F or C).use html and javascript
Using the sample.sql script, create the sample database in MySQL. Submit the MySQL interactive screen that...
Using the sample.sql script, create the sample database in MySQL. Submit the MySQL interactive screen that results. create database sample; use sample; create table customer (custno int auto_increment primary key, firstname varchar(20), middle varchar(20), lastname varchar(20), address varchar(60), telnum1 varchar(10), telnum2 varchar(10), telnum3 varchar(10), pin varchar(6), email varchar(30)); create table accttype (id int primary key, type varchar(10)); insert into accttype (id, type) values (1,'check'); insert into accttype (id, type) values (2,'save'); insert into accttype (id, type) values (3,'cd'); insert into...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT