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

后端 未结 9 1401
既然无缘
既然无缘 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: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 '
    ';
            print_r($db_insert_sample_user);
            echo '
    '; }catch (PDOException $exc){ echo '
    ';
            print_r($exc);
            echo '
    '; }

    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

提交回复
热议问题