Invalid parameter number: parameter was not defined in PDO PHP

1

Good day community, I have this problem when wanting to save data in my database, I am creating a SignUp, but since adding "FDia", "FMes" and "Fño" to my form and it is stored by PHP , throw me that error, and investigate other related questions and nothing.

  

Fatal error: Uncaught PDOException: SQLSTATE [HY093]: Invalid parameter   number: parameter was not defined in   C: \ xampp \ htdocs \ AureaEdu \ classes \ DB.php: 10 Stack trace: # 0   C: \ xampp \ htdocs \ AureaEdu \ classes \ DB.php (10):   PDOStatement-> execute (Array) # 1   C: \ xampp \ htdocs \ AureaEdu \ RegAlumn.php (12): DB :: query ('INSERT INTO   reg ... ', Array) # 2 {main} thrown in   C: \ xampp \ htdocs \ AureaEdu \ classes \ DB.php on line 10

This PHP

<?php
include('classes/DB.php');
if (isset($_POST['createaccount'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $genero = $_POST['genero'];
        $FDia = $_POST['FDia'];
        $FMes = $_POST['FMes'];
        $FAño = $_POST['FAño'];

        DB::query('INSERT INTO registroalumnos 
                   VALUES (
                            null, 
                            :name, 
                            :email, 
                            :password, 
                            :genero, 
                            :FDia, 
                            :FMes, 
                            :FAño)',
                    array(
                            ':name'=>$name, 
                            ':email'=>$email, 
                            ':password'=>$password, 
                            ':genero'=>$genero, 
                            ':FDia'=>$FDia, 
                            ':FMes'=>$FMes, 
                            ':FAño'=>$FAño)
                          );
        echo "Success!";

And here is where the arrangement is:

    <?php class DB {
        private static function connect() {
                $pdo = new PDO('mysql:host=127.0.0.1;dbname=aureaedu;charset=utf8', 'root', '1592854Aa');
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $pdo;
        }
        public static function query($query, $params = array()) {
                $statement = self::connect()->prepare($query);
                $statement->execute($params);
                // $data = $statement->fetchAll();
                // return $data;
        }
    }
    
asked by Antonio Alejos 27.01.2018 в 21:25
source

1 answer

3

The error could be solved simply by changing the name of your variable, the name of the field in your table and the attribute name of input that brings the year from HTML

//En Tabla SQL , las Variable PHP y en el name de su input en su HTML también
FAño -> FAnio  

Code

$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$genero = $_POST['genero'];
$FDia = $_POST['FDia'];
$FMes = $_POST['FMes'];
$FAnio = $_POST['FAnio'];

$query = 'INSERT INTO registroalumnos VALUES (null, :name, :email,:password,
           :genero, :FDia, :FMes, :FAnio)';
$params  = array(':name'=>$name, ':email'=>$email, ':password'=>$password, 
           ':genero'=>$genero, ':FDia'=>$FDia, ':FMes'=>$FMes, ':FAnio'=>$FAnio );

DB::query($query, $params); 
  

As I mentioned in the comments, you should avoid using the lyrics    ñ for variables and name of fields, Review

    
answered by 27.01.2018 / 22:25
source