I'm unsure why my if statementy always validates to false no matter the situation. I keep reading the code and it doesnt look like theres anything wrong. If anyone can help me out that would be great.
The if statement in question is
if($dataValid && $_POST)
Here's my code:
<?php
$instrument = trim($_POST["itemName"]);
$description = trim($_POST["description"]);
$supplier = trim($_POST["supplierCode"]);
$cost = trim($_POST["cost"]);
$price = trim($_POST["price"]);
$onHand = trim($_POST["onHand"]);
$reorderPoint = trim($_POST["reorder"]);
$dataValid = true ;
$instrumentChk = "/^[a-z0-9:; -,]+$/i";
$descriptionChk = "/^[a-z0-9\., -'
]+$/im";
$supplierChk = "/^[a-z0-9 -]+$/i";
$costChk = "/^\d{0,}\.\d\d$/";
$priceChk = "/^\d{0,}\.\d\d$/";
$onHandChk = "/^\d*$/";
$reorderPointChk = "/^\d*$/";
if($_POST){
if(!preg_match($instrumentChk, $instrument)) {
$instrumentErr = "<span style='color:#f00'>Instrument name must be either letters, spaces,
colon, semi-colon, dash, comma, apostrophe or numeric character (0-9)</span>";
$dataValid = false;
}
elseif($instrument == ""){
$instrumentErr = "<span style='color:#f00'>Instrument name must not be blank</span>";
$dataValid = false;
}
if(!preg_match($descriptionChk, $description)) {
$descriptionErr = "<span style='color:#f00'>description must be either letters, spaces,
dashes, commas, apostrophes or digits</span>";
$dataValid = false;
}
elseif($description == "") {
$descriptionErr = "<span style='color:#f00'>Description must not be blank</span>";
$dataValid = false;
}
if(!preg_match($supplierChk, $supplier)) {
$supplierErr = "<span style='color:#f00'>Supplier code must be either
letters, spaces, dashes, or numeric characters(0-9)</span>";
$dataValid = false;
}
elseif($supplier == "") {
$supplierErr = "<span style='color:#f00'>Supplier must not be blank</span>";
$dataValid = false;
}
if(!preg_match($costChk, $cost)) {
$costErr = "<span style='color:#f00'>You must enter monetary amounts only</span>";
$dataValid = false;
}
elseif($cost == null) {
$costErr = "<span style='color:#f00'>Cost cannot be blank</span>";
$dataValid = false;
}
if(!preg_match($priceChk, $price)) {
$priceErr = "<span style='color:#f00'>You must enter monetary amounts only</span>";
$dataValid = false;
}
elseif($price == null) {
$priceErr = "<span style='color:#f00'>Price cannot be blank</span>";
$dataValid = false;
}
if(!preg_match($onHandChk, $onHand)) {
$onHandErr = "<span style='color:#f00'>You must enter digits only</span>";
$dataValid = false;
}
elseif($onHand == null) {
$onHandErr = "<span style='color:#f00'>Instruments on hand cannot not be blank</span>";
$dataValid = false;
}
if(!preg_match($reorderPointChk, $reorderPoint)) {
$reorderPointErr = "<span style='color:#f00'>You must enter digits only</span>";
$dataValid = false;
}
elseif($reorderPoint == null){
$reorderPointErr = "<span style='color:#f00'>Reorder point cannot not be blank</span>";
$dataValid = false;
}
}
if($dataValid && $_POST) {
$secret = file('/home/int322_151a14/secret/topsecret');
$link = mysqli_connect(trim($secret[0]), trim($secret[1]), trim($secret[2]), trim($secret[3])) or die(mysqli_connect_error());
$deleted = 'n';
$backOrder = (isset($_POST['backOrder']) && $_POST['backOrder'] == 'y')?"y":"n";
$sql_insert_query = 'INSERT INTO inventory values("","' . $instrument . '", "' . $description . '", "' . $supplier . '",
"' . $cost . '", "' . $price . '", "' . $onHand . '", "' . $reorderPoint . '", "' . $backOrder . '", "' . $deleted . '")';
$sql_insert_result = mysqli_query($link, $sql_insert_query) or die("Query unsuccessful " . mysqli_error($link));
if($sql_insert_result) {
header("Location: view.php");
} else {
echo "Query unsuccessful - <a href=add.php>please try again</a>";
}
} else {
?>
<?php $page_title = 'add'; ?>
<?php include "library.php"; ?>
<?php heading(); ?>
<h1>Chidi's Musical Instruments</h1>
<header>
<nav id="navigation">
<a href="add.php">Add</a>
<a href="view.php">View All</a>
</nav>
</header>
<br/>
<form action="" method='post'>
<table id="newTable">
<tr>
<td>
<label>Instrument name:</label>
<input type="text" name="itemName" value="<? echo $instrument ?>"><? echo $instrumentErr ?>
</td>
</tr>
<tr>
<td>
<label>Description:</label>
<textarea rows="5" cols="20" name="description"><? echo $description ?></textarea><? echo $descriptionErr ?>
</td>
</tr>
<tr>
<td>
<label>Supplier Code:</label>
<input type="text" name="supplierCode" value="<? echo $supplier ?>"><? echo $supplierErr ?>
</td>
</tr>
<tr>
<td>
<label>Cost:</label>
<input type="text" name="cost" value="<? echo $cost ?>"><? echo $costErr ?>
</td>
</tr>
<tr>
<td>
<label>Selling Price:</label>
<input type="text" name="price" value="<? echo $price ?>"><? echo $priceErr ?>
</td>
</tr>
<tr>
<td>
<label>Number on Hand:</label>
<input type="text" name="onHand" value="<? echo $onHand ?>"><? echo $onHandErr ?>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
<?
}
?>
<?php footer(); ?>
It should evaluate to true if the data is valid and return an error as it wont be able to connect to the database, but instead it just erases all the fields and does nothing. If the data is actually invalid it repopulates the fields as it should and makes you retry. There is nowhere in my code that tells it to do nothing if the if statement evaluates to false, and it seems like that's what the if statement is returning.