Hi I am working through a book to get a better grasp of using php in web development. Leading up to the issue I am having I learned the very basics of creating a database and table in that database through mysql in the linux prompt and all went well there. From there I transfered the database to phpmyadmin on my localhost lampp.
Currently I am working through an example that is a small single page php program inside html that is suppose to output the data in the database I created earlier, onto a webpage. In addition the the data it is suppose to output there is suppose to be a form at the top of the page where you can add new entries and delete them.
I was able to work through a previous example that outputs the same database data to a webpage and it outputs without issue. When I load the current example that has a form though, nothing shows on the page. All I get from chrome is “localhost is currently unable to handle this request”. I have tirelessly checked for syntax errors and am at a loss for why nothing loads. Any help would be greatly appreciated.
This is the code in the index.php. I understand it ins’t secure code but i’m only using it for learning.
<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) {
die("Fatal Error"); }
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post($conn, 'isbn');
$query = "DELETE FROM classics WHERE isbn='$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed<br><br>";
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post($conn, 'author');
$title = get_post($conn, 'title');
$category = get_post($conn, 'category');
$year = get_post($conn, 'year');
$isbn = get_post($conn, 'isbn');
$query = "INSERT_INTO classics VALUES" .
"('$author', '$title', '$category', '$year', '$isbn')";
$result = $conn->query($query);
if (!$result) {
echo "INSERT failed<br><br>";
}
}
echo <<<_END
<form action="index.php" method="post"<pre>
Author <input type="text" name="author">
Title <input type="text" name="title">
Category <input type="text" name="category">
Year <input type="text" name="year">
ISBN <input type="text" name="isbn">
<input type="submit" value="ADD RECORD">
</pre></form>
_END;
$query = "SELECT * FROM classics";
$result = $conn->query($query);
if (!$result) {
die("Database access failed");
}
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = $result->fetch_array(MYSQLI_NUM);
$r0 = htmlspecialchars($row[0]);
$r1 = htmlspecialchars($row[1]);
$r2 = htmlspecialchars($row[2]);
$r3 = htmlspecialchars($row[3]);
$r4 = htmlspecialchars($row[4]);
echo <<<_END
<pre>
Author $r0
Title $r1
Category $r2
Year $r3
ISBN $r4
</pre>
<form action='index.php' method='post'>
<input type='hidden' name='delete' value='yes'>
<input type='hidden' name='isbn' value='$r4'>
<input type='submit' value='DELETE RECORD'></form>
_END;
}
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>
my login.php is:
<?php // login.php
$hn = 'localhost';
$db = 'publications';
$un = 'root';
$pw = '';
?>
* Admin added code tags