【问题标题】:Html / Php form not adding to SQL databaseHtml / Php 表单未添加到 SQL 数据库
【发布时间】:2017-08-24 22:21:06
【问题描述】:

我已经为此工作了几个小时,包括重建我的整个代码,我无法弄清楚哪里出了问题。 HTML 表单应该填充患者 SQL 数据库,但它不起作用。我有一个类似的表格,我在网站的另一部分使用它可以完美运行,但这似乎没有,也无法弄清楚为什么。

感谢您的帮助

这是数据库字段的图片: Database fields

代码如下:

<?php

//session_start();

//if( isset($_SESSION['user_id']) ){
//  header("Location: /");
//}

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>

【问题讨论】:

  • 你的 $_POST 是否通过了? print_r($_POST);高于你的 if 测试
  • 通过添加error_reporting(E_ALL)来启用错误;在页面顶部。
  • 它是否打印$message 值之一?
  • @Barmar 不,它不打印任何 $message 值。
  • 那么开头的if()语句一定不成功。检查$_POST中的值。

标签: php html mysql forms


【解决方案1】:

正如您在duplicate post 上回答的那样(为了完整性也在这里回答),您正在检查if 语句中不存在的“doa”字段。您的表格定义了“dob”两次:一次是出生日期,一次是入学日期:

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

...

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

【讨论】:

  • 据我所知,这是正确的答案,因为他说您没有“doa”字段,因此它将始终为“空”,并且您的 if() 语句将永远不会执行。
  • @rickdenhann 非常感谢您帮助解决您看不到的小事!
【解决方案2】:

我不完全确定这是最安全的方式,但你为什么不尝试简单的mysqli_query($conn,$sql);而不是使用 MSQLI PDO@

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多