0

我正在尝试将我的 txt 文件上传到我的数据库中,但我认为不会发生任何事情。我在 phpmyadmin 中检查了我的数据库,但没有插入任何内容。如何将我的数据加载并插入到 mysql 数据库中?

这是我的代码:

<?php 
$conn = mysql_connect("localhost", "login", "password") or die(mysql_error()); 

mysql_select_db("database", $conn);

if(!isset($_POST['submit']))
{
    $uploadtxt = "nyccrash.txt";

    $handle= fopen($uploadtxt, "r");

    // error checking.
    if($handle === false) {
   die("Error opening $uploadtxt");
}


    while($fileop = fgetcsv($handle, 1000, ",") !== false) { 

    $crash_year = $fileop[0];
    $accident_type = $fileop[1];
    $collision_type = $fileop[2];
    $weather_condition = $fileop[3];
    $light_condition = $fileop[4];
    $x_coordinate = $fileop[5];
    $y_coordinate = $fileop[6];


    $sql = mysql_query("INSERT INTO nyccrash (crash_year, accident_type, collision_type, weather_condition, light_condition, x_coordinate, y_coordinate) VALUES ($crash_year, $accident_type, $collision_type, $weather_condition, $light_condition, $x_coordinate, $y_coordinate)"); 

    } } 

?>

<!DOCTYPE html> 
<html>
<head> 
<meta charset="utf-8">
<title> NYC Crash Data </title> 
<link ref="stylesheet" type "text/css" href="../style/style.css" /> 

</head> 
<body> 
<div id="mainWrapper"> 

    <form method="post" action="" enctype="multipart/form-data"> 
        <input type="file" name="file"/>
        <br/> 
        <input type="submit" name="submit" value="submit"/> 
    </form> 

</div> 

4

3 回答 3

0

您可以使用答案 的库来完成

$csv    =   New CSVReader();
$result =   $csv->parse_file('Test.csv');//path to file should be csv
echo '<pre>';     //
print_R($result); // Only for testing

if($result){
    foreach($result as $row){
            $crash_year     =   $row['crash_year'];
            $accident_type      =   $row['accident_type'];
            $collision_type =   $row['collision_type'];
            $weather_condition  =   $row['weather_condition'];
            $light_condition    =   $row['light_condition'];
            $x_coordinate       =   $row['x_coordinate'];
            $y_coordinate       =   $row['y_coordinate'];

            $query  =   "INSERT INTO nyccrash";
            $query  .=  "(crash_year, accident_type, collision_type, weather_condition, light_condition, x_coordinate, y_coordinate)";
            $query  .=  " VALUES ";
            $query  .=  "('$crash_year','$accident_type','$collision_type', '$weather_condition', '$light_condition', '$x_coordinate', '$y_coordinate')";   
            mysqli_query($query);
            unset($query);
    }
}

我注意到一件事,在插入查询中,您的数据库表中必须有一些 varchar 字段,因此您缺少逗号。用逗号包裹 varchar 字段。这可能是问题所在,使用 die 和 mysql_error 来查看错误的真正含义。

于 2013-04-18T11:34:53.420 回答
0

如果这是文本数据,那么您忘记'了数据

$sql = mysql_query("INSERT INTO nyccrash (crash_year, accident_type, 
collision_type, weather_condition, light_condition, x_coordinate, y_coordinate) 
VALUES ('$crash_year', '$accident_type', '$collision_type', 
'$weather_condition', '$light_condition', '$x_coordinate', '$y_coordinate')"); 
于 2013-04-18T11:03:58.303 回答
0

以下是如何使用不受信任的输入参数化 SQL 语句。

$stmt = $db->prepare("INSERT INTO nyccrash (crash_year, accident_type, 
  collision_type, weather_condition, light_condition, x_coordinate, y_coordinate) 
  VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('sssssss', $crash_year, ...);
$stmt->execute();

有关这方面的更多信息,请参阅http://codular.com/php-mysqli

如果你不明白为什么要这样做,请查看 SQL 注入,在你理解之前不要再写一行代码。

于 2013-04-18T11:32:26.607 回答