I'm currently trying to get a database updated through a PHP form. Firstly I get all the information from the database within the form, and then trying to update details within that form to reflect on the database.
I could successfully managed to get all the details from the database within the form but unfortunately doesn't update for that reason.
Here is the code to retrieve the info from DB, within the form which works 100%:
include_once 'db_connect.php';
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form id=updateVehicle action='' method=post";
echo "<div id='vehicleDescription'>";
echo "<div>";
echo "<label for=vin>VIN</label>";
echo "<input type=text id=vin name=vin minlength=17 maxlength=17 placeholder=e.g. BMW1234567890ABCD value=" . $row['vin'] . " />";
echo "<br/>";
echo "</div>";
echo "<div>";
echo "<label for=make>Make</label>";
echo "<input type=text id=make name=make minlength=3 maxlength=13 placeholder=e.g. BMW value=" . $row['make'] . " />";
echo "<br/>";
echo "</div>";
echo "<div>";
echo "<label for=model>Model</label>";
echo "<input type=text id=model name=model minlength=2 maxlength=15 placeholder=e.g. 530D value=" . $row['model'] . " />";
echo "<br/>";
echo "</div>";
echo "<div>";
echo "<input type=text name=id value=" . $row['id'] . " />";
echo "</div>";
echo "<span><?php echo $errorMessage;?></span>";
echo "</div> <!--End of vehicleDescription div-->";
echo "<input type='submit' class='submit' value='Update Ads' />";
echo "</form>";
}
}
The action is empty because it submits to itself, I've tried to use "echo htmlspecialchars($_SERVER["PHP_SELF"]);" but it didn't work, so I left it out.
Here is the code to process the update which doesn't work:
if(isset($_POST['vin'], $_POST['make'], $_POST['model'])) {
$vin = filter_input(INPUT_POST, 'vin', FILTER_SANITIZE_STRING);
$make = filter_input(INPUT_POST, 'make', FILTER_SANITIZE_STRING);
$model = filter_input(INPUT_POST, 'model', FILTER_SANITIZE_STRING);
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
if($insert_stmt = $mysqli->prepare("UPDATE vehicles SET (vin=?, make=?, model=? WHERE id=? ")) {
$insert_stmt->bind_param("sssi", $vin, $make, $model, $id);
if(!$insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: UPDATE');
$mysqli->close();
}
}
header('Location: ../includes/register_success.php');
$mysqli->close();
}
For some unknown reasons to me, it doesn't update the database at all. The 'db_connect.php' works 100% as I'm using it for other database queries.
I'd really appreciate any help if you could point me in the right direction.
Thank you!
$vin $make $model $idafter sanitizing them?