在我的PHP代码中遇到参数问题,参数未定义

时间:2015-04-01 23:56:52

标签: php mysql pdo

尝试构建一个表单以将项目添加到数据库,但每次单击“提交”时都会抛出错误,并且我无法发现错误。

我正在使用的代码,它为第52行抛出错误,其中包含'));'而且我试图改变它,没有运气。任何帮助/指导将不胜感激。

错误消息'警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未定义'

<?php

if(isset($_POST['submit'])){

    // Field validation
    if(strlen($_POST['groupname']) == ''){
        $error[] = 'Please enter group/individual name';
    }

    if(strlen($_POST['address1']) == ''){
        $error[] = 'Please enter first line of address';
    }

    if(strlen($_POST['city']) < 2){
        $error[] = 'City field too short.';
    } 
    if(strlen($_POST['postcode']) < 7){
        $error[] = 'Please enter valid postcode.';
    }
    if(strlen($_POST['phone']) < 11){
        $error[] = 'Please enter valid contact number.';
    }
    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
        $error[] = 'Please enter a valid email address';
    } else {
        $stmt = $con->prepare('SELECT email FROM groups WHERE email = :email');
        $stmt->execute(array(':email' => $_POST['email']));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if(!empty($row['email'])){
            $error[] = 'Email provided is already in use.';
        }


        //If no errors - proceed
        if(!isset($error)){

            try {

                //insert into database with a prepared statement
                $stmt = $con->prepare('INSERT INTO groups (groupName,address1,address2,city,postcode,phoneNumber,email,coOrdinator)
                                VALUES (:groupname, :address1, :address2, :city, :postcode, :phone, :email, :co-ordinator)');
                $stmt->execute(array(
                                     ':groupname' => $_POST['groupname'],
                                     ':address1' => $_POST['address1'],
                                     ':address2' => $_POST['address2'],
                                     ':city' => $_POST['city'],
                                     ':postcode' => $_POST['postcode'],
                                     ':phone' => $_POST['phone'],
                                     ':email' => $_POST['email'],
                                     ':co-ordinator' => $_POST['co-ordinator']
                                     ));

                if ( $stmt ){
                    echo "<p>Kit has been added successfully!</p>";
                } else {
                    echo "<p>Sorry, there has been a problem adding the item.</p>";
                }


                exit();

                //else catch the exception and show the error.
            } catch(PDOException $e) {
                $error[] = $e->getMessage();
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

占位符中不能包含连字符,它们的语法与SQL标识符相同(:前缀除外)。将:co-ordinator更改为:coordinator