Home > Net >  SQL Insert value list does not match column list error
SQL Insert value list does not match column list error

Time:04-16

I have error: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

here is my function for inserting

in phpmyadmin insert:

INSERT INTO `najomnik`
            (`id_najomnik`, `meno`, `priezvisko`, 
            `rodne_cislo`, `telefon`, `mail`) 
    VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6])

id_najomnik is int

public function insert($meno, $priezvisko, $rodne_cislo, $telefon, $mail) 
{
    try{
        $sql = "INSERT INTO `najomnik` 
                            (meno,priezvisko,rodne_cislo,telefon,mail) 
                    VALUES ('meno, priezvisko,r odne_cislo, telefon, mail')";
        $stmt = $this->db->prepare($sql);
        $stmt->execute();
        return true;
    } catch(PDOException $e) {
        echo $e->getMessage();
        return false;
    }
}

this is the form i am inserting values into

<?php
require_once 'nav.php';
require_once 'conn.php';

?>

<form method="post" action="success.php" enctype="multipart/form-data">
        <div >
            <label for="firstname">First Name</label>
            <input required type="text"  id="meno" name="meno">
        </div>
        <div >
            <label for="lastname">Last Name</label>
            <input required type="text"  id="priezvisko" name="priezvisko">
        </div>
        <div >
            <label for="dob">Date Of Birth</label>
            <input type="text"  id="rodne_cislo" name="rodne_cislo">
        </div>
        <div >
            <label for="email">Email address</label>
            <input required type="email"  id="mail"  name="mail" aria-describedby="emailHelp" >
            <small id="emailHelp" >We'll never share your email with anyone else.</small>
        </div>
        <div >
            <label for="phone">Contact Number</label>
            <input type="text"  id="telefon" name="telefon" aria-describedby="phoneHelp" >
            <small id="phoneHelp" >We'll never share your number with anyone else.</small>
        </div>
        <br/>
        
        
        <button type="submit" name="submit" >Submit</button>
    </form>

i've tried to change it

       public function insert($meno, $priezvisko, $rodne_cislo, $telefon, $mail) {
            try{
                $sql = "INSERT INTO `najomnik` (meno,priezvisko,rodne_cislo,telefon,mail) VALUES (':meno, :priezvisko, :rodne_cislo, :telefon, :mail')";
                $stmt = $this->db->prepare($sql);

                $stmt->bindparam(':meno', $meno);
                $stmt->bindparam(':priezvisko', $priezvisko);
                $stmt->bindparam(':rodne_cislo', $rodne_cislo);
                $stmt->bindparam(':telefon', $telefon);
                $stmt->bindparam(':mail', $mail);

                $stmt->execute();
                return true;
            } catch(PDOException $e) {
                echo $e->getMessage();
                return false;
        }
    }

but in this case i am getting error: Invalid parameter number: number of bound variables does not match number of tokens

CodePudding user response:

you shouldn't have quotes around the list of values.

And each value needs to be a placeholder, which you then provide with values when you call $stmt->execute().

public function insert($meno, $priezvisko, $rodne_cislo, $telefon, $mail) {
    try{
        $sql = "INSERT INTO `najomnik` (meno,priezvisko,rodne_cislo,telefon,mail) VALUES (:meno, :priezvisko, :rodne_cislo, :telefon, :mail)";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':meno' => $meno,
            ':priezvisko' => $priezvisko,
            ':rodne_islo' => $rodne_islo,
            ':telefon' => $telefon,
            ':mail' => $mail
        ]);
        return true;
    } catch(PDOException $e) {
        echo $e->getMessage();
        return false;
    }
}

CodePudding user response:

i copied it but now there is error: Invalid parameter number: number of bound variables does not match number of tokens

  • Related