Hi All
Im stuck on a form that wont submit to the db. At the end of the PHP code i get a “Something went wrong. Please try again later.” which is just before the closing statement.
I’ve been over the code so many times but i cant see the wood through the trees anymore. Please could someone cast their eye over this to see what i’m missing… Nothing shows up in the PHP logs either?
<?php
// Include config file
require_once "includes/db_conn.php";
// Define variables and initialize with empty values
$name = $contactno = $email = $whatservice = $make = $mdl = $engine = $fueltype = "";
$name_err = $contactno_err = $email_err = $whatservice_err = $make_err = $mdl_err = $engine_err = $fueltype_err = "";
// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['contactno'])) {
$contactno = $_POST['contactno'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['whatservice'])) {
$whatservice = $_POST['whatservice'];
}
if (isset($_POST['make'])) {
$make = $_POST['make'];
}
if (isset($_POST['mdl'])) {
$mdl = $_POST['mdl'];
}
if (isset($_POST['engine'])) {
$engine = $_POST['engine'];
}
if (isset($_POST['fueltype'])) {
$fueltype = $_POST['fueltype'];
}
// Check input errors before inserting in database
if (empty($name_err) && empty($contactno_err) && empty($email_err) && empty($engine_err)) {
// Prepare an insert statement
$sql = "INSERT INTO svcenq (name, contactno, email, whatservice, make, mdl, engine, fueltype) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($con, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssssss", $param_name, $param_contactno, $param_email, $param_whatservice, $param_make, $param_mdl, $param_engine, $param_fueltype);
// Set parameters
$param_name = $name;
$param_contactno = $contactno;
$param_email = $email;
$param_whatservice = $whatservice;
$param_make = $make;
$param_mdl = $mdl;
$param_engine = $engine;
$param_fueltype = $fueltype;
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
echo "Record created successfully"; //Redirect to landing page
header("location: index.php");
exit();
} else {
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($con);
}
?>
<form action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>" method="post">
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Name<sup>*</sup></label>
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
<span class="help-block"><?php echo $name_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Phone</label>
<input type="text" name="contactno" class="form-control" value="<?php echo $contactno; ?>">
<span class="help-block"><?php echo $contactno_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Email<sup>*</sup></label>
<input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
<span class="help-block"><?php echo $email_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Fuel Type<sup>*</sup></label>
<select type="text" name="whatservice" class="selectpicker" value="<?php echo $whatservice; ?>">
<span class="help-block"><?php echo $whatservice_err;?></span>
<option>Petrol</option>
<option>Diesel</option>
</select>
</div>
<!-- end col -->
</div>
<hr class="invis2">
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Make</label>
<input type="text" name="make" class="form-control" value="<?php echo $make; ?>">
<span class="help-block"><?php echo $make_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Model</label>
<input type="text" name="mdl" class="form-control" value="<?php echo $mdl; ?>">
<span class="help-block"><?php echo $mdl_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Engine Size</label>
<input type="text" name="engine" class="form-control" value="<?php echo $engine; ?>">
<span class="help-block"><?php echo $engine_err;?></span>
</div>
<!-- end col -->
<div class="col-md-3 col-sm-4 col-xs-12">
<label>Fuel Type</label>
<select type="text" name="fueltype" class="form-control" value="<?php echo $fueltype; ?>">
<span class="help-block"><?php echo $fueltype_err;?></span>
<option>Petrol</option>
<option>Diesel</option>
</select>
</div>
<!-- end col -->
</div>
<hr class="invis1">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<input type="submit" class="btn btn-primary btn-block" value="Submit">