MYSQL 使用下拉菜单和会话变量插入

MYSQL Insert using Dropdowns and Session Variables

我已经尝试解决这个问题几个小时了,但似乎没有取得进展。我正在创建一个预订表格,它涉及 2 个下拉菜单和一些会话变量的使用。

HTML

<form accept-charset="UTF-8" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method="POST">
    <input type="date" data-role="date" name = "Date"data-inline="true" id ="Date" placeholder="Click here to select a date">

   <br>
    <select id="Time" name="Time">
        <option value="Null">Select a Time</option>
        <option value="9am">09:00</option>
        <option value="9.20am">09:20</option>
        <option value="9.40am">09:40</option>
        <option value="10am">10:00</option>
        <option value="10.20am">10:20</option>
        <option value="10:40am">10:40</option>
        <option value="11am">11:00</option>
        <option value="11:20am">11:20</option>
        <option value="11:40am">11:40</option>
        <option value="12am">12:00</option>
    </select>
  <br>
    <select id="Person" name="Person">
        <option value="Person1">Who Would you Like to See?</option>
        <option value="Person2">A Doctor</option>
        <option value="Person3">A Nurse</option>  
    </select>
    <br>
    <input type="submit" data-role="button" id="submit" value="Book" data-icon="action" data-iconpos="right">

我没有给出错误消息,也没有收到我在查询成功时编码的成功消息。任何帮助将不胜感激

PHP

//This adds the connection file which has the details to connect to the database and what database to connect to
include_once('connection.php');
//Checks if the submit button is not set
if(!isset($_POST['submit']))
{
exit;
}
    //Declared Variables
    $Date = $_GET['Date'];
            $Time = $_GET['Time'];
            $Person = $_GET['Person'];
    $userID= $_SESSION['user'];
    $GPID= $_SESSION['GPID'];


    //Database Connection, this connects to the database using the connection.php
    $conn=ConnectionFactory::connect();

    //Insert Query
    $query="INSERT INTO `Appointments`(`AppID`, `Date`, `Time`, `Booked_With`, `UserID`, `GPID`) VALUES (NULL, :Date,:Time,:Person,:userID,:GPID)";

    $stmt=$conn->prepare($query);

    //Binding values
    //$stmt->bindValue(':post', $Post);
    $stmt->bindValue(':Date', $Date);
    $stmt->bindValue(':Time', $Time);
    $stmt->bindValue(':Person', $Person);
    $stmt->bindValue(':userID', $userID);
    $stmt->bindValue(':GPID', $GPID);

    $affected_rows = $stmt->execute();

    //Message if insert is Successful
    if($affected_rows==1){
        print "<script type=\"text/javascript\">"; 
        print "alert('Post Successful')"; 
        print "</script>";
        exit;
        }

    //Terminates the connection
            $conn=NULL;
?>
     </form>  

您的代码存在一些问题,我将从头开始。

这个条件语句:

if(!isset($_POST['submit']))
{
exit;
}

您没有带有 submit name 属性的元素,因此您的脚本将在页面加载后立即退出。

您可能依赖 "id" 作为您的提交按钮:

<input type="submit" data-role="button" id="submit" ...

并且应该被命名。

即:

<input type="submit" name="submit" data-role="button" id="submit" ...
  • 使用 error reporting,会立即发出 "Undefined index submit..." 信号。

那么您在表单中使用了 POST 方法,但使用的是 GET 数组,它们应该是 POSTs.

$Date = $_GET['Date'];

$Date = $_POST['Date'];
  • 然后,将其余的 GET 数组更改为 $_POST

error reporting 添加到您的文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告只能在试运行中进行,绝不能在生产中进行。

此外,请确保您确实在连接 PDO 而不是另一个 MySQL API 不会与您的 PDO 查询混合。

另一件事;由于您使用的是会话数组,因此请确保您已启动会话。

session_start(); 必须驻留在所有使用会话的页面中。

连接打开后立即添加$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  • 如果仍然无法启动,您可能还想使用 bindParam 而不是 bindValue