PHP Form wont write to DB - Where am i going wrong?

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">

Aside from whatever problem you have, stop creating variables for nothing, get rid of the form action completely and there is no need to manually close the DB connection. I would also recommend you use PDO.

1 Like

It looks like you are defining the parameter values after you have bound them.
I assume that’s a problem, though I don’t use mysqli in favour of PDO.
And I also don’t see the point of tranfering those values to new variables.
Why not just bind the original variable in the first place?

2 Likes

Yeah, after looking up the syntax I believe the problem is what @SamA74 said. Look at the manual for the proper syntax.

You have at least 50% more code than you even need.

1 Like

Thanks guys. I’m pretty new to PHP so I followed a tutorial and modified it to suit my code.

Are you suggesting that I bin off the variables at the top in place of the variables declared after I’ve bound them?

Oddly, I have the same form on another page with one variable difference and it writes to that particular table perfectly.

Looking at the examples there, the parameter values are being defined after binding.
That seems illogical to my mind, but that’s how it is.

Though I think it would be worth testing by binding the variables already set.

Also, do you have error reporting on?

Forgive my lack of knowledge, how would i test binding the variables already set?

Currently using PHP logs in MAMP and its just fired out this:-
“mysqli_stmt_close() expects parameter 1 to be mysqli_stmt,”

If i remove the variables at the top, it spits out a load of PHP Notice lines stating that i have undefined variables :confused:

Something like:-

mysqli_stmt_bind_param($stmt, "ssssssss", $name, $contactno, $email, $whatservice, $make, $mdl, $engine, $fueltype);

Then lose the // Set parameters section.
But I could be wrong, I’m not familiar with prepared statements in mysqli.

Thanks Sam, i’ve just removed the set parameters section and i got a PHP warning of: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt,.

This form works perfectly on another page, Theres only one variable difference in the two forms and obviously the table it writes to but this form doesn’t want to work.

One more reason to not use mysqli…

By the way OP, checking for isset on your form fields is pointless. They will always be isset. You need to trim the post array and then check for empty.

That suggests an issue with the $stmt variable.
Try var_dump()ing it at some point in the script to see what it really is.

Thanks Benanamen, i’ll adjust the code

Thanks Sam. Just tried var dumping and it fired out this in the PHP error log…
var_dump() expects at least 1 parameter, 0 given

You have to put something in to dump about.
https://www.php.net/manual/en/function.var-dump.php

Thanks for the link. I’m going to spend a few nights reading up on php.net as i think i’m missing a lot.

I managed to fix the issue, it was on the DB itself, i must have been over characters on one of the columns. Thanks gents for your help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.