I haven't done anything with PHP or MYSQL since last year and I was still only a beginner then, so please forgive the question. I'm sure I am missing something so simple and obvious but for the life of me can't figure it out, so I hope someone can point me in the right direction.
Here is my set up: I have a db_connect.php page where my Database connection details are
$dbc = new mysqli('localhost', 'root', '', 'db_name');
//run connect_errno to enure it connects to the db. If not then kill the rest of the script and show error message
if($dbc->connect_errno) {
die('Failed to connect to the MYSQL Database');
}
I have a add_stocktake.php page with the following form:
<div class ="wrapper">
<?php if (isset($_GET["status"]) AND $_GET["status"] == "success") { ?>
<p> Your Successfully added a new stock take. </p>
<?php } else { ?>
<form method="post" action="add_stocktake.php">
<table>
<tr>
<th>
<label for="manufacturer">Manufacturer</label>
</th>
<td>
<input type="text" name="manufacturer" id="manufacturer">
</td>
<th>
<label for="model">Model</label>
</th>
<td>
<input type="text" name="model" id="model">
</td>
<th>
<label for="product_name">Product Name</label>
</th>
<td>
<input type="text" name="product_name" id="product_name">
</td>
<th>
<label for="quantity">Quantity</label>
</th>
<td>
<input type="text" name="quantity" id="quantity">
</td>
</tr>
<tr>
<th>
<label for="stocktake_date">Date of Stock take</label>
</th>
<td>
<input type="date" name="stocktake_date" id="stocktake_date">
</td>
</table>
<p>
<input type="submit" value="Save">
</form>
<?php }?>
</div>
and the following php:
<?php
require 'db_connect.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$manufacturer = trim($_POST["manufacturer"]);
$model = trim($_POST["model"]);
$product_name = trim($_POST["product_name"]);
$quantity = trim($_POST["quantity"]);
$stocktake_date = trim($_POST["stocktake_date"]);
//VALIDATIONS
if ($manufacturer == "" OR $model == "" OR $product_name == "" OR $quantity == "" OR $quantity == "" OR $stocktake_date == ""){
$error_message = "You must fill in all the fields.";
}
// if there's not a previous error message run a database query to add information to the database
if (!isset ($error_message)) {
$query =
$sql = "INSERT INTO stocktake_tbl ('manufacturer','model','product_name','quantity','stocktake_date') VALUES ('".$manufacturer."','".$model."','".$product_name."','".$quantity."','".$stocktake_date."')";
$query_run = mysqli_query($dbc, $sql);
echo "This has been saved successfully";
}
}
?>
Here is what has happens: When I load the page first time and click the save button with nothing entered the page is just refreshed (no error messages). When I fill in the information and click save the page refreshes and it says the "This has been saved successfully" message. When I go to PHPmyadmin there has been nothing added. I added some values while I was at myadmin and then did a query within my page to see if I could pick up the information and it just returned a blank screen so I know it must something I have or haven't done correctly in regards to connecting it up.
Any advice is greatly appreciated (will teach me for leaving it alone for so long!)
Thanks