简体   繁体   中英

I'm trying to get some data into my database. It creates the new tables but doesn't fill them

I'm using a MySQL database on localhost and I'm trying to upload some world data. When I run the code below it creates the forms but then they don't get filled.

I tested the file reading. It was able to echo out the name, region, lat, and lng just fine.

<?php

require_once 'connect.ini.php';

$file_names = scandir('Countries');

foreach ($file_names as &$file_name)
    if ($file_name != '.' && $file_name != '..') {

        $file_path = '/Applications/XAMPP/xamppfiles/htdocs/series/dbsetup/Countries/'.$file_name;
        $file_handle = fopen("$file_path", "r");

        $sql = $sql = "CREATE TABLE `countries1`.`$file_name` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(20) NOT NULL, `region` VARCHAR(20) NOT NULL, `latitude` FLOAT(15) NOT NULL, `longitude` FLOAT(15) NOT NULL) ENGINE = MyISAM;";
        mysql_query($sql);

        while(!feof($file_handle)) {
            $explode = explode(", ", fgets($file_handle));
            $name = $explode[0];
            $region = $explode[1];
            $latitude = $explode[2];
            $longitude = $explode[3];
            $sql = "INSERT INTO `countries1`.`$file_name` (`id`, `name`, `region`, `latitude`, `longitude`) VALUES (NULL, \'$name\', \'$region\', \'$latitude\', \'$longitude\');";
            mysql_query($sql);
        }
    }
?>

Below is my connect.ini.php file although I'm pretty sure it's working because it is creating the forms.

<?php
$conn_error='Could not connect.';

$mysql_host='localhost';
$mysql_user='root';
$mysql_pass='';

$mysql_db='a_database';


if(!(@mysql_connect($mysql_host,$mysql_user,$mysql_pass)&&@mysql_select_db($mysql_db)))
    die($conn_error);
?>

Well, you should not escape single quotes when the variable are inside double quotes. So change the insert query to:

$sql = "INSERT INTO `countries1`.`abc` (`id`, `name`, `region`, `latitude`, `longitude`) VALUES (NULL, '$name', '$region', '$latitude', '$longitude');";

You also don't need the trailing semicolon ; inside the double quotes if you are using mysql functions to execute your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM