title: Using PDO and PHP to insert data from form into MySQL database
body: <p>I'm currently working on taking data from my form "form.php" and sending that to "index.php" where i'm using PDO statements to insert those values into the MySQL database. In MySQL I have it set up where firstname and lastname can not be null because the data is going to be submitted back to website where the First Name + Last Name = Full Name and that full name pops up on the supporters list. </p>
  
  <p>My question is, </p>
  
  <p>If I don't want to set MySQL to accept a NULL value, what should I change in my PHP to accept values from the form and submit that to the database with the value attached to that instance of that specific user? I tried php.net/docs and the other StackOverflow questions didn't fully solve my challenge, any help is greatly appreciated</p>
  
  <p><img src="https://i.stack.imgur.com/Ml00Q.png" alt="Form"></p>
  
  <p>FORM.PHP</p>
  
  <pre><code>&lt;form action="../index.php" method="post"&gt;
        &lt;fieldset class="form-group"&gt;
        &lt;label for="firstname"&gt;First Name&lt;/label&gt;
        &lt;input type="text" name="firstname" class="form-control" id="firstname" placeholder="First Name"&gt;
      &lt;/fieldset&gt;
  
  
      &lt;fieldset class="form-group"&gt;
        &lt;label for="lastname"&gt;Last Name&lt;/label&gt;
        &lt;input type="text" name="lastname" class="form-control" id="lastname" placeholder="Last Name"&gt;
      &lt;/fieldset&gt;
  
  
          &lt;fieldset class="form-group"&gt;
            &lt;label for="exampleInputEmail1"&gt;Email address&lt;/label&gt;
            &lt;input type="text" name="email" class="form-control" id="email" placeholder="Enter email"&gt;
            &lt;small class="text-muted"&gt;We'll never share your email with anyone else.&lt;/small&gt;
        &lt;/fieldset&gt;
  
        &lt;button type="submit" id="form-button" class="btn btn-primary"&gt;Submit&lt;/button&gt;
  
      &lt;/form&gt;
  </code></pre>
  
  <p>I am sending that to index.php which is where I am receiving the error!</p>
  
  <p>"Error in connection to databaseSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstname' cannot be null"</p>
  
  <p>Here is my index.php code</p>
  
  <pre><code>$form = $_POST;
  $id = $form['id'];
  $firstname = $form['firstname'];
  $lastname = $form['lastname'];
  $email = $form['email'];
  
  
  try {
  $db = new PDO("mysql:host=" . DB_HOST. ";dbname=" .DB_NAME .";port=".DB_PORT,DB_USER,DB_PASS);
  $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $add_supporter = $db-&gt;prepare("INSERT INTO supporters (id, firstname, lastname, email) 
                               VALUES (:id, :firstname, :lastname, :email)");
  
  $add_supporter-&gt;bindParam(":id", $id);
  $add_supporter-&gt;bindParam(":firstname", $firstname);
  $add_supporter-&gt;bindParam(":lastname", $lastname);
  $add_supporter-&gt;bindParam(":email", $email);
  $add_supporter-&gt;execute();
  
  
  
  } catch (Exception $e) {
  echo "Error in connection to database" . $e-&gt;getMessage() . "&lt;/br&gt;";
  die();
  }
  </code></pre>