First, we need to be installed WAMP or XAMPP like setup in our PC.

Now access http://localhost/phpmyadmin using dbusername user/pass.

Now Create a new test Database, Example: “mysite” is out test database.

Now we will create a new table name “myguest” inside the “mysite” database using PHP script.

PHP script for creating “myguest” table:

<?php
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "mysite";

echo "Welcome to My Site...<br><br>";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!!<br><br>";

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

//Close connections
$conn->close();

echo "Connection Close.<br><br>";
?>

Save the Above PHP code in a .php file and hit the page in your browser while wamp server is running.
It will create a new Table named “myguest”.

To see the new table go to http://localhost/phpmyadmin and browse the database named “mysite”.

Now we need to insert data into the table “myguest” using another PHP script.

PHP script for inserting data into the “myguest” table:

<?php
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "mysite";

echo "Welcome to My Site...<br><br>";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully!!<br><br>";


$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Denial', 'Cooper', 'jcooper@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Robert', 'Brown', 'rbrown@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Alex', 'Brown', 'abrown@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Reo', 'Cooper', 'rcooper@example.com')";

if ($conn->multi_query($sql) === TRUE) {
 echo "New records created successfully";
} else {
 echo "Error: " . $sql . "<br>" . $conn->error;
}

//Close connections
$conn->close();
?>

Copy and paste the above PHP code into another .php file and hit the .php file into the browser.
It will insert 6 records into the “myguest” table.

Now we will show the data of the table which we have inserted in the previous step.

PHP code for printing table data:

<?php
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "mysite";

echo "Welcome to My Site...<br><br>";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully!!<br><br>";


$sql = "SELECT * FROM myguests";
$result = $conn->query($sql);
while($row = $result->fetch_object())
 {
 echo "<pre>";
 print_r($row);
 }

//Close connections
$conn->close();
?>

That’s all for today!!

Next, we will learn how to insert data from a Form submission.

Advertisements