参数错误,SQLSTATE [HY093]:无效的参数号:未定义参数

时间:2019-05-29 04:43:44

标签: php mysql forms parameters

所以我得到SQLSTATE [HY093]:无效的参数号:尝试提交表单时未定义参数。我有一个带有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
}

1 个答案:

答案 0 :(得分:2)

insert语句的语法已关闭,并且似乎是插入和更新之间的混合。试试这个版本:

$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语句提供值,但是您没有使用此表单。