Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

What i am doing wrong here ?

whenever i try to insert data i got errors. How to solve this I want to insert data through prepare statement.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1' in C:\xampp\htdocs\Authentication\request\register.php:16 Stack trace: #0 C:\xampp\htdocs\Authentication\request\register.php(16): PDOStatement->execute() #1 C:\xampp\htdocs\Authentication\request\register.php(48): submit(Array) #2 {main} thrown in C:\xampp\htdocs\Authentication\request\register.php on line 16
<?php
require 'db.php';

   function submit($data){
        $column = implode(",", array_keys($data));
        $values = "'".implode("','", $data)."'";

        try{
          global $db;
          $insert = $db->prepare("insert into user_data($column) values(?)");
          $insert -> bindParam(1,$values); 
        }catch(Exception $e){
            $e->getMessage();
        }
        $insert->execute();

   }

  if(isset($_POST) && !empty($_POST)){

     $userName = filter_input(INPUT_POST, 'userName' ,FILTER_SANITIZE_STRING);
     $email = filter_input(INPUT_POST, 'email' ,FILTER_SANITIZE_STRING);
     $password = filter_input(INPUT_POST, 'password' ,FILTER_SANITIZE_STRING);
     $confirm = filter_input(INPUT_POST, 'confirm' ,FILTER_SANITIZE_STRING);



     if($userName == "" || $email == "" || $password == "" || $confirm == ""){
        echo '<span class="error">Empty Fields</span>';
     }elseif(strlen($userName) < 4){
        echo '<span class="error">Username must be greater than 4 characters</span>';
     }elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)){
        echo '<span class="error">Invalid email address</span>'; 
     }elseif($password < 8){
        echo '<span class="error">Password must be more than 8 characters</span>';
     }elseif($password !== $confirm){
        echo '<span class="error">Password not match</span>';
     }else{
        $cost = [md5($userName)."12"];
        $password = password_hash($password,PASSWORD_DEFAULT,$cost);
        $data = [
            'userName' => $userName,
            'email'    => $email,
            'password' => $password
        ];

        submit($data);

     }


  }

1 Answer

Henrique Voni
Henrique Voni
12,296 Points

Please check if your table in database has all the fields you are trying to save using the INSERT query. Your fields must be in the same order as you insert also.