Thursday 19 January 2017

PHP & SQL - Basic sending of info to database

In order to become familiar with PHP's syntax and sending data to the SQL database I created a simple test that allows the user to input their first and last name. Once submitted it sends that information to the database, creating a new place for them in the database array.

Here is the test on my website:
http://clymsmith.co.uk/Clym_Test/

This is comprised of two pages;
index.php
welcome.php


Inside the HTML of index.php, I just use a basic form, which once submitted takes the user to another page that lists all the current users. I also use some of the PHP code from the next page, to print to the HTML page all the current names in the database - just as an extra personal feature.

<form action="welcome.php" method="post">
First Name: <input type="text" name="f_name"><br>
Second Name: <input type="text" name="s_name"><br>
<input type="submit">


The code for welcome.php:

First of all inside the php tag, I set the variables up for; server, username, pass and database name. Because the PHP file will be executed on the host server, I use 'localhost'. I use the f_name and s_name from the form on the previous page, saving it to variables that are used later to send to the database.

<?php
$servername = "localhost";
$username = "*******";
$password = "*******";
$dbname = "*******";
$F_name = $_POST['f_name']; 

$S_name = $_POST['s_name'];

Next up, I create a new variable called $conn which creates a new mysqli connection, and it passses through the servername, user, pass and dbname variables. Following that I used a if statement to check if there is a connect error, and if so to kill the connection script. For this I use the object-oriented way ($conn->connect_error) vs procedural (conn_connect_error (void)).

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);



Next, it takes the info stored in the $F_name and $S_name variable, and stores it in the $sql variable.

$sql = "INSERT INTO  (*dbname*) (f_name, s_name)

VALUES ('$F_name', '$S_name')";

Next up is the $sql2 variable, in this I use the SELECT statement to grab the id, f_name, s_name from the database (I replaced the actual database name with '(*dbname*)' instead for privacy - just so you get an idea). I also create a $result variable which stores the connection result and uses the $sql data.

$sql2 = "SELECT id, f_name, s_name FROM (*dbname*)";
$result = $conn->query($sql);

Next I use a if statement to check if the resulting number of rows is greater than 0. If it is, then I use a while loop, to create the HTML that will display the new content (using first name and second name). To finish it off I close the connection.

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br>First Name: ". $row["f_name"]. "<br> "."Second Name: " . $row["s_name"] . "<br>";
     }
} else {
     echo "0 results";
}

$conn->close();

?>  



No comments:

Post a Comment