繁体   English   中英

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

[英]Parameter error, SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

所以我得到SQLSTATE [HY093]:无效的参数号:当我尝试提交表单时,参数未定义。 我有一个带有index.php文件的reservations文件夹,该文件具有一个包含文件作为reservations.html.php,其中包含html形式。

因此,当我在Reservations.html.php中的表单填写好并具有名字的值时,它将尝试将表单中的所有值发布到我在mysql中创建的保留表中。 下面是我在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
}

您的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语句来提供值,但是您没有使用这种形式。

暂无
暂无

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

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