我正在尝试将我的 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>