php - How do I insert data from a CSV file into my database? -
i'm trying upload txt file database don't think happens. checked database in phpmyadmin nothing inserted. how load , insert data mysql database?
here's code:
<?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 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>
if text data forgot '
around data
$sql = mysql_query("insert 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')");
Comments
Post a Comment