PHP将html表单输入提交到数据库

时间:2019-01-02 22:55:45

标签: php html mysql

我想将HTML表单中的数据提交到数据库表中。我正在使用下面的代码,但是我总是遇到错误,并且没有提交。

HTML:

              <form class="contact_form" action="reminder.php" method="post">
            <div class="row">
              <div class="col-12">
                <div class="form-group row">
                  <label for="name" class="col-sm-3 col-form-label">Meno*</label>
                  <div class="col-sm-9">
                    <input type="text" name="name" class="form-control" placeholder="Meno" required>
                  </div>
                </div>                                        
                <div class="col-12 text-right p-0">
                  <input name="submit" type="submit" class="btn btn-custom-small" value="Odoslať">
                </div>

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

PHP:

<?php 
    $connection = mysqli_connect("server", "tmpco_bc_02", "password", "tmpco_bc_02", 3311);

    if ($connection === false){
      die("Connection failed: " . mysqli_connect_error());
    }

    $name = mysqli_real_escape_string($connection, $_REQUEST['name']);

    $sql = "INSERT INTO reminders (name) VALUES ('$name')";

    if (mysqli_query($connection, $sql)) {
      # Set a 200 (okay) response code.
      http_response_code(200);
      echo '<p class="alert alert-success">Ďakujeme za tvoj záujem. Mailom ti pripomenieme keď bude čas objednať tvoj Birthday Capsule.</p>';
    } else {
      # Set a 500 (internal server error) response code.
      http_response_code(500);
      echo '<p class="alert alert-warning">Error: tvoju žiadosť sa nepodarilo odoslať. Prosím skús to znova.</p>';
    }
    mysqli_close($connection);
?>

MYSQL: 表是具有ID |名称|电子邮件|名人名称|生日的“提醒”

提交后,我总是得到http_response_code(500)

我尝试查找任何错误,但无法解决。知道可能是什么问题,如何解决?

0 个答案:

没有答案