0

Hello could anyone assist me on this issue. Im getting connection failed each time I enter this code, and I'm unsure why.

<?php include "includes/conn.php" ?>
<?php
if (isset($_POST['submit'])) {
  $firstname = $_POST['fName'];
  // echo $firstname;
  $lastname = $_POST['lName'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $date_ob = date('Y-m-d', strtotime($_POST['DOB']));
  $gender = $_POST['Gender'];
  $ethnicity = $_POST['Ethnicity'];


  $insert_query = "INSERT `alumni_table`(fName,lName,email,phone,DOB,Gender,Ethnicity) VALUES ('$firstname', '$lastname', '$email', '$phone', '$date_ob', '$gender', '$ethnicity') ";
  $result = mysqli_query($conn, $insert_query);
  if ($result) {
    echo "Data inserted successfully";
  } else {
    die("Connection failed: " . $conn->connect_error);
  }
}

Below is my form data

<div class="container my-5">
    <form action="" method="post">
      <div class="form-group">
        <label>First Name</label>
        <input type="text" required="required" autocomplete="off" placeholder="Enter your First Name" name="fName" id="fName" class="form-control">
      </div>
      <div class="form-group">
        <label>Last Name</label>
        <input type="text" required="required" autocomplete="off" placeholder="Enter your Last Name" name="lName" id="lName" class="form-control">
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="email" required="required" autocomplete="off" placeholder="Enter your Email" name="email" id="email" class="form-control">
      </div>
      <div class="form-group">
        <label>Phone #</label>
        <input type="text" required="required" autocomplete="off" placeholder="Enter your Phone #" name="phone" id="phone" class="form-control">
      </div>
      <div class="form-group">
        <label>DOB</label>
        <input type="date" autocomplete="off" placeholder="Enter your Date of Birth" name="DOB" id="DOB" class="form-control">
      </div>
      <div class="form-group">
        <label>Gender</label>
        <input type="text" required="required" autocomplete="off" placeholder="Enter your Gender" name="Gender" id="Gender" class="form-control">
      </div>
      <div class="form-group">
        <label>Ethnicity</label>
        <input type="number" required="required" autocomplete="off" placeholder="Enter your Ethnicity" name="Ethnicity" id="Ethnicity" class="form-control">
      </div>
      <div class="row">
        <div class="col-sm-6 col-sm-offset-3">
          <input type="submit" name="submit" id="submit" tabindex="4" class="form-control btn btn-dark" value="Update User">
        </div>
    </form>
  </div>

Here is my conn.php file

$servername = "localhost";
$username = "root";
$password = "root";
$database = "example_test";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

And Below is my MySQL phpMyadmin column names and values associated with it

fName type =varchar(45),
lName type = varchar(45),
email type = varchar(45),
phone type = varchar(12),
DOB   type = date,
Gender type = char(1),
Ethnicity type = tinyint(1)

I apologize if the code is sloppy this is my first time asking on here. Would appreciate the help

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    You are wide open for SQL injection. Since you're using mysqli, take advantage of [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php). **This will take care of any pesky quoting issues that may occur,** such as apostrophes in names (O'Neill) – aynber Jul 07 '22 at 16:29
  • 4
    Is the *connection* failing, or is the *query* failing? The code doesn't seem to distinguish between the two. If the *query* is failing then you want to look at `$conn->error` to find the actual error message. (Which could be any number of syntax errors, since the code is wide open to SQL injection.) – David Jul 07 '22 at 16:30
  • 1
    I guess you did copy & paste for the error handling code, because your code reports "Connection error" after trying to execute the query, and it echoes the wrong error variable. I suggest you review the code examples on this page: https://www.php.net/manual/en/mysqli.error.php – Bill Karwin Jul 07 '22 at 16:39

0 Answers0