PHP MySQL表单 - 保存到具有可查看结果的数据库

时间:2018-05-04 22:57:31

标签: php mysql forms

编程新手,不知道我做错了什么,我昨晚在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页 - process-form-data.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

0 个答案:

没有答案