<!DOCTYPE html> <html lang="en"> <head> <title>Store Data</title> </head> <body> <center> <h1>Storing Form data in Database</h1> <form action="insert.php" method="post"> <p> <label for="firstName">First Name:</label> <input type="text" name="first_name" id="firstName"> </p> <p> <label for="lastName">Last Name:</label> <input type="text" name="last_name" id="lastName"> </p> <p> <label for="Gender">Gender:</label> <input type="text" name="gender" id="Gender"> </p> <p> <label for="Address">Address:</label> <input type="text" name="address" id="Address"> </p> <p> <label for="emailAddress">Email Address:</label> <input type="text" name="email" id="emailAddress"> </p> <input type="submit" value="Submit"> </form> </center> </body> </html> **************************************************************** <?php $first_name = $_REQUEST['first_name']; $last_name = $_REQUEST['last_name']; $gender = $_REQUEST['gender']; $address = $_REQUEST['address']; $email = $_REQUEST['email']; $conn = mysqli_connect("localhost", "root", "", "staff"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "INSERT INTO college VALUES('$first_name','$last_name','$gender','$address','$email')";
if(mysqli_query($conn, $sql)) { echo "<h3>data stored in a database successfully.</h3>"; echo nl2br("n$first_namen $last_namen $gendern $addressn $email"); } else { echo "ERROR:". mysqli_error($conn); } mysqli_close($conn); ?> ***************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "Name: " . $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> ************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student WHERE last_name='Dutt'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc())
{ echo "Name: " . $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> **************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student ORDER BY first_name"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "Name: " . $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> **************************************************************************************** The die() function is used to print the message. The exit() method exits the script or it may be used to print alternate messages. The mysqli_connect() function attempts to open a connection to the MySQL Server running on host which can be either a host name or an IP address. PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. The mysqli_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.

The HyperText Markup Language or HTML is the standard markup language

  • 1.
    <!DOCTYPE html> <html lang="en"> <head> <title>StoreData</title> </head> <body> <center> <h1>Storing Form data in Database</h1> <form action="insert.php" method="post"> <p> <label for="firstName">First Name:</label> <input type="text" name="first_name" id="firstName"> </p> <p> <label for="lastName">Last Name:</label> <input type="text" name="last_name" id="lastName"> </p> <p> <label for="Gender">Gender:</label> <input type="text" name="gender" id="Gender"> </p> <p> <label for="Address">Address:</label> <input type="text" name="address" id="Address"> </p> <p> <label for="emailAddress">Email Address:</label> <input type="text" name="email" id="emailAddress"> </p> <input type="submit" value="Submit"> </form> </center> </body> </html> **************************************************************** <?php $first_name = $_REQUEST['first_name']; $last_name = $_REQUEST['last_name']; $gender = $_REQUEST['gender']; $address = $_REQUEST['address']; $email = $_REQUEST['email']; $conn = mysqli_connect("localhost", "root", "", "staff"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "INSERT INTO college VALUES('$first_name','$last_name','$gender','$address','$email')";
  • 2.
    if(mysqli_query($conn, $sql)) { echo "<h3>datastored in a database successfully.</h3>"; echo nl2br("n$first_namen $last_namen $gendern $addressn $email"); } else { echo "ERROR:". mysqli_error($conn); } mysqli_close($conn); ?> ***************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "Name: " . $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> ************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student WHERE last_name='Dutt'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc())
  • 3.
    { echo "Name: ". $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> **************************************************************************************** <?php $conn = mysqli_connect("localhost", "root", "", "student_db"); if($conn === false) { die("ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT first_name, last_name FROM student ORDER BY first_name"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "Name: " . $row["first_name"] . " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> **************************************************************************************** The die() function is used to print the message. The exit() method exits the script or it may be used to print alternate messages. The mysqli_connect() function attempts to open a connection to the MySQL Server running on host which can be either a host name or an IP address. PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. The mysqli_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.