Hopefully you can help me with this. I have been working on a very simple database and have hit a brick wall.
I have 2 PHP files, 1 containing functions (bintype.php) and then a file called update_bintype.php.
See below function code:
function update(){
$query = "UPDATE
" . $this->table_name . "
SET
BinTypeName=:bintypename, BinTypeDescription=:bintypedescription, CleanPricePerBin=:costperbin
WHERE
BinTypeID = :id";
$stmt = $this->conn->prepare($query);
// posted values
$this->BinTypeName=htmlspecialchars(strip_tags($this->BinTypeName));
$this->BinTypeDescription=htmlspecialchars(strip_tags($this->BinTypeDescription));
$this->CleanPricePerBin=htmlspecialchars(strip_tags($this->CleanPricePerBin));
$this->BinTypeID=htmlspecialchars(strip_tags($this->BinTypeID));
// bind values
$stmt->bindParam(":bintypename", $this->BinTypeName);
$stmt->bindParam(":bintypedescription", $this->BinTypeDescription);
$stmt->bindParam(":costperbin", $this->CleanPricePerBin);
$stmt->bindParam(":id", $this->BinTypeID);
// execute the query
if($stmt->execute()){
return true;
}
return false;
}
Update_bintype.php contains the following code, a very simple form which calls saved values from the database and supposedly updates any amendments to the database when Save is pressed.
<?php
// get ID of the product to be edited
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: missing ID.');
// include database and object files
include_once 'config/database.php';
include_once 'objects/bintype.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// prepare objects
$BinType = new BinType($db);
// set ID property of product to be edited
$BinType->BinTypeID = $id;
// read the details of product to be edited
$BinType->readOne();
// set page header
$page_title = "Edit Bin Type";
include_once "layout_header.php";
echo "<div class='right-button-margin'>";
echo "<a href='view_bintypes.php' class='btn btn-default pull-right'>View Bin Types</a>";
echo "</div>";
?>
<?php
// if the form was submitted
if($_POST){
// set bin type property values
$BinType->BinTypeName = $_POST['bintypename'];
$BinType->BinTypeDescription = $_POST['bintypedescription'];
$BinType->CleanPricePerBin = $_POST['cleanpricerperbin'];
// update the bin type
if($BinType->update()){
echo "<div class='alert alert-success alert-dismissable'>";
echo "Bin Type was updated.";
echo "</div>";
}
// if unable to update the bin type, tell the user
else{
echo "<div class='alert alert-danger alert-dismissable'>";
echo "Unable to update Bin Type.";
echo "</div>";
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Bin Type Name</td>
<td><input type='text' name='bintypename' value='<?php echo $BinType->BinTypeName; ?>' class='form-control' /></td>
</tr>
<tr>
<td>Description</td>
<td><input type='text' name='bintypedescription' value='<?php echo $BinType->BinTypeDescription; ?>' class='form-control' /></td>
</tr>
<tr>
<td>Cost Per Bin</td>
<td><input type='text' name='costperbin' value='<?php echo $BinType->CleanPricePerBin; ?>' class='form-control' /></td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" class="btn btn-primary">Save</button>
</td>
</tr>
</table>
</form>
<?php
// set page footer
include_once "layout_footer.php";
?>
All fields update perfectly except the Cost Per Bin field, which when I submit overwrites the current value with NULL and saves NULL to the database and I can't figure it out for the life of me - PLEASE HELP BEFORE I GO CRAZZZZYYYY!
Thanks so much, Ross