Online gallery database

In previous post, message sent by visitor does not get stored in database. To use database we need PHP to get the data, connect to database and save the data there in database. Database that we are going to use is MySQL. Easiest and quickest way too use PHP and MySQL is to install XAMPP.

After install your XAMPP copy the gallery folder, into server htdoc folder. Now type localhost/gallery/home.html URL in your browser. The browser should be able to open the gallery home page. Make sure you have started Apache and MySQL from XAMPP control panel.

Before saving the message from visitor into database, we have to create a database first. To do that, go to phpMyAdmin page and create a database called gallery.

After the database is created, create a table called messageTable. Below is the entity diagram that represent the table.

Next, create a PHP file in the gallery folder. Open notepad and type the following code. Save the file as saveMessaageToDatabase.php and select All Files as file type.


<?php

$conn = mysqli_connect("localhost","root","");

if($conn){
echo "connected";
}else{
echo "Error" . $conn->connect_error;
die();
}

mysqli_select_db($conn, 'gallery');

$subject = $_POST['subject'];
$message =  $_POST['messaage'];

$sql = "INSERT  INTO messageTable (subject, message)". 
"VALUES ('$subject', '$message')";

$retVal = mysqli_query($conn, $sql);

if($retVal){
echo "message is stored succesfully";
}else{
echo "Error : ".$conn->error;
}
mysqli_close();
?>

We need to pass the data from contact form to this PHP file. To do that, open contact.html, add following code in in existing form tag.

<form name = "contactForm" action = "saveMessageToDatabase.php">


Now, every time visitor enter information and submit the form, the data will go to this PHP and messages will be saved in your database. To check if the messages are saved, go to phpMyAdmin page and refresh the table. New messages should be entered to you database.

As exercise, include field for visitor name and email address. We should also include date and time the message was sent to you through your website.

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net