0

INSERT INTO for PHP is not inserting data into my database users table on MySQL workbench. When click the submit button it just goes to a blank submit.php file when it's suppose to redirect to the login page once a user finishes inputting their information. Note: I'm still in the process of learning PHP, so any help would be appreciated. My code for my registration page(landingTwo) and my submit.php page is below:

landingTwo.php

<!DOCTYPE html>


<?php

require_once('db-include/config.php');
require_once('db-include/session.php');

if (!isset($_SESSION['needs']))
  header("location: /index.php");

?>

<html lang="en">
<body>

  <form action="db-include/submit.php" method="POST">
    <input class="form-control form-control-lg" name="first_name" type="text" style="
          width: 300px;
                height: 53px;
                text-align: center;
                margin-top: 20px;
                margin-bottom: 10px;
              " placeholder="First Name" required />
          <input class="form-control form-control-lg" name="last_name" type="text" style="
                width: 300px;
                height: 53px;
                text-align: center;
                margin-top: 10px;
                margin-bottom: 10px;
              " placeholder="Last Name" required />
          <input class="form-control form-control-lg" name="email" type="email" style="
                width: 300px;
                height: 53px;
                text-align: center;
                margin-top: 10px;
                margin-bottom: 10px;
              " placeholder="Email Address" required />
          <input class="form-control form-control-lg" name="city" type="text" style="
                width: 300px;
                height: 53px;
                text-align: center;
                margin-top: 10px;
                margin-bottom: 10px;
              " placeholder="City" required />
          <select class="bg-light form-select" name="state_name" value="state" style="
                width: 300px;
                height: 53px;
                font-size: 20px;
                text-align: center;
                color: var(--bs-gray);
              ">
            <optgroup label="State">
              <option value="AK">AK</option>
              <option value="AL">AL</option>
              <option value="AR">AR</option>
              <option value="AZ">AZ</option>
              <option value="CA">CA</option>
              <option value="CO">CO</option>
              <option value="CT">CT</option>
              <option value="DC">DC</option>
              <option value="DE">DE</option>
              <option value="FL">FL</option>
              <option value="GA">GA</option>
              <option value="HI">HI</option>
              <option value="IA">IA</option>
              <option value="ID">ID</option>
              <option value="IL">IL</option>
              <option value="IN">IN</option>
              <option value="KS">KS</option>
              <option value="KY">KY</option>
              <option value="LA">LA</option>
              <option value="MA">MA</option>
              <option value="MD">MD</option>
              <option value="ME">ME</option>
              <option value="MI">MI</option>
              <option value="MN">MN</option>
              <option value="MO">MO</option>
              <option value="MS">MS</option>
              <option value="MT">MT</option>
              <option value="NC">NC</option>
              <option value="ND">ND</option>
              <option value="NE">NE</option>
              <option value="NH">NH</option>
              <option value="NJ">NJ</option>
              <option value="NM">NM</option>
              <option value="NV">NV</option>
              <option value="NY">NY</option>
              <option value="OH">OH</option>
              <option value="OK">OK</option>
              <option value="OR">OR</option>
              <option value="PA">PA</option>
              <option value="RI">RI</option>
              <option value="SC">SC</option>
              <option value="SD">SD</option>
              <option value="TN">TN</option>
              <option value="TX">TX</option>
              <option value="UT">UT</option>
              <option value="VA">VA</option>
              <option value="VT">VT</option>
              <option value="WA">WA</option>
              <option value="WI">WI</option>
              <option value="WV">WV</option>
              <option value="WY">WY</option>
            </optgroup>
          </select>
          <input class="form-control form-control-lg" name="passwd" type="password" style="
                width: 300px;
                height: 53px;
                margin-top: 10px;
                margin-bottom: 10px;
                text-align: center;
              " placeholder="Create Password" required="" minlength="6" maxlength="20" />
          <input class="btn btn-lg btn-primary" type="submit" style="
                width: 300px;
                height: 55px;
                background: var(--bs-orange);
                margin-top: 25px;
                margin-bottom: 10px;
              " value="Find Match" />
        </form>

      </div>
      <div class="col"></div>
      <div class="col-md-4"></div>
    </div>
  </div>
 
       
</body>

</html>

submit.php

<?php

require_once('config.php');
require_once('session.php');

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$city = $_POST['city'];
$state_name = $_POST['state_name'];
$passwd = $_POST['passwd'];

// Hash the password for security
$hashed_password = password_hash($passwd, PASSWORD_DEFAULT);

$sql = "INSERT INTO users (first_name, last_name, email, city, state_name, passwd, date_added) VALUES (?, ?, ?, ?, ?, ?, NOW())";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssss", $first_name, $last_name, $email, $city, $state_name, $hashed_password);

if ($stmt->execute()) {
    header("Location: /login.php"); // Redirect to the login page after successful registration
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$stmt->close();
$conn->close();


?>

I expect the form if successful to go to the login.php file when successfully executed and inserted into the users database.

A Knight
  • 1
  • 1
  • 1
    _"it just goes to a blank submit.php"_... sounds like you've got an error you can't see due to your configuration – Phil Apr 26 '23 at 02:07
  • At the top of `submit.php`, before the _require_ lines, add `ini_set('display_errors', 'On'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` and see if you get any more detail about what's going wrong – Phil Apr 26 '23 at 02:37
  • 2
    Also, hardly anyone reads the edit summaries so if you want to communicate any issues with the duplicate link, please [edit] your question to explain what you tried and / or reply here in the comments – Phil Apr 26 '23 at 02:44

0 Answers0