0

I'm new about the PHP. I create a login form and after I fill up all the input fields. I clicked "SUBMIT" and then it says.. "Column count doesn't match value count at row 1". anybody help me about this. I'm stack with this almost 1 week.. here is my code. and my db connection.

<?php
    $connection = mysqli_connect("localhost","root","","db_violation");
    function DBCon(){
        try{
            $connection = new PDO("mysql:host=localhost;port=3306;dbname=db_violation;","root","");
            return $connection;
        }catch(PDOException $e){echo "Connection failed: ".$e->getMessage();}
    }function DBDiscon(){return $connection = null;}
?>

<?php
        include '../../PROCESS/dbconnect/dbconnect.php';

        $datauserssystems = json_decode(file_get_contents("php://input"));

            $user_firstname = mysqli_real_escape_string($connection, $datauserssystems->user_fname);
            $user_middlename = mysqli_real_escape_string($connection, $datauserssystems->user_mname);
            $user_lastname = mysqli_real_escape_string($connection, $datauserssystems->user_lname);
            $user_address = mysqli_real_escape_string($connection, $datauserssystems->user_address);
            $user_positionss = mysqli_real_escape_string($connection, $datauserssystems->user_positions);
            $user_age = mysqli_real_escape_string($connection, $datauserssystems->user_age);
            $user_username = mysqli_real_escape_string($connection, $datauserssystems->user_uname);
            $user_userpass = mysqli_real_escape_string($connection, $datauserssystems->user_pwd);

            $tableRows = DBCon()->prepare("SELECT COUNT (*) AS row FROM sb_account");
            $tableRows->execute();
            $count = $tableRows->fetch(); 
            $count = ($count["row"]+1);
            $gain ="SbAdmin";
            $encrpt = md5($gain.$user_userpass);
            $security = DBCon()->prepare("INSERT INTO sb_account_security VALUES('".$count."','".$encrpt."','".$user_userpass."')")->execute();

            $sbuserssssquery = mysqli_query($connection, "SELECT * FROM sb_account WHERE sb_username='$user_username'");
            $sbuserssssarray = mysqli_fetch_array($sbuserssssquery);
            $sbuserssss = $sbuserssssarray['sb_username'];

            if($sbuserssss == $user_username) {
                echo "sbtwice";
                die();
            }
            else {
            mysqli_query($connection, "INSERT INTO sb_account (sb_firstname,sb_middlename,sb_lastname,sb_fullname,sb_address,sb_position,sb_age,sb_username,sb_password)  VALUES ('$user_firstname','$user_middlename','$user_lastname','$user_address','$user_positionss','$user_age','$user_username','$encrpt')");
            if(mysqli_affected_rows($connection) > 0) {
                echo "AddUserSystem";
                die();
            }
            else {
                echo mysqli_error($connection);
                die();
            }
            }
    ?>
  • Does this answer your question? [PHP ~ Column count doesn't match value count at row 1](https://stackoverflow.com/questions/11989112/php-column-count-doesnt-match-value-count-at-row-1) – Don't Panic Apr 19 '20 at 03:59
  • Never add any login or db credentials in your post!! The connection that you specified is for your local database so anyways no one would be able to access it over the net. Just something to keep in mind when dealing with prod application. – Ashish Patel Apr 19 '20 at 03:59
  • The error is pretty clear. It says you have more columns than values. Look at your 2nd `INSERT` - you list 9 column names, and then give 8 values. Also - [**do not use `md5` for passwords!!**](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords). – Don't Panic Apr 19 '20 at 04:02
  • 1
    There is a *lot* wrong with your code, starting from the fact that you're using both `mysqli` and `PDO` - use one or the other, not both. Then you're using `prepare` yet still injecting values rather than using parameters... `DBDiscon()` does absolutely nothing as it creates a new local variable, sets it to `null` and returns it... It's a mess honestly. – Niet the Dark Absol Apr 19 '20 at 04:05

1 Answers1

0

Your mysql insert query has issue:

INSERT INTO sb_account 
            (sb_firstname, 
             sb_middlename, 
             sb_lastname, 
             sb_fullname, 
             sb_address, 
             sb_position, 
             sb_age, 
             sb_username, 
             sb_password) 
VALUES      ('$user_firstname', 
             '$user_middlename', 
             '$user_lastname', 
             '$user_address', 
             '$user_positionss', 
             '$user_age', 
             '$user_username', 
             '$encrpt');

Here you have 9 columns but are supplying 8 values. You are not passing value for sb_fullname column.

Ashish Patel
  • 442
  • 3
  • 8