please provide a line by line explanation for the following php file in the cont
ID: 3838832 • Letter: P
Question
please provide a line by line explanation for the following php file in the context of the FHC implementation. Describe how the file is invoked and the input to and output generated by the file.
RemoveComp.php
<?php
/* Program: RemoveComp.php
* Desc: Removes record from the database.
*/
$host = "localhost";
$user = "root";
$password = "shawnee";
$dbname = "FHC";
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die ("couldn't connect to server");
?>
<html>
<head><title>Remove Record</title></head>
<body>
<?php
$query = "DELETE FROM Computer WHERE compID = '{$_POST['compID']}'";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query");
?>
<p>Your record has been deleted<p>
<p><a href="FHCHome.php">FHC Home</a></p>
</body></html>
Explanation / Answer
I order to run a php script you need a web server, mysql and php installed on your system. To ease the process you can install an all in one package called Xampp. Just download xampp installer and install all the services.
After installation open xampp control panel and start apache and mysql server by clicking on the respective start buttons. (If any of the services are already running then no need to start again )
After the installation, if you are using windows, php home directory will be "c:/xampp/htdocs". All your php files should be inside this directory or sub directories.
put your php file in htdocs folder and open browser and type localhost/filename.php
2. Line by line explaination of php code in your file
<?php
/* Program: RemoveComp.php
* Desc: Removes record from the database.
*/
// U shd call this file from a file containing a form which takes compid from the user and sends that id to this file // via html form for deletion
// This file connects to mysql database and deletes the record which it gets through post request.
$host = "localhost"; // mysql server is running on local machine otherwise use ip address of tht machine
$user = "root"; // user to login to database
$password = "shawnee"; // pasword to be used to conect to the database using user root
$dbname = "FHC"; //name of the database to connect
/*mysqli_connect() function takes the host, username, pwd and database name to connect to the database
so the below two line connects to database "FHC" using user "root" and password "shawnee" . If the connection fails then die i:e stop execution of rest of the code
*/
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die ("couldn't connect to server");
?>
<html>
<head><title>Remove Record</title></head>
<body>
<?php
// Prepare the query
$query = "DELETE FROM Computer WHERE compID = '{$_POST['compID']}'";
// Execute the query and return the result in result set. If the query fails then die
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query");
?>
<p>Your record has been deleted<p>
<p><a href="FHCHome.php">FHC Home</a></p>
</body></html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.