Okay, so I'm getting a MySQL Error that says "Column count doesn't match value count at row 1". I'm not sure what the problem is. Here is the code

<?php

    if( !preg_match( "/index.php/i", $_SERVER['PHP_SELF'] ) ) { die(); }

    if( $_GET['id'] ) {

        $id = $core->clean( $_GET['id'] );

        $query = $db->query( "SELECT * FROM users WHERE id = '{$id}'" );
        $data  = $db->assoc( $query );

        $data['ugroups'] = explode( ",", $data['usergroups'] );

        $editid = $data['id'];

    }

?>
<form action="" method="post" id="addUser">

    <div class="box">

        <div class="square title">
            <strong>Add user</strong>
        </div>

        <?php

            if( $_POST['submit'] ) {

                try {

                    $username = $core->clean( $_POST['username'] );
                    $password = $core->clean( $_POST['password'] );
                    $email    = $core->clean( $_POST['email'] );
                    $habbo    = $core->clean( $_POST['habbo'] );
                    $dgroup   = $core->clean( $_POST['dgroup'] );
                                        $banned   = $core->clean( $_POST['banned'] );
                    $warnings = $core->clean( $_POST['totalWarnings'] );
                    $infractions = $core->clean( $_POST['totalInfractions'] );

                    $query    = $db->query( "SELECT * FROM usergroups" );

                    while( $array = $db->assoc( $query ) ) {

                        if( $_POST['ugroup-' . $array['id']] ) {

                            $ugroups .= $array['id'] . ",";

                        }

                    }

                    $password_enc = $core->encrypt( $password );

                    if( !$username or ( !$password and !$editid ) or !$dgroup or !$ugroups ) {

                        throw new Exception( "All fields are required." );

                    }
                    else {

                        if( $editid ) {

                            if( $password ) {

                                $password = ", password = '{$password_enc}'";

                            }
                            else {

                                unset( $password );

                            }

                            $db->query( "UPDATE users SET username = '{$username}'{$password}, email = '{$email}', habbo = '{$habbo}', displaygroup = '{$dgroup}', usergroups = '{$ugroups}', banned = '{$banned}', totalInfractions = '{$infractions}', totalWarnings = '{$warnings}' WHERE id = '{$editid}'" );


                        }
                        else {

                            $db->query( "INSERT INTO users VALUES (NULL, '{$username}', '{$password_enc}', '{$email}', '{$habbo}', '{$dgroup}', '{$ugroups}', '{$banned}', '{$infractions}', '{$warnings}', 0);" );

                        }

                        echo "<div class=\"square good\">";
                        echo "<strong>Success</strong>";
                        echo "<br />";
                        echo "User added!";
                        echo "</div>";

                    }

                }
                catch( Exception $e ) {

                    echo "<div class=\"square bad\">";
                    echo "<strong>Error</strong>";
                    echo "<br />";
                    echo $e->getMessage();
                    echo "</div>";

                }

            }

        ?>

        <table width="100%" cellpadding="3" cellspacing="0">
            <?php
            if ($data['banned'] == "1") {
            ?>
            <div style="background: red; border: 1px solid black; padding: 5px;"><strong>User is currently banned!</strong></div>
            <?php
            }

                $query = $db->query( "SELECT * FROM usergroups" );

                while( $array = $db->assoc( $query ) ) {

                    if( in_array( $array['id'], $data['ugroups'] ) ) {

                        $groups[$array['id'] . '_active'] = $array['name'];

                    }
                    else {

                        $groups[$array['id']] = $array['name'];

                    }

                    if( $array['id'] == $data['displaygroup'] ) {

                        $dgroups[$array['id'] . '_active']  = $array['name'];

                    }
                    else {

                        $dgroups[$array['id']]  = $array['name'];

                    }

                }

                $opt_banned = Array (
                            "0" => "Unbanned",
                            "1" => "Banned"
                );

                echo $core->buildField( "text",
                                        "required",
                                        "username",
                                        "Username",
                                        "The new username.",
                                        $data['username'] );

                echo $core->buildField( "password",
                                        "<?php if( !$editid ) { ?>required<?php } ?>",
                                        "password",
                                        "Password",
                                        "The new password." );

                echo $core->buildField( "text",
                                        "",
                                        "email",
                                        "Email",
                                        "The new email (optional).",
                                        $data['email'] );

                echo $core->buildField( "text",
                                        "",
                                        "totalInfractions",
                                        "Infractions",
                                        "Number of infractions the user has",
                                        $data['totalInfractions'] );

                echo $core->buildField( "text",
                                        "",
                                        "totalWarnings",
                                        "Warnings",
                                        "Number of warnings the user has",
                                        $data['totalWarnings'] );

                echo $core->buildField( "text",
                                        "",
                                        "habbo",
                                        "Habbo name",
                                        "The new Habbo name (optional).",
                                        $data['habbo'] );

                echo $core->buildField( "select",
                                        "",
                                        "banned",
                                        "Banned",
                                        "To ban a user, enter 1, thus restricting them from logging in or 0 to not ban.",
                                        $opt_banned );

                echo $core->buildField( "select",
                                        "required",
                                        "dgroup",
                                        "Display group",
                                        "The user's display group.",
                                        $dgroups );

                echo $core->buildField( "checkbox",
                                        "required",
                                        "ugroup",
                                        "Active usergroups",
                                        "The user's active groups.",
                                        $groups );


            ?>
        </table>

    </div>

    <div class="box" align="right">

        <input class="button" type="submit" name="submit" value="Submit" />

    </div>

</form>

<?php
    echo $core->buildFormJS('addUser');

?>

So any idea whats wrong in this? Please help.

Member Avatar for diafol

UPDATE or INSERT query has the wrong number of columns.

hello please do cross check the number of column in the particular table you are dealing with and insert or update code that is trying to insert into the table. these are the kind of errors that gave me this kind of message. thanks.gr8 day as you work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.