Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Nick Davies
Nick Davies
7,972 Points

Inserting/Recording data from a Userform into a database with MySQL and PHP.

I have been trying to insert data into my database. I keep getting an error where it isn't recognising what is set. It thinks this is invalid: if (isset($_POST['$user_email']) AND isset($_POST['$user_choice'])) { so it skips and displays my echo with the values coming through.

Here is my HTML file:

 <?php 
require_once("../inc/config.php");

$emailAddress ="";
if(isset($_POST["user_email"])) {
  $emailAddress = trim($_POST["user_email"]);
  $newsletter = trim($_POST["emailLetter"]);
  if($emailAddress != "") {
    require_once(ROOT_PATH . "inc/images.php");
    $results = capture_email($emailAddress, $newsletter);
  } 
  var_dump($results);
  exit();
}
?>

   <footer id="footer-class">
      <form id="footer-form" method="post" action="">
        <fieldset>
          <legend id="footer-legend">Sign up for...</legend>
          <input type="radio" name="emailLetter" id="PC" value="PC" checked><label class="footerlabel" for="PC">Computer</label>
          <input type="radio" name="emailLetter" id="Photo" value="Photo"><label class="footerlabel" for="Photo">Photo</label>
          <legend id="footer-legend">Newsletter!</legend>
          <input id="footer-email" type="email" name="user_email" placeholder="Enter your email address here!" required>
          <button type="submit" id="footer-button">Subscribe</button>
        </fieldset>
        <p>I won't spam you. Promise</p>
      </form>
    </footer>
  </body>
</html>

Here is my PHP function:

<?php
function capture_email($user_email, $user_choice) {
  require(ROOT_PATH . "inc/database.php");
  if (isset($_POST['$user_email']) AND isset($_POST['$user_choice'])) {
    try {
      $newsletter = $_POST["$user_choice"];
      $email_address = $_POST["$user_email"];
      $results = $db->prepare("INSERT INTO userinfo (Email, userOption) VALUES (?, ?");
      $results->bindparam(1, $email_address);
      $results->bindparam(2, $newsletter);
      $results->execute();
    } catch (Exception $e) {
      var_dump($e);
      exit();
    }
} else {
  echo "Oops, something went wrong. Here is the user Email: " . $user_email . "<br>Here is the user choice: " . $user_choice;
  exit();
}
}

Additional information: Config.php is a file containing variables for the database connection. images.php is a file where I store all my php functions.

Please let me know if you need anymore information.

Thanks

4 Answers

Nick Davies
Nick Davies
7,972 Points

Silly mistake, had a typo in the SQL statement. Below is the fix: $results = $db->prepare("INSERT INTO userinfo (Email, userOption) VALUES (?, ?");

Nic Hoo
Nic Hoo
7,992 Points

Try dropping the $'s as in, if (isset($_POST['user_email']) AND isset($_POST['user_choice']))

Nick Davies
Nick Davies
7,972 Points

Dropped the $'s and it still returns false

Nic Hoo
Nic Hoo
7,992 Points

Try printing your array to screen, like so;

Insert this above your if statement.

echo "<pre>"; print_r($_POST); echo "</pre>"; if (isset($_POST['$user_email']) AND isset($_POST['$user_choice'])) { ...

Nick Davies
Nick Davies
7,972 Points

Sorry Nic, thought I had answered my own question already. Found out the issue was with my SQL statement this is what it should have looked like: ("INSERT INTO userinfo (Email, userOption) VALUES (?, ?)"); I had then ending like this: (?,?"));

Nic Hoo
Nic Hoo
7,992 Points
echo "<pre>";
print_r($_POST);
echo "</pre>";