PHP – Midterm

addComic.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Add Comic</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Add a Comic - PHP Midterm</h2>
<form action="addComic.php" method="post">
<fieldset>
<legend>Add to database</legend>
<label>Title
<input type="text" name="title" />
</label>
<label>State
<select name="state">
<option value="mint">Mint</option>
<option value="near_mint">Near Mint</option>
<option value="Used">Used</option>
</select>
</label>
<label>Company
<select name="company">
<option value="Marvel">Marvel</option>
<option value="DC">DC</option>
</select>
</label>
<label>Price
<input type="text" name="price" />
</label>
<input type="submit" value="submit" name="submit"/>
<input type="reset" value="reset" name="reset"/>
</fieldset>
</form>
Return to the display comics page <a href="displayComic.php">here</a>
</body>
</html>
addComic.php
<!DOCTYPE html>
<html>
<head>
<title>Add Comic - PHP Midterm</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Welcome to Keith's Comic Collection</h2>
<?php
//add comic to the database utilizing the form
if(isset($_POST['submit'])){
require_once('connectvars.php');
$title=$_POST['title'];
$state=$_POST['state'];
$company=$_POST['company'];
$price=$_POST['price'];
$dbc=mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
$query="INSERT INTO comic VALUES('0','$title','$state','$company','$price')";
mysqli_query($dbc,$query);
}
else{
echo '<br/> You have visited the wrong page. Please click <a href="addcomic.html">here</a>'.mysqli_error($dbc);
}
mysqli_close($dbc);
?>
<p>To return to display comic click <a href="displayComic.php">here</a></p>
<p>To add another comic click <a href="removeComic.php">here</a></p>
</body>
</html>
connectvars.php
<?php
//insert defined constants here
define('DB_USERNAME','w0664731');
define('DB_PASSWORD','grr7grr7');
define('DB_HOST','127.0.0.1');
define('DB_NAME','w0664731db');
?>
createComicTable.php
<!DOCTYPE html>
<html>
<head>
<title>Create Comic Table - PHP Midterm</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Attempting to create the comic table...</h2>
<?php
//insert create table script here
require_once('connectvars.php');
$dbc=mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
$query="CREATE TABLE comic (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,title VARCHAR(64),state VARCHAR(20), company VARCHAR(20),price FLOAT(7,2))";
if(mysqli_query($dbc,$query)){
echo 'table created';
}
else{
echo 'table not created'.mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</body>
</html>
displayComic.php
<!DOCTYPE html>
<html>
<head>
<title>Display Comic Collection - PHP Midterm</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Displaying Kevin's Comic Collection</h2>
<nav>
<a href="addComic.html">Add a Comic</a>
<a href="removeComic.html">Remove a Comic</a>
<a href="createComicTable.php">Create Table</a>
</nav>
<br/>
<section>
<table>
<?php
//enter your PHP code here:
$dbc=mysqli_connect('127.0.0.1','w0664731','grr7grr7','w0664731db');
$query="SELECT * FROM comic";
$result=mysqli_query($dbc,$query);
echo '<tr><th>ID</th><th>Title</th><th>State</th><th>Company</th><th>Price</th></tr>';
while($row=mysqli_fetch_array($result)){
echo '<tr>';
echo "<td> " . $row['id'] . "</td>" .
"<td>" . $row['title'] . "</td>" .
"<td>" . $row['state'] . "</td>" .
"<td>" . $row['company'] . "</td>" .
"<td>" . $row['price'] . "</td>";
echo '</tr>';
}
mysqli_close($dbc);
?>
</table>
</section>
</body>
</html>
removeComic.php
<!DOCTYPE html>
<html>
<head>
<title>Remove Comic - PHP Midterm</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Welcome to Keith's Comic Collection</h2>
<?php
// remove comic from database by ID
if(isset($_POST['submit'])){
require_once('connectvars.php');
$id=$_POST['id'];
$dbc=mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
$query="DELETE FROM comic WHERE id=$id)";
if(mysqli_query($dbc,$query)){
echo '<br/>The following query executed successfully:<br/>'.$query;
}
}
else{
echo '<br/> You have visited the wrong page. Please click <a href="addcomic.html">here</a>'.mysqli_error($dbc);
}
mysqli_close($dbc);
?>
<p>To return to display comic click <a href="displayComic.php">here</a></p>
<p>To remove another comic click <a href="removeComic.html">here</a></p>
</body>
</html>
removeComic.html/strong>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Remove comic - PHP Midterm</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Remove a comic from Keith's collection</h2>
<form action="removeComic.php" method="post">
<fieldset>
<legend>Remove from database</legend>
<label>Remove ID:
<input type="number" name="id" />
</label>
<p>
<input type="submit" value="submit" name="submit"/>
<input type="reset" value="reset" name="reset"/>
</p>
</fieldset>
</form>
<p style="text-align:center;">Return to the display comics page <a href="displayComic.php">here</a></p>
</body>
</html>
main.css
body{
background-color: black;
color:white;
}
h2, nav{
text-align:center;
}
table{
background-color: red;
border-collapse: collapse;
margin:0 auto;
}
tr, td, th {
border: 1px solid white;
padding:5px;
}
nav a{
border:1px solid white;
border-radius: 5px ;
text-decoration:none;
margin: 5px 5px 2px 0;
padding:3px;
background-color:rgb(250,0,0);
color:white;
}
nav a:hover{
background-color:rgb(150,0,0);
}
a{
color:red;
text-decoration: none;
}
fieldset{
width:400px;
}
legend{
font-weight: bold;
font-size: 13pt;
}
select, input[type="text"]{
width:120px;
position: absolute;
left:90px;
}
input[type="number"]{
width:25px;
left:95px;
}
label{
display:block;
position:relative;
margin: 4px 0;
}
h2 {
color:red;
text-shadow: -1px -1px white,
-1px -1px white,
-1px -1px white,
-1px -1px white;
}
section{
background-color: navy;
padding:10px 15px;
width:60%;
margin: 0 auto;
border: 1px solid white;
border-radius: 10px;
}
fieldset, p{
margin: 0 auto;
}
fieldset p {
text-align:center;
}
Leave a reply