-2

I've been working on this for hours now and including rebuilding my whole code I cannot figure out what is wrong. The HTML form is supposed to populate the patients SQL database but it is not working. I have a similar form that I am using on another part of the website that works flawlessly but this on doesn't seem to and a can't figure out why.

I have checked if the $_POST 's are coming through and they are but the $message values are not.

thanks for your help Here is a picture of the database fields: database fields

Heres the code:

<?php


require 'connection.php';

$message = '';

if(!empty($_POST['name']) && !empty($_POST['dob']) && !empty($_POST['nok']) && !empty($_POST['nok_add']) && !empty($_POST['nok_phone'])&& !empty($_POST['doa'])&& !empty($_POST['allergies'])):

  // Enter the new user in the database
  $sql = "INSERT INTO patients (name, dob, nok, nok_add, nok_phone, doa, allergies) VALUES (:name, :dob, :nok, :nok_add, :nok_phone, :doa, :allergies)";
  $stmt = $conn->prepare($sql);

  $stmt->bindParam(':name', $_POST['name']);
  $stmt->bindParam(':dob', $_POST['dob']);
  $stmt->bindParam(':nok', $_POST['nok']);
  $stmt->bindParam(':nok_add', $_POST['nok_add']);
  $stmt->bindParam(':nok_phone', $_POST['nok_phone']);
  $stmt->bindParam(':doa', $_POST['doa']);
  $stmt->bindParam(':allergies', $_POST['allergies']);

  if( $stmt->execute() ):
    $message = 'Successfully created new user';
  else:
    $message = 'Sorry there must have been an issue creating your account';
  endif;

endif;



?>


<html>
  <head>
    <title>Patient Add</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" type="text/css" href="assets/css/style.css">
      <link href="//fonts.googleapis.com/css?fami=Roboto:400,300,200,100&subset=latin,cyrillic" rel="stylesheet">
<style type="text/css">      
        #headline {
        padding: 0.8em;
        color: white;
        font-family: Roboto, helvetica, arial, sans-serif;
        background-color: black;
        background-image: url(backgroundimage.jpg);
        background-size: cover;
      } 

</style>

  </head>
  <body>

    <div id="headline">
      <div class="container">
        <header>
          <h1>Patient Add</h1>
          <p></p>
        </header>



    <button onClick="window.location='manager_select.php';" class="_button">Back</button>
    <form method="POST" id="add">

      <h2>Enter the Patient's Details</h2>

      <label for="name">Name:</label>
      <input type="text" name="name">

      <label for="dob">Date of Birth:</label>
      <input type="text" name="dob">

      <label for="nok">Next of Kin:</label>
      <input type="text" name="nok">

      <label for="nok_add">Address:</label>
      <input type="text" name="nok_add">

      <label for="nok_phone">Phone:</label>
      <input type="text" name="nok_phone">

      <label for="doa">Date of Admission:</label>
      <input type="text" name="dob">

      <label for="allergies">Allergies:</label>
      <input type="text" name="allergies">

      <?php if(!empty($message)): ?>
        <p><?= $message ?></p>
      <?php endif; ?>
      <input type="submit">

    </form>
        <br>
      </div>
    </div>

  <!-- // [START footer] -->
      <footer>
        <div class="container">
          <p>Copyright Ghyll Court Residental Home 2017</p>
        </div>
      </footer>
      <!-- // [END footer] -->

  </script>
      <script type="text/javascript">
        function init() {
          window.matchMedia("(max-width: 600px)").addListener(hitMQ);
        }

        function hitMQ(evt) {
          sampleCompleted("GettingStarted-ContentWithStyles");
        }

        init();

      </script>

  </body>
</html>
miken32
  • 42,008
  • 16
  • 111
  • 154
  • You need to do more error checking here. Does the `prepare()` succeed? How about the binds? Does `$conn` even exist? – miken32 Mar 30 '17 at 21:40
  • @miken32 the $conn is in the database connection file but I am not sure how to check the prepare and the binds succeed – charles verfuerth Mar 30 '17 at 21:43
  • Are there any errors thrown? - in the PHP log, for example? – Ray O'Donnell Mar 30 '17 at 21:43
  • @RayO'Donnell no errors are thrown that is what is mainly confusing me – charles verfuerth Mar 30 '17 at 21:44
  • Check the same way you check `execute()`, or enable exceptions and use try/catch. `$conn` may be defined in the database file, but if it isn't a valid database connection, you'll not have much luck using it. See also http://stackoverflow.com/q/845021/1255289 – miken32 Mar 30 '17 at 21:44
  • Is the body of the outer `if` getting executed at all? Try adding an `else` clause to it, to check - something like `...else { print 'This happened instead!'; }` – Ray O'Donnell Mar 30 '17 at 21:44
  • you posted this already http://stackoverflow.com/questions/43127227/html-php-form-not-adding-to-sql-database – Funk Forty Niner Mar 30 '17 at 21:49

1 Answers1

1

Your if-statement checks !empty($_POST['doa']), but your form does not contain a doa:

<label for="doa">Date of Admission:</label>
<input type="text" name="dob">

This <input> should probably have name="doa" instead of dob.

rickdenhaan
  • 10,857
  • 28
  • 37