Design for Digital Media Environments-Understanding PHP-Connecting to a Database

For the last couple of week in the workshops we have been covering how to connect to our own database running on myphpadmin. I have decided to look more into this in my own time: the code I have created is this and I will run through what it all means:

<?php — Opening PHP file
if(isset($_POST[“post”])) {  this is saying if a submit button  with the name post run the following code.

$name = ($_POST[“name”]);   This is saying what ever is in the input part of the form with the name “name” then put that information into the database table with the name “name”
$post = ($_POST[“post”]); This is saying what ever is in the input part of the form with the name “post” then put that information into the database table with the name “post”

if(empty($name)) { This is just if submit is hit out there is no data inside the form if that is true then it will display the following message on the site
$message = “Invalid Name”;
} else if(empty($post)) {
$message = “Invalid Post”;
} else {   This is else statement for the above code so if the form was filled out it will run the below code
$query = “INSERT INTO post (name, post) VALUES (‘{$name}’, ‘{$post}’)”;  This is the code that puts the data into the database table “post”

$result = mysqli_query($connection, $query);

if($result) {
$message = “Success, your post was added”;
} else {
$message = “Sorry, something went wrong”;
}

echo $message;

}

}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s