“Unknown column in 'field list'”, but column does exist

后端 未结 9 1360
既然无缘
既然无缘 2020-12-06 04:41
DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `purchase_date` datetime DEFAULT NULL,
  PRIMAR         


        
相关标签:
9条回答
  • 2020-12-06 05:15

    Look at the name of the table you are handling

    0 讨论(0)
  • 2020-12-06 05:19

    This can also happen if you paste a column name when building the table structure. Same error - but the unprintable/invisible characters are in the table structure, not the query.

    0 讨论(0)
  • 2020-12-06 05:21

    This might not help anyone else, but adding this "just in case" it helps someone.

    In my situation it was a different solution.

    I receive large datasets as Excel CSV files and use a (WIL) script to convert the .csv file into an importable .sql file. I had an error in my script whereby these two lines did not reference the same table name (I had hard-coded the first location and forgot to update it):

    * "INSERT INTO `old_table_name` (`cid`, `date`, etc etc"
    * "CREATE TABLE IF NOT EXISTS `":_dbName:"` (etc etc "
    

    I just changed the first line to also get the table name from the variable, and voila!

    * "INSERT INTO `":_dbName:"` (`cid`, `date`, etc etc"
    

    So check those two lines in your import SQL file.

    0 讨论(0)
  • 2020-12-06 05:23

    when you want to work with mysql using a function like this:

    function insert($table, $info_array){
        // implode keys as columns by [`,`] glue string
        $columns = implode("`,`", array_keys($info_array));
    
        // implode values as sql ready values by [','] glue string
        $values = implode("','", array_values($info_array));
    
        // make query(careful about [`] for columns name and ['] for values)
        $sql = "INSERT INTO ".$table." (`".$columns."`) VALUES ('".$values."');";
        return $sql;
    }
    

    you should be careful about [ ` ] for table columns names and [ ' ] or [ " ] for values. for example, I used above function this way:

    try{
            $db_insert_sample_user = $connection->query(insert(TABLE_PREFIX."users", [
                "username" => "my_name_2",
                "password" => md5("how457fty")
            ]));
            echo '<pre>';
            print_r($db_insert_sample_user);
            echo '</pre>';
        }catch (PDOException $exc){
            echo '<pre>';
            print_r($exc);
            echo '</pre>';
        }
    

    the query string is this:

    INSERT INTO php_pdo_users (`username`,`password`) VALUES ('my_name_2','ee04708d313adf4ff8ba321acf3eb568');
    

    and the result was like : (for two users) PHPMyAdmin Result

    if you want functions based on prepared statements, test this : (placeholders, params and values, don't need [ ' ] or [ " ] at all!!!)

    function insert_prepared(PDO $connection, $table, $info_array){
        // columns
        $columns = implode("`,`", array_keys($info_array));
    
        // placeholders
        $place_holders = [];
        for ( $i = 0; count(array_keys($info_array)) > $i; $i++){
            $place_holders[] = '?';
        }
    
        // convert placeholders to query string
        $place_holders_str = implode(",", $place_holders);
        $prepared_stmt = "INSERT INTO ".$table." (`".$columns."`) VALUES (".$place_holders_str.");";
    
        // prepare statement
        $stmt = $connection->prepare($prepared_stmt);
    
        // values
        $values = array_values($info_array);
    
        // bind all params to values
        for($i = 0; count($values) > $i; $i++){
            $stmt->bindParam($i + 1, $values[$i]);
        }
    
        // execute and return results
        return $stmt->execute();
    }
    

    after code execution this way :

    try {
            $db_insert_sample_user = insert_prepared(
                $connection,
                TABLE_PREFIX . "users",
                [
                    "username" => "my_name_4",
                    "password" => md5( "HelloDolly#__3" )
                ]
            );
        } catch ( PDOException $exc ) {
            echo "Failed : " . $exc->getMessage();
        }
    

    results is : Results with insert_prepared function

    0 讨论(0)
  • 2020-12-06 05:32

    I have had the same issue this morning and I didn't find my answer. But I found my problem when I changed the single quotes around my query to double quotes. Something so small and an oversight can cause a real headache.

    Unknown column x in "field list" - Code below wrapped in single quotes - Non working.

        $likepost='INSERT INTO reaction(reaction_num,userreaction_id,timereacted,
        streamitem_id,comment_posted_on) 
        VALUES ($reaction,$target,NOW(),$streamid,$comment_id)';
    

    Code below wrapped in double quotes. working

        $likepost="INSERT INTO reaction(reaction_num,userreaction_id,timereacted,
        streamitem_id,comment_posted_on) 
        VALUES ($reaction,$target,NOW(),$streamid,$comment_id)";
    
    0 讨论(0)
  • 2020-12-06 05:33

    There is an unprintable character 30 (RecordSeparator) inserted between purchase_date and the ' in the INSERT statement. Just remove the text ('purchase_date') and rewrite it by hand it should be fine.

    0 讨论(0)
提交回复
热议问题