首页 > 解决方案 > PHP MySQL 表单 - 以可查看的结果保存到数据库

问题描述

编程新手,不知道自己做错了什么,昨晚我在 Google 和 stackoverflow 上进行了广泛搜索,并认为我会在今天早上再次解决它。

第 1 页 - index.php(仅限表单);

  <form class="cover p-3" method="post" action="process-form-data.php">
    <h1 class="mb-4 text-center">Make a Request</h1>
    <p class="my-4">Please use as much information as possible. A Phone Number and/or Customer Email is
      <b>
        <u>required</u>&nbsp;</b>to submit.</p>
    <div class="form-group">
      <label>Date</label>
      <input type="date" id="date_d" name="date_d"></div>
    <div class="form-group">
      <label>Staff Member</label>
      <input type="text" class="form-control" placeholder="Who is filling this out?" id="staff_member" name="staff_member" value="staff_member"> </div>
    <div class="form-group">
      <label>Item/Request</label>
      <textarea class="form-control" placeholder="Request of Customer" rows="5" name="request" id="request"></textarea>
    </div>
    <div class="form-group">
      <label>Comments</label>
      <textarea class="form-control" placeholder="Additional Comments of Customer or Staff" rows="5" name="comments" id="comments"></textarea>
    </div>
    <div class="form-group">
      <label>Customer Name</label>
      <input type="text" class="form-control" placeholder="First &amp; Last Name" size="8" name="customer_name" id="customer_name"> </div>
    <div class="form-group">
      <label>Phone Number</label>
      <input type="number" class="form-control" placeholder="Customers Contact Number" name="phone_number" id="phone_number"> </div>
    <div class="form-group">
      <label>Email Address</label>
      <input type="email" class="form-control" placeholder="Customers Email Address" name="email" id="email"> </div>
    <button type="submit" class="btn mt-4 btn-block p-2 btn-outline-primary" value="submit" id="submit">
      <b>Submit</b>
    </button>
  </form>

第 2 页 - 流程表单数据.php

<?php
$servername = "localhost";
$username = "********";
$password = "********";
$dbname = "********";

$conn = new mysqli($servername, $username, $password,$dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$date_d = $_POST['date_d'];
$staff_member = $_POST['staff_member'];
$request = $_POST['request'];
$comments = $_POST['comments'];
$customer_name = $_POST['phone_number'];
$email = $_POST['email'];

$sql = "INSERT INTO form_test (id, date_d, staff_member, request, comments, customer_name, phone_number, email)";
VALUES ('".$date_d."', '".$staff_member."', '".$request."', '".$comments."', '".$customer_name."', '".$phone_number."', '".$email."');

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

该数据库名为 form_test,有 8 列(1 是唯一 ID)。此处显示 https://imgur.com/a/o5Yw6Hu

错误.log -

[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: date_d in /home/itcomau2/public_html/beta_form/process-form-data.php on line 13
[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: staff_member in /home/itcomau2/public_html/beta_form/process-form-data.php on line 14
[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: request in /home/itcomau2/public_html/beta_form/process-form-data.php on line 15
[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: comments in /home/itcomau2/public_html/beta_form/process-form-data.php on line 16
[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: phone_number in /home/itcomau2/public_html/beta_form/process-form-data.php on line 17
[04-May-2018 22:48:40 UTC] PHP Notice:  Undefined index: email in /home/itcomau2/public_html/beta_form/process-form-data.php on line 18
[04-May-2018 22:48:40 UTC] PHP Fatal error:  Call to undefined function VALUES() in /home/itcomau2/public_html/beta_form/process-form-data.php on line 21

标签: phpmysqlforms

解决方案


推荐阅读