php - 参数错误,SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
问题描述
所以我得到 SQLSTATE[HY093]: Invalid parameter number: parameter was not defined,当我尝试提交我的表单时。我有一个带有 index.php 文件的 reservations 文件夹,该文件有一个包含文件作为 reservations.html.php,其中包含 html 中的表单。
因此,我在reservations.html.php 中的表单在填写并在名字中有一个值时,将尝试将表单中的所有值发布到我在mysql 中创建的reservations 表中。下面是我在 index.php 中的代码
<?php
// Edit or Replace this try/catch statement to work with the current PHT configuration
include '../includes/db.inc.php';
// Modify the If statement so the try only runs if the First Name field has been submitted AND the honeypot field is empty ''
if (isset($_POST['myfname'])) {
$myFName = $_POST['myfname'];
$myTour = $_POST['tour'];
$myLName = $_POST['mylname'];
$myEmail = $_POST['myemail'];
// If the if statement is true, save each form field value as a variable. These variable values will be used in the thank you page.
// And run the try/catch to attempt to insert data in the database. Modify the INSERT statement to write all the form filed values (except the honeypot) to the database.
try
{
$sql = 'INSERT INTO reservations SET
tour = :tour,
fname = :fname,
lname = :lname,
email = :email';
$s = $pdo->prepare($sql);
$s->bindValue(':tour', $myTour);
$s->bindValue(':myfname', $myFName);
$s->bindValue(':mylname', $myLName);
$s->bindValue(':myemail', $myEmail);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted joke: ' . $e->getMessage();
include '../includes/error.html.php';
exit();
}
// load the thank you page after the INSERT runs
include 'success.html.php';
// Add an else to load the initial page if the initial (line 19) if statement is false
} else {
include 'reservations.html.php'; //Modify this to include the initial file for this folder
}
解决方案
您的插入语句的语法已关闭,并且似乎是插入和更新之间的混合。试试这个版本:
$sql = "INSERT INTO reservations (tour, fname, lname, email) ";
$sql .= "VALUES (:tour, :fname, :lname, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':tour', $myTour, PDO::PARAM_STR);
$stmt->bindParam(':fname', $myFName, PDO::PARAM_STR);
$stmt->bindParam(':lname', $myLName, PDO::PARAM_STR);
$stmt->bindParam(':email', $myEmail, PDO::PARAM_STR);
$stmt->execute();
$stmt->close();
为了清楚起见,SQL 插入语句需要以下内容:
INSERT INTO
关键字,后跟列列表- 然后是一个 VALUES 子句,后跟一个包含要插入的值的元组
还有一个INSERT INTO ... SELECT
,它使用 select 语句来提供值,但您没有使用这种形式。
推荐阅读
- javascript - C# JSON 反序列化为复杂类
- angular - Angular 6 - AnguarFire - 使用多个文档字段查询集合
- android - startForeground 的频道无效
- python - 帮助部分中列为可选的必需互斥参数是否有解决方案?
- python-3.x - 如何在python中将谷歌分析行增加超过10000
- python - 我需要帮助在 python 中创建菜单
- reactjs - 尝试使用 TypeScript 在另一个类中导入时反应 createElement 错误
- php - 如何根据codeigniter中的url重定向页面?
- swift - 如何允许用户使用 Firebase 中的任何电子邮件地址进行访问?
- android - 如何使用折叠工具栏在协调器布局中固定工具栏?