-6ren">
gpt4 book ai didi

php - 使用下拉菜单和 session 变量的 MYSQL 插入

转载 作者:太空宇宙 更新时间:2023-11-04 16:20:29 27 4
gpt4 key购买 nike

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

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 提交...”信号。

然后您在表单中使用 POST 方法,但使用 GET 数组,它们应该是 POST。

$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,而不是另一个不会与您的 PDO 查询混合的 MySQL API。

另一件事;确保您已经开始 session ,因为您正在使用 session 数组。

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

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

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

关于php - 使用下拉菜单和 session 变量的 MYSQL 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com