简体   繁体   English

使用下拉列表和会话变量进行MYSQL插入

[英]MYSQL Insert using Dropdowns and Session Variables

I've been trying to solve this problem for a few hours and can't seem to make headway. 我已经尝试解决这个问题了几个小时,似乎并没有取得进展。 I am creating a booking form and it involves 2 dropdown menus and the use of some session variables. 我正在创建一个预订表单,其中包含2个下拉菜单和一些会话变量的使用。

HTML 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">

I'm not been giving an error message, nor am I getting the success message that I've coded in if the query is successful. 如果查询成功,我没有给出错误消息,也没有得到编码的成功消息。 Any help would be appreciated 任何帮助,将不胜感激

PHP 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>  

There are a few issues with your code and I'll start from the top. 您的代码有一些问题,我将从头开始。

This conditional statement: 此条件语句:

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

You don't have an element bearing the submit name attribute, therefore your script will exit as soon as the page is loaded. 您没有带有submit name属性的元素,因此,脚本将在页面加载后立即退出。

You're probably relying on the "id" for your submit button being: 您可能依赖于“ id”作为提交按钮:

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

and should be named. 并应命名。

Ie: 即:

<input type="submit" name="submit" data-role="button" id="submit" ...
  • Having used error reporting , would have signaled an "Undefined index submit..." right away. 使用错误报告功能后 ,将立即发出“未定义索引提交...”的信号。

Then you're using a POST method in your form, but using GET arrays, where they should be POSTs. 然后,您将在表单中使用POST方法,但使用GET数组,它们应为POST。

$Date = $_GET['Date'];

to

$Date = $_POST['Date'];
  • Then, change those to $_POST for the rest of your GET arrays. 然后,将其余的GET数组更改为$_POST

Add error reporting to the top of your file(s) which will help find errors. 错误报告添加到文件顶部,这将有助于发现错误。

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

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production. 旁注:错误报告仅应在登台进行,而不应在生产过程中进行。

Plus, make sure you are indeed connecting with PDO and not another MySQL API that doesn't intermix with your PDO query. 另外,请确保您确实与PDO连接,而不要与另一个不会与PDO查询混合的MySQL API连接。

Another thing; 另一件事; make sure you've started the session since you are using a session array. 请确保您已开始使用会话,因为您正在使用会话数组。

session_start(); must reside inside all pages using sessions. 必须使用会话驻留在所有页面内。

Add $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 添加$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened. 在连接打开之后。

  • You may also want to use bindParam instead of bindValue if that still doesn't make it kick in. 您可能还想使用bindParam而不是bindValue如果仍然不能bindValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM