简体   繁体   English

我正在尝试将一些数据输入数据库。 它创建新表,但不填充它们

[英]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.

相关问题 我正在尝试使用此代码将项目添加到数据库中,但它没有将它们添加到数据库中。 如何使用带有ajax的PHP代码使用POST方法? - I'm trying to use this code to add items to the database, but it doesn't add them to database. How to use POST METHOD using PHP CODE with ajax? 我无法在数据库中插入数据。 我要去哪里错了? - I can't insert data in my database. Where I'm going wrong? 在Symfony3中,我试图将表单数据持久化到数据库中,但是它不会转到当前用户的行,而是创建一个新条目 - In Symfony3 I am trying to persist form data to the database, but it doesn't go to the current user's row, instead creates a new entry 如何创建数据库和表,并用数据填充它们? 我收到了错误 - How create database and tables, and fill them with data? I got error 我的数据库中有一个整数。 我正在努力获得正确的专栏 - I have an integer in my database. I'm tryingto get the right column 我过度查询我的MySql数据库。 另一种检索许多数据的方法? - I'm over-querying my MySql database. Another way to retrieve many data? 我正在尝试使用参数创建可重用的php函数以从数据库中的表中选择数据 - I'm trying to create a reusable php function with parameters to select data from tables in my database 无法理解为什么此代码不起作用。 我的 sql 数据库有问题。 $sqll=&quot;SELECT * FROM netbarg WHERE r_number=&#39;02177736&#39;&quot; - Cannot understand why this code doesn't work. I have some trouble with my sql database. $sqll="SELECT * FROM netbarg WHERE r_number='02177736'" 插入查询不会将数据添加到我的数据库中。 我的语法是否错误? - Insert query won't add data to my database. Do I have my syntax wrong? 我正在尝试将MySQL数据库中的数据显示到HTML表上,但未注册列字段 - I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM