首页 > 解决方案 > 表单数据未在 MySql 数据库中提交

问题描述

我在MySql数据库中提交数据时遇到问题,我的代码没有错误,但是当我检查数据库时,似乎表中没有提交数据

这是我的 php 代码

if(isset($_GET['date'])){
    $date = $_GET['date'];
}
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $timeslot = $_POST['timeslot'];
    $mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
    $stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
    $stmt->bind_param('ssss', $name, $email, $date, $timeslot);
    $stmt->execute();
    $msg = "<div>Booking Successfully</div>";
    $stmt->close();
    $mysqli->close();
}

这是表格

<form action="" method="post">
  <h4>Booking: <p id="slot"></p>
  </h4>

  <label> Name </label>
  <input required type="text" name="name">
  <label> Email </label>
  <input required type="email" name="email">
  <label> TIMESLOT </label>
  <input required type="text" readonly name="timeslot" id="timeslot">
  <button type="submit">
    SUBMIT
  </button>

</form>

代码没有分开,当我单击“提交”按钮时,它看起来正在工作,但是当我检查数据库时没有提交数据。

非常感谢您的帮助!

标签: phpmysql

解决方案


您忘记提供名称提交按钮并检查isset($_POST['submit'])这就是如果条件未运行内部代码的原因。

if(isset($_GET['date'])){
    $date = $_GET['date'];
}

if(isset($_POST['submit'])){
   //print_r($_POST);
    $name = $_POST['name'];
    $email = $_POST['email'];
    $timeslot = $_POST['timeslot'];
    $mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
    $stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
    $stmt->bind_param('ssss', $name, $email, $date, $timeslot);
    $stmt->execute();
    $msg = "<div>Booking Successfully</div>";
    $stmt->close();
    $mysqli->close();
}
 ?>
<form action="" method="post">
  <h4>Booking: <p id="slot"></p>
  </h4>

  <label> Name </label>
  <input required type="text" name="name">
  <label> Email </label>
  <input required type="email" name="email">
  <label> TIMESLOT </label>
  <input required type="text" readonly name="timeslot" id="timeslot">
  <button type="submit" name="submit">
    SUBMIT
  </button>

</form>

推荐阅读