簡體   English   中英

表單數據未在 MySql 數據庫中提交

[英]Form data is not submitting in MySql database

我在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>

代碼沒有分開,當我點擊“提交”按鈕時,它看起來像是在工作,但是當我檢查數據庫時,沒有提交數據。

非常感謝您的幫助!

if (isset($_GET['date'])){
    $date = $_GET['date'];
    // Every code of php should be between here.
}

請記住,單引號用於SQL 字符串,反引號用於列或表等數據庫實體。 您在這里使用了錯誤的引號。

這是更正后的版本:

$stmt = $mysqli->prepare("INSERT INTO `bookings` (`name`, `email`, `date`, `timeslot`) VALUES (?,?,?,?)");

請注意,除非您與MySQL 保留關鍵字發生沖突,否則您不需要反引號。 在這種情況下,只有date是沖突的:

$stmt = $mysqli->prepare("INSERT INTO bookings (name, email, `date`, timeslot) VALUES (?,?,?,?)");

試試這個簡單的解決方案

<button type="submit" name="submit">
          SUBMIT
</button>

嘗試這個:

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $timeslot = $_POST['timeslot'];
    $date=$_GET['date'];
    $mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
    $stmt = "insert into bookings(name, email, data,timeslot) values('$name','$email','$date','$timeslot');";
    if($mysqli->query($stmt))
    {
       echo 'data saved successfully';
    }
} 

您忘記指定名稱提交按鈕並檢查isset($_POST['submit'])這就是 if 條件未運行的原因。

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM