Database Management Using PHP
Database Management Using PHP
In this example , we are going to store data in database which is submitted through
HTML form.
Requirements:
XAMPP Server (Web Server)
HTML
PHP
MySQL
HTML form: First we create an HTML form that need to take user input from
keyboard.
HTML form is a document which stores information of a user on a web server using
interactive controls.
An HTML form contains different kind of information such as username, password,
contact number, email id etc.
The elements that are used in an HTML form are check box, input box, radio buttons,
submit buttons etc.
With the help of these elements, the information of an user is submitted on the web
server.
The form tag is used to create an HTML form.
Syntax:
<form> Form Elements... </form>
or
To pass the values to next page, we use the page name with the following syntax. We
can use either GET or POST method to sent data to server.
<form action=other_page.php method= POST/GET>
Form Elements...
</form>
Example
<?php
$host = 'localhost';
$user = 'root';
$password = “”';
$dbname = 'origincorp';
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$query = "SELECT email FROM emailtbl WHERE email = '$email'";
$result = $mysqli->query($query);
if ($result->num_rows > 0)
{
echo 'Email already exists in the mailing list.';
}
Else
{
$query = "INSERT INTO emailtbl (email) VALUES ('$email')";
if ($mysqli->query($query) === TRUE)
{
echo 'Email added to the mailing list.';
}
Else
{
echo 'Error adding email to the mailing list: ' . $mysqli->error;
}
}
}
$query = "SELECT email FROM emailtbl";
$result = $mysqli->query($query);
if ($result->num_rows > 0)
{
echo '<h2>Mailing List:</h2>';
while($row = $result->fetch_assoc())
{
echo $row['email'] . '<br>';
}
}
else
{
echo 'The mailing list is empty.';
}
$mysqli->close();
?>
<form method="post">
<label for="email">Email Address:</label>
<input type="email" name="email" required>
<button type="submit" name="submit">Submit</button>
</form
$_POST: This is a superglobal array in PHP that holds values sent to the server via the
HTTP POST method. When a form is submitted using the POST method, the form
data is stored in this array.
['submit']: This is an array key, referring to a specific input element named 'submit' in
the HTML form. The name attribute of an HTML form element corresponds to keys
in the $_POST array.
isset(): This is a PHP function that checks if a variable is set and is not null. It returns
true if the variable is set and false otherwise.
So, isset($_POST['submit']) checks whether the 'submit' button or input field in the
HTML form has been clicked or submitted. If it has been submitted, this expression
will evaluate to true; otherwise, it will evaluate to false. This check is commonly used
in PHP scripts that process form submissions to determine if the form has been
submitted before processing the form data.
3. $email = $_POST['email'];
This line is used to retrieve data submitted through an HTML form using the
POST method from the input type by name = email.
This query is used to retrieve data from a database table named emailtbl.
5. $result = $mysqli->query($query);
This line executes a SQL query against a MySQL database using the mysqli
extension.
$result: This is a variable that will hold the result of the query execution.
After the query is executed, this variable will contain an object representing
the result set returned by the database, or it may contain FALSE if the query
encountered an error.
It takes a single argument, which is the SQL query string you want to execute
(in this case, the query stored in the $query variable).
executes the SQL query stored in the $query variable against the MySQL
database connected by the $mysqli object. The result of the query execution is
then stored in the $result variable for further processing.
It's important to note that $result may contain different types of data depending
on the type of query executed.
For example, if the query is a SELECT statement, $result will contain a result
set that can be iterated over to fetch rows of data.
6. $row = $result->fetch_assoc()
This part fetches the next row of the result set returned by the mysqli query
execution.
The fetch_assoc() method is used to fetch a single row of data as an associative
array, where the keys of the array correspond to the column names in the result
set.
Each iteration of the loop fetches the next row of data from the result set and
stores it in the $row variable.
7. Then using the echo statement the values of the fetched data is displayed on the
screen.
Other examples
PROGRAM
<?php
VALUES
('aysha','aysha@gmail.com',9884713895,'HR',200000),
('izyan','izyan@gmail.com',9887613678,'Finance',300000),
('shazana','shazana@gmail.com',8879657885,'Advertising',250000),
('yasmeen','yasmeen@gmail.com',8879657885,'Advertising',150000)");
if ($result->num_rows > 0) {
echo '<br>';
else
$mysqli->close();
Output
Example
PROGRAM
<?php
// initialize variables
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
else
?>
<form method="post">
<label for="name">Name:</label>
<br><br>
<br><br>
<label for="password">Password:</label>
<br><br>
</form>
Explanation
1. $name = trim($_POST['name']);
is a line of code used to retrieve data submitted through an HTML form using
the POST method and remove any leading or trailing whitespace characters
from the input.
!filter_var($email, FILTER_VALIDATE_EMAIL):
In this case, it is used to validate the format of the email address stored in the
variable $email.
!: This is the logical NOT operator in PHP. It negates the result of the
condition.
<input type="text">: This part creates an input field where users can input text.
It's one of the types of input fields available in HTML forms.
name="name": This attribute specifies the name of the input field. When the
form is submitted, the value entered into this field will be sent to the server
with the name "name". This name is crucial for identifying the input data on
the server-side.
It uses PHP to echo out the value stored in the $name variable.
If this attribute is present, the browser will not allow the form to be submitted
unless the input field has a value.