<?php require_once("PasswordMstr.php"); ?>

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Add A Record</title>

<link rel="stylesheet" href="php_styles.css" type="text/css" />

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

</head>

<body>

<h3>Add A Record to Address Book</h3>

<?php

if (empty($_GET['in_name']))

                exit("<p>You must enter at least the name. Click back.</p>");

 

// $DBConnect = @mysqli_connect("localhost", "root")                                                                                 // (Test)  Connect to MySQL                                

$DBConnect = mysqli_connect("db1234.abcdefg.net", "db1234567890", "abcdefgh", "db1234567890")   // Connect to MySQL

                Or die("<p>Unable to connect to data base server.</p>"

                . "<p>Error code " . mysqli_connect_errno()

                . "; " . mysqli_connect_error()) . "</p>";

 

// $DBName = "AddressBook";                                                                                                                                                                  // (Test)               

$DBName = "db240712012";                                                                                                      

if ( ! mysqli_select_db($DBConnect, $DBName))

{

                $SQLstring = "CREATE DATABASE $DBName";

                $QueryResult = @mysqli_query($DBConnect, $SQLstring)            // 1. Create data base

                                Or die("<p>Unable to Create Data Base.</p>"

                                . "<p>Error code " . mysqli_errno($DBConnect)

                                . "; " . mysqli_error($DBConnect)) . "</p>";

                echo "<p>Successfully created the database.</p>";       

                @mysqli_Select_db($DBConnect, $DBName);                                                                   // 1.2. Select data base

}

 

$TableName = "persons";                                                                                                                                                            // 2. Create Table

$SQLstring = "SELECT * FROM $TableName";

$QueryResult = @mysqli_query($DBConnect, $SQLstring);

if (! $QueryResult)

{

                $SQLstring = "CREATE TABLE persons (personID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,

                                name VARCHAR(60), email VARCHAR(80), phone VARCHAR(80), address VARCHAR(120))";

                $QueryResult = @mysqli_query($DBConnect, $SQLstring)           

                                Or die("<p>Unable to create the person table 1.</p>"

                                . "<p>Error code " . mysqli_errno($DBConnect)

                                . "; " . mysqli_error($DBConnect)) . "</p>";

                echo "<p>Successfully created the persons table.</p>";              

}

                                                                                                                                                                                                                                                // 3. Add Record

$Name = $_GET['in_name']; $Email = $_GET['in_email']; $Phone = $_GET['in_phone']; $Address = $_GET['in_address'];               

$SQLstring = "INSERT INTO persons VALUES (NULL, '$Name', '$Email', '$Phone', '$Address')";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)

                Or die("<p>Unable to add a record to persons table.</p>"

                                . "<p>Error code " . mysqli_errno($DBConnect)

                                . "; " . mysqli_error($DBConnect)) . "</p>";

$PersonID = @mysqli_insert_id($DBConnect);                   // Retrieve the assigned Primary Key

               

mysqli_close($DBConnect);

 

echo "<p><strong>$Name's</strong> addrss info was added to Address Book! with ID of <strong>$PersonID</strong>.</p>";

 

?>

<p><a href="indexMstr.php">Return to Menu Page</a></p>

</body>

</html>