Question
create a flow chart Open the course index.php file in Notepad.exe Write PHP to connect to the MySQL database service Copy the following PHP syntax.
create a flow chart
Open the course index.php file in Notepad.exe
Write PHP to connect to the MySQL database service
Copy the following PHP syntax.
Paste it under the php variable code you just entered/updated in Appendix E.
(This following code is used to allow index.php to connect to your database.
// Connect to database.
try {
$conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
//$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e){
die(var_dump($e));
}
Copy and paste the following PHP syntax below the database connection code you just inserted into index.php.
(This code will to insert a new record into the tblCSA3315C table using the information that a user types into the web page form. Notice the SQL code below that follows $sql_insert= is the same as what you might use in the phpMyAdmin SQL window).
//Code for inserting registration information into the tblCSA3315C table in the database
if(!empty($_POST)) {
try {
//retrieve data sent from the web page form fields and place into php variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
// Insert data into the tblCDA3315C table
$sql_insert = "INSERT INTO tblCDA3315C(FIRST_NAME, LAST_NAME, TELEPHONE)
VALUES (?,?,?)";
$stmt = $conn->prepare($sql_insert);
$stmt->bindValue(1, $first_name);
$stmt->bindValue(2, $last_name);
$stmt->bindValue(3, $telephone);
$stmt->execute();
}
catch(Exception $e) {
die(var_dump($e));
}
echo "
You're Registered!
";}
Copy and paste the following PHP syntax under the query code you just pasted into index.php.
(This code will send a query to retrieve data from the database service. The database will return the results to the app service. The app service will then stream back the results of the query and place the results into the web page).
//Finally, following the code above, add code for retrieving data from the database.
$sql_select = "SELECT * FROM tblCDA3315C";
$stmt = $conn->query($sql_select);
$registrants = $stmt->fetchAll();
if(count($registrants) > 0) {
echo "
People Who Are Registered:
";echo "
FIRST_NAME | ";LAST_NAME | ";TELEPHONE |
---|---|---|
".$registrant['FIRST_NAME']." | ";".$registrant['LAST_NAME']." | ";".$registrant['TELEPHONE']." |
} else {
echo "
No one is currently registered.
";}
Preview the sample code in the Google Chrome browser
At this point, you may expperience several errors since the browser is trying to connect to the database service
This is normal and for now, try to ignore the errors
Done
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started