[英]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. 我在本地主机上使用MySQL数据库,并尝试上传一些世界数据。 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. 它能够很好地呼应名称,区域,纬度和lng。
<?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. 下面是我的connect.ini.php文件,尽管我非常确定它正在工作,因为它正在创建表单。
<?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. 如果您正在使用mysql函数执行查询,请在双引号内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.