I am trying to create a simple catalog. I have a sqlConnect.php file that contains the following
<?php
$host = 'localhost';
$db = 'books';
$user = 'root';
$pass = 'password';
$con = mysqli_connect($host, $user, $pass, $db);
if ($con) {
echo 'Successfully connected to database!';
} else{
die('Did not connect');
}
?>
I then have the actual book.php (index page) that contains the following code:
<?php
include_once 'sqlConnect.php';
?>
<!doctype html>
<html lang="en">
<head>
<title> Library Catalog </title>
</head>
<style>
h1 {
color: #08298A;
}
</style>
<body>
<h1> <center> Library Catalog </center> </h1>
<h4> <center> Add a New Book </center> </h4>
<center>
<form method="POST">
<input type="text" name="title" placeholder="Title" id="title">
<input type="text" name="author" placeholder="Author" id="author">
<input type="text" name="genre" placeholder="Genre" id="genre">
<input type="text" name="quantity" placeholder="Quantity" id="quantity">
<input type="submit" name="submit" value="Submit"/>
<!-- <button type="submit" name="submit"> Submit</button> -->
</form>
</center>
<?php
$title = $_POST['title'];
$author = $_POST['author'];
$genre = $_POST['genre'];
$quantity = (int)$_POST['quantity'];
$submit = $_POST['submit'];
if ($submit) {
$sql = "INSERT INTO catalog (id, title, author, genre, quantity) VALUES (NULL, '$title', '$author', '$genre', '10');";
mysqli_query($con, $sql);
}
?>
</body>
</html>
When I enter in values on the page and hit submit, nothing happens. I have tested to make sure the query is acceptable. I ran into the issue that "quantity" is actually set to a string not an int like it wants in database so i just hard coded in a 10 for now. I can get the query code to work if I place it in sqlConnect.php but it will not work inside of book.php. Am I not connecting to the database correctly by including the sqlConnect.php class?
Any help would be greatly appreciated!