I need to create a database where a user enters a first and last name and then that gets recorded into a table that can be sorted alphabetically.
I have what I think is the makings of a PHP table here:
<?php
$con=mysqli_connect("mysql6.000webhost.com","owen","*********","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql = "CREATE TABLE Persons (PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID), FirstName CHAR(15), LastName CHAR(15))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
Next I have the actual html code where the user submits his or her name:
<!DOCTYPE HTML>
<html>
<head>
<link rel="shortcut icon" href="http://2.bp.blogspot.com/-W6qoXhCjKSs/Tpul-XAR3-I/AAAAAAAACeU/Oe1ZEzpnUCg/s1600/sbstweb.gif">
<link rel="stylesheet" type="text/css" href="../style.css">
<title>Almost Done!</title>
</head>
<body>
<img class= "logo" src= "http://2.bp.blogspot.com/-W6qoXhCjKSs/Tpul-XAR3-I/AAAAAAAACeU/Oe1ZEzpnUCg/s1600/sbstweb.gif">
<h1>Enter your name below to complete the quiz</h1>
<form action="insert.php" method="post">
<p><input autofocus name="firstname" placeholder="First Name" type="text"></p>
<p><input autofocus name="lastname" placeholder="Last Name" type="text"></p>
<input type="submit">
</form>
</body>
</html>
And lastly I have what I think is PHP code that inserts the new entries into the database:
<?php
$con=mysqli_connect("mysql6.000webhost.com","a3159217_owen","*********","a3159217_student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName)
VALUES
('$_POST[firstname]','$_POST[lastname]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
My first question is how to get the PHP code to implement and work. When I hit submit in the webpage, it directs me to the insert.php code that I made. How do I get it to submit to the database, and how do I create the database in the first place.
Second, to actually show the table, do I have to make another html page that renders the PHP table? How does this work?