Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:01:48+00:00 2026-06-06T20:01:48+00:00

The database table only contains the four fields that the query is attempting to

  • 0

The database table only contains the four fields that the query is attempting to insert into. For some reason I get the error: Query failed: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined.

I have troubleshot by echoing the output of the foreach loops and it always returns four items, I’m not sure what parameter isn’t defined. I have also played around with including the field names in the $sql string as well as not including them. Same results either way. Please help if you can.

<?php
class DB {
        private $_conn;

        public function openDB() {
                $dsn = "mysql:host=localhost;dbname=news";
                $username = "root";
                $password = "password";

                try {
                        $this->_conn = new PDO( $dsn, $username, $password );
                        $this->_conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                } catch ( PDOException $e ) {
                        echo "Connection failed: " . $e->getMessage();
                }
        }

        public function closeDB() {
                $this->_conn = null;
        }

        public function selectData( $myQuery ) {
                $rows = $this->_conn->query( $myQuery );

                foreach ( $rows as $row ) {
                        echo "Index: " . $row['id'] . "<br />";
                        echo "Title: " . $row['title'] . "<br />";
                }
        }

        public function insertData( $tableName ) {

                $q = $this->_conn->prepare("DESCRIBE " . $tableName);
                $q->execute();
                $getFields = $q->fetchAll(PDO::FETCH_COLUMN);

                $dbFieldCount = count( $getFields );
                $implodedFields = implode( ", :", $getFields );

                //$sql = "INSERT INTO " . $tableName . " ( " . implode( ", ", $getFields ) . " ) VALUES ( :" . $implodedFields . " )";
                $sql = "INSERT INTO " . $tableName . " VALUES ( :" . $implodedFields . " )";
                echo "$sql<br />";

                try {
                        $insert = $this->_conn->prepare( $sql );

                        foreach ( $getFields as $dbKey => $dbValue ) {
                                foreach( $_POST as $formKey => $formValue ) {
                                        if ( $dbValue == 'id' ) {
                                                $insert->bindValue( '\":' . $dbValue . '\"', null, PDO::PARAM_INT );
                                                echo "$dbValue<br />";
                                                break;
                                        } else if ( is_int( $formValue ) && $dbValue == $formKey ) {
                                                $insert->bindValue( '\":' . $dbValue . '\"', $formValue, PDO::PARAM_INT );
                                                echo "$formValue<br />";
                                                break;
                                        } else if ( is_string( $formValue ) && $dbValue == $formKey ) {
                                                $insert->bindValue( '\":' . $dbValue . '\"', $formValue, PDO::PARAM_STR );
                                                echo "$formValue<br />";
                                                break;
                                        }
                                }
                        }

                        $insert->execute();
                } catch ( PDOException $e ) {
                        echo "Query failed: " . $e->getMessage();
                }
        }

}
?>
<!DOCTYPE html>
<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title></title>
        </head>
        <body>
                <?php

                if ($_POST) {
                        $conn = new DB();
                        $conn->openDB();
                        $conn->insertData( 'login' );
                        $conn->closeDB();
                }

                ?>

                <form action="#" method="POST" name="register">
                        <label for="username">Username</label><br />
                        <input type="text" id="username" name="username"><br />
                        <label for="password">Password</label><br />
                        <input type="password" id="password" name="password"><br />
                        <label for="email">Email Address</label><br />
                        <input type="text" id="email" name="email"><br />
                        <input type="submit" value="Submit" />
                </form>

        </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:01:49+00:00Added an answer on June 6, 2026 at 8:01 pm
    $sql = "INSERT INTO " . $tableName . " VALUES ( :" . $implodedFields . " )";
    

    Here you’re adding all columns into your SQL statement, but later you only add values that are sent when the form is submitted. It’s possible that you have columns in your database that aren’t in the form, so you’re coming up with a statement like:

    INSERT INTO someTable VALUES (:id, :value1, :value2)
    

    And then you only bind :id and :value1, leaving MySQL confused about what :value2 is supposed to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table (ff_projections) that contains the following fields: ID Player Position
I have a database table that contains some records to be processed. The table
I have a database table that contains among other things partial postal codes. I'm
When creating a database what happens if one table only contains 2 primary keys
Lets say I have a simple table that only contains two columns: MailingListUser -
I have a database that has 16 tables, but only four are relevant to
I have a database table that contains collection data for product collected from a
I have a local sqlite server. The SQL database only contains a table called
I have a SQL Server database table ( DuplicateIds ) that contains the ID's
we use an external Database where we cant edit table designs only add own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.