This one is leaving me scratching my head. I'm sure I'm missing something simple, but for the life of me I can't see what when I compare it to other INSERT statements I've written that work just fine. The parameters are being passed when I check the network tab in developer, but it won't forward to the page specified in the header, and the table is not updated with information when I fill in the form.
Here is the function I wrote in the model layer:
function add_yarn($yarnbrand, $yarnamount, $yarnweight, $yarncolor) {
global $db;
$query = 'INSERT INTO yarn
(yarnbrand, yarnamount, yarnweight, yarncolor)
VALUES
(:yarnbrand, :yarnamount, :yarnweight, :yarncolor)';
$statement = $db->prepare($query);
$statement->bindValue(':yarnbrand', $yarnbrand);
$statement->bindValue(':yarnamount', $yarnamount);
$statement->bindValue(':yarnweight', $yarnweight);
$statement->bindValue(':yarncolor', $yarncolor);
$statement->execute();
$statement->closeCursor();
}
Here is the control layer in for the action associated with that function in index.php:
case 'yarn_add' :
$yarnbrand = filter_input(INPUT_POST, 'yarnbrand');
$yarnamount = filter_input(INPUT_POST, 'yarnamount', FILTER_VALIDATE_INT);
$yarnweight = filter_input(INPUT_POST, 'yarnweight');
$yarncolor = filter_input(INPUT_POST, 'yarncolor');
if ($yarnbrand == NULL || $yarnamount == NULL ||
$yarnweight == NULL || $yarncolor == NULL) {
echo 'Empty or invalid data input.';
} else {
add_yarn($yarnbrand, $yarnamount, $yarnweight, $yarncolor);
header('Location: index.php?action=view_yarn');
}
break;
and here is the view layer with the add form:
<?php include '../view/header.php'; ?>
<main>
<h1>Add Yarn</h1>
<form action="index.php" method="post" id="add_yarn_form">
<input type="hidden" name="action" value="yarn_add">
<label>Brand:</label>
<input type="text" name="yarnbrand"> <br>
<label>Weight:</label>
<input type="text" name="yarnweight"><br>
<label>Amount:</label>
<input type="text" name="yarnamount"><br>
<label>Color:</label>
<input type="text" name="yarncolor"><br>
<label> </label>
<input type="submit" value="Save Changes"><br>
</form>
<div class="bottomtext">
<a href="index.php?action=view_yarn">View Yarns</a>
<a href="index.php?action=list_supplies">View Supplies</a>
</div>
</main>
<?php include '../view/footer.php'; ?>
I appreciate any help!
Maybe a variable with an illegal caracter ? You don't use filters in your filter_input, expected for the integer variable.
Take a look on the manual :
If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.
Not really secure :/