0% found this document useful (0 votes)
126 views24 pages

7 MySQLL

The document contains PHP code for connecting to a database, creating a database and table, inserting data into the table, and retrieving data from the table. It connects to a MySQL database hosted on 000webhost.com and creates a database called "store". It then creates a table called "users" with columns for ID, first name, last name, email, and password. Data is inserted into the table and retrieved for display. The code is contained in multiple HTML pages with Bootstrap styling for formatting.

Uploaded by

Khuong Lam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views24 pages

7 MySQLL

The document contains PHP code for connecting to a database, creating a database and table, inserting data into the table, and retrieving data from the table. It connects to a MySQL database hosted on 000webhost.com and creates a database called "store". It then creates a table called "users" with columns for ID, first name, last name, email, and password. Data is inserted into the table and retrieved for display. The code is contained in multiple HTML pages with Bootstrap styling for formatting.

Uploaded by

Khuong Lam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>introduction</title>

<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Connect to database:</h1>
<?php
//mysqli_connect(host, username, password, dbname)
//$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
//var_dump($link);
//
//
//echo "<p>Connected successfully to the database.</p>";

//connect
$link = new mysqli("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db"); //
your host should be local if you are using my free web
hosting
//check connection

if($link->connect_errno > 0){


die("Unable to connect: " . $link->connect_error);
}
echo "<p>Connected successfully to the database.</p>";
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Create database</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Create database:</h1>
<h3>Connect to server</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1") or die("ERROR: Unable
to connect: " . mysqli_connect_error());
var_dump($link);

echo "<p>Connected successfully to the server.</p>";


?>
<h3>Create a database</h3>
<?php
$sql = "CREATE DATABASE store";
if(mysqli_query($link, $sql)){
echo "<p>Database store was created successfully</p>";
}else{
echo "ERROR: Unable to execute $sql" .
mysqli_error($link);
}
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Add a table to database</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Add a table to database:</h1>
<h3>Connect to database</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Create table</h3>
<?php
$sql = "CREATE TABLE users(ID INT(4) NOT NULL PRIMARY KEY
AUTO_INCREMENT, firstname CHAR(20) NOT NULL, lastname
CHAR(20) NOT NULL, email VARCHAR(30), password
VARCHAR(40))";
if(mysqli_query($link, $sql)){
echo "<p>Table users created successfuly!</p>";
}else{
echo "ERROR: Unable to execute $sql" .
mysql_error($link);
}
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Populate table</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Add a table to database:</h1>
<h3>Connect to database</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Populate table</h3>
<?php
$sql = "INSERT INTO users (firstname, lastname, email,
password) VALUES ('Mark', 'Zuckerberg',
'[email protected]', 'password')";
if(mysqli_query($link, $sql)){
echo "<p>New row added successfully!</p>";
}else{
echo "ERROR: Unable to execute $sql" .
mysql_error($link);
}
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Populate table using form</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Populate table using form:</h1>
<h3>Connect to database:</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Send data to database:</h3>
<?php
//get user inputs
$id = $_POST["ID"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$password = $_POST["password"];
//error messages
$missingFirstname = "<p><strong>Please enter your
firstname!</strong></p>";
$missingLastname = "<p><strong>Please enter your
lastname!</strong></p>";
$missingemail = "<p><strong>Please enter your
email!</strong></p>";
$invalidemail = "<p><strong>Please enter a valid email
address!</strong></p>";
$missingPassword = "<p><strong>Please enter a
password!</strong></p>";

if($_POST["submit"]){
//check for errors
if(!$firstname){

$errors .= $missingFirstname;
}else{
$firstname = filter_var($firstname,
FILTER_SANITIZE_STRING);
}
if(!$lastname){
$errors .= $missingLastname;
}else{
$lastname = filter_var($lastname,
FILTER_SANITIZE_STRING);
}
if(!$email){
$errors .= $missingemail;
}else{
$email = filter_var($email,
FILTER_SANITIZE_EMAIL);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors .= $invalidemail;
}
}
if(!$password){
$errors .= $missingPassword;
}
if($errors){
$resultMessage = '<div class="alert alertdanger">' . $errors .'</div>';
echo $resultMessage;
}else{
//no errors, prepare variables for the query
$tblname = "users";
$firstname = mysqli_real_escape_string($link,
$firstname);
$lastname = mysqli_real_escape_string($link,
$lastname);
$email = mysqli_real_escape_string($link, $email);
$password = mysqli_real_escape_string($link,
$password);
$password = md5($password);
//execute insert query
if(!$id){

$sql = "INSERT INTO users (firstname,


lastname, email, password) VALUES ('$firstname',
'$lastname', '$email', '$password')";
}else{
$sql = "INSERT INTO users (ID, firstname,
lastname, email, password) VALUES ('$id', '$firstname',
'$lastname', '$email', '$password')";
}
if(mysqli_query($link, $sql)){
$resultMessage = '<div class="alert alertsuccess">Data added successfully to the database
table</div>';
echo $resultMessage;
}else{
$resultMessage = '<div class="alert alertwarning">ERROR: Unable to excecute: ' .$sql. '. ' .
mysqli_error($link). '</div>';
echo $resultMessage;
}
}
}
?>
<form action="5.populatetableusingform.php"
method="post">
<div class="form-group">
<label for="ID">ID:</label>
<input type="number" id="ID"
placeholder="ID" class="form-control" name="ID"
maxlength="4">
</div>
<div class="form-group">
<label
for="firstname">Firstname:</label>
<input type="text" id="firstname"
placeholder="Firstname" class="form-control"
name="firstname" maxlength="20">
</div>
<div class="form-group">
<label
for="lastname">Lastname:</label>

<input type="text" id="lastname"


placeholder="Lastname" class="form-control"
name="lastname" maxlength="20">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email"
placeholder="Email" class="form-control" name="email"
maxlength="30">
</div>
<div class="form-group">
<label
for="password">Password:</label>
<input type="password" id="password"
placeholder="Password" class="form-control"
name="password" maxlength="40">
</div>
<input type="submit" name="submit"
class="btn btn-success btn-lg" value="Send data">

</form>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Retrieve data</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Retrieve data from a table:</h1>
<h3>Connect to database:</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Retrieve data from database:</h3>
<?php
$sql = "SELECT * FROM users";
//$sql = "SELECT * FROM users WHERE firstname = 'george'";
//$sql = "SELECT * FROM users ORDER BY lastname";
//$sql = "SELECT * FROM users ORDER BY lastname DESC";
if($result = mysqli_query($link, $sql)){
print_r($result);
if(mysqli_num_rows($result)>0){
echo "<table class='table table-stripped tablehover table-condensed table-bordered'>
<tr>
<th>ID</th>
<th>firstname</th>
<th>lastname</th>
<th>email</th>
<th>password</th>
</tr>
";
//
$count = 0;
while($row = mysqli_fetch_array($result,
MYSQLI_ASSOC)){
//
$count++;
//
echo "<p>Row number: $count</p>";
//
print_r($row);
echo "<tr>";

echo "<td>"
echo "<td>"
echo "<td>"
echo "<td>"
echo "<td>"
echo "</tr>";

.
.
.
.
.

$row["ID"] . "</td>";
$row["firstname"] . "</td>";
$row["lastname"] . "</td>";
$row["email"] . "</td>";
$row["password"] . "</td>";

}
echo "</table>";
//close the result set
mysqli_free_result($result);
}else{
echo "<p>mySQL returned an empty result set.</p>";
}
}else{
echo "<p>Unable to excecute: $sql. " .
mysqli_error($link) ."</p>";
}
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Update data</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Update data in a table:</h1>
<h3>Connect to database:</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Update data:</h3>
<?php
$sql = "UPDATE users SET lastname='Wilson' WHERE
firstname='Peter' AND email='[email protected]'";
if(mysqli_query($link,$sql)){
echo "<p>Data was updated successfuly!</p>";
}else{
echo "<p>ERROR: Unable to execute $sql.".
mysqli_error($link) ." </p>";
}
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Delete data</title>
<link href="css/bootstrap.min.css"
rel="stylesheet">
<style>
h1{
color:purple;
}
h3{
color:#42d5ce;
}
.containingDiv{
border:1px solid #7c73f6;
margin-top: 100px;
border-radius: 15px;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-1 col-sm-10
containingDiv">
<h1>Delete Data:</h1>
<h3>Connect to database:</h3>
<?php
//mysqli_connect(host, username, password, dbname)
$link = @mysqli_connect("mysql11.000webhost.com",
"a7099072_user", "Hellodevelopers1", "a7099072_db") or
die("ERROR: Unable to connect: " .
mysqli_connect_error());
echo "<p>Connected successfully to the database.</p>";
?>
<h3>Delete data:</h3>
<?php
$sql = "DELETE FROM users WHERE firstname='george'";
//$sql = "DROP users";
if(mysqli_query($link,$sql)){
echo "<p>Data was deleted successfuly!</p>";
}else{
echo "<p>ERROR: Unable to execute $sql.".
mysqli_error($link) ." </p>";
}
//close connection
mysqli_close($link);
//$link->close($link);
?>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/j
query.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

You might also like