I have the following code for uploading a csv file to a mysql database. Its working just fine, but if the csv file contains a heading for each column its getting uploaded in the first row of the table. I want to remove the first row of the csv file while getting stored in the database. How can i do that ?
<?php
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("crm",$connect); //select the table
//
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO lead (name, lead_value, status) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
example code from what I'm using, and it should work because I'm using it in production environment
$handle = fopen("stuff.csv","r");
/* assuming that first row has collumns names */
$query = "INSERT INTO table(";
$cols = fgetcsv($handle,100000,';','"');
$query .= implode(", ",$cols).") VALUES";
$values = "";
/* cycle through each row and build mysql insert query */
while($data = fgetcsv($handle,100000,';','"')) {
$values .= " ( ";
foreach($data as $text) {
$values .= "'".$text."', ";
}
$values = substr($values,0,-2);
$values .= "), ";
}
/* remove last 2 chars */
$values = substr($values,0,-2);
$query .= $values;
echo $query;
fclose($handle);
Mind the fact that this script will return the mysql query... not execute it, so alter it to your needs.
I think you mean that you want to not insert the first row of the CSV data into your table. Working on that assumption (note that I'm not a PHP programmer, so the changes I've made should be taken as pseudocode):
if ($_FILES[csv][size] > 0) {
$first_time = true;
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($first_time == true) {
$first_time = false;
continue;
}
if ($data[0]) {
mysql_query("INSERT INTO lead (name, lead_value, status) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location: import.php?success=1'); die;
}
If I am understanding you correctly you want to remove the header row? I would use LOAD DATA instead of INSERT INTO
LOAD DATA INFILE '/tmp/test.csv' INTO TABLE test FIELDS TERMINATED BY ',' IGNORE 1 LINES;
LOAD DATA is vastly superior performance-wise than INSERT.
MySQL - LOAD DATA
Related
I'm working on a script that allows me to take .txt files content from a folder on my computer and read them with file_get_contents() then save them to my database, each time I manage to save only the first file, and I would like to save the contents of each file from the values retrieved by the file_get_contents() or file() function (with for loop).
Thank you for your help !
Here's the code:
<?php
$pdo = require 'db_settings.php';
if(isset($_POST['submit'])){
$line1 = $_FILES['filetxt']["tmp_name"];
echo '<pre>';
$count = count($line1);
for ($i=0; $i < $count; $i++) {
$insert1 = file_get_contents($line1[$i]);
// Begin
try {
$sql = "INSERT INTO fill_wp (content) VALUES (:insert1)";
$stmt = $conn->prepare($sql);
$stmt->execute(array(":insert1" => $insert1));
}
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
//End
echo '</pre>';
}
<form method='post' action='get_temp.php'>
<input type="text" name="urldirectory" id="textfileurl">
<input type='submit' name='submit' value='Upload'>
</form>
I am trying to upload a CSV file which contains the fields stated in the link below [see CSV example] via a web form. The user can browse their computer by clicking the upload button, select their CSV file and then click upload which then imports the CSV data into the database in their corresponding fields. At the moment when the user uploads their CSV file, the row is empty and doesn't contain any of the data that the CSV file contains. Is there a way i could solve this so that the data inside the CSV file gets imported to the database and placed in its associating fields?
CSV example:
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-10at145431_zps80a42938.png
uploads.php
<!DOCTYPE html>
<head>
<title>MySQL file upload example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file"><br>
<input type="submit" value="Upload file">
</form>
<p>
See all files
</p>
</body>
</html>
upload_file.php
<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', 'vario007', 'spineless');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$filedata= file_get_contents($_FILES ['uploaded_file']['tmp_name']); //this imports the entire file.
// Create the SQL query
$query = "
INSERT INTO `Retail` (
`date`, `order_ref`, `postcode`, `country`, `quantity`, `packing_price`, `dispatch_type`, `created`
)
VALUES (
'{$date}', '{$order_ref}', '{$postcode}', '{$country}', '{$quantity}', '{$packing_price}', '{$dispatch_type}', NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click here to go back</p>';
?>
try this
<?php
if(isset($_FILES['uploaded_file'])) {
if($_FILES['uploaded_file']['error'] == 0) {
$dbLink = new mysqli('localhost', 'root', 'vario007', 'spineless');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
$file = $_FILES ['uploaded_file']['tmp_name'];
$handle = fopen($file, "r");
$row = 1;
while (($data = fgetcsv($handle, 0, ",","'")) !== FALSE)
{
if($row == 1)
{
// skip the first row
}
else
{
//csv format data like this
//$data[0] = date
//$data[1] = order_ref
//$data[2] = postcode
//$data[3] = country
//$data[4] = quantity
//$data[5] = packing_price
//$data[6] = dispatch_type
$query = "
INSERT INTO `Retail` (
`date`, `order_ref`, `postcode`, `country`, `quantity`, `packing_price`, `dispatch_type`, `created`
)
VALUES (
'".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', NOW()
)";
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
$row++;
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click here to go back</p>';
?>
fgetcsv() in third argument in separator to you want to save in csv file. Ex: comma,semicolon or etc.
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
$filedata= csv_to_array($_FILES ['uploaded_file']['tmp_name']);
foreach($filedata as $data)
{
$sql = "INSERT into table SET value1='".$data['value1']."'......";
}
Currently I am trying to upload a CSV file and enter each record into the database 1 by 1. The columns on the CSV have the same name as the field names in the database but sometimes the data will be in a different order within the CSV. When I say in a different order, I mean that instead of a list of names always being in the 1st column, they might be in the 3rd column.
Really what I'm asking is how will I do the above as I'm really stuck.
At the moment I doesn't insert into the database but it does get the array from the CSV file.
Code Below:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>CSV Import</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="csv"/>
<input type="submit" name="submit" value="Save" />
</form>
</body>
</html>
config.php
<?php
/* Database Connection */
$con = mysql_connect('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxx');
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$select_db = mysql_select_db('xxxxxxxx');
?>
upload.php
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = filesize($file);
$handle = fopen($file, "r");
$csvData = fgetcsv($handle, $length, $separator);
fclose($handle);
$i = 0;
while($i >= 1){
$title = $csvData[0];
$firstName = $csvData[1];
$secondName = $csvData[2];
$emailAddress = $csvData[3];
$houseNumber = $csvData[4];
$mobileNumber = $csvData[5];
$address1 = $csvData[6];
$address2 = $csvData[7];
$address3 = $csvData[8];
$address4 = $csvData[9];
$postcode = $csvData[10];
mysql_query("INSERT csv SET title='$title', firstName='$firstName' ,secondName='$secondName', emailAddress='$emailAddress', houseNumber='$houseNumber' ,mobileNumber='$mobileNumber', address1='$address1', address2='$address2', address3='$address3' ,address4='$address4', postcode='$postcode'")
$i++;
}
?>
The code you're using to insert the records will fail here:
$i = 0;
while($i >= 1){ // $i = 0. This test will fail.
// do stuff
}
The correct upload.php, assuming that there is a header line which contains the column names:
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = 0; // size of the longest line(!), 0 = no limit
$fields = array('title', 'firstName', 'secondName', 'emailAddress', 'houseNumber', 'mobileNumber', 'address1', 'address2', 'address3', 'address4', 'postcode'); // use it as a white list
$handle = fopen($file, "r");
// get 1st line (header) and flip keys and values
// format like [title] --> 0, [firstName] --> 1, ...
$header = array_flip(fgetcsv($handle, $length, $separator));
$values = array();
// while we can read lines as csvData:
while(($csvData = fgetcsv($handle, $length, $separator)) !== false){
foreach ($fields as $field) // put all values in an array in correct order
$values[] = $csvData[$header[$field]];
mysql_query("INSERT INTO csv (" . implode(',', array_keys($header)) . ") VALUES (" . implode(',', $values) . ")"); // only for demonstration - be careful with spaces and quotes in values - better switch to PDO!
}
fclose($handle);
Hope that helps! Untested, but might be working :).
Note: this will fail when someone omits values and you have declared them in your database as NOT NULL.
Be aware that this sample does not do any counter-measures to SQL injections. Please forget about mysql_query and learn to use PDO.
I'm trying to upload my txt file into my database but I don't think anything happens. I checked my database in phpmyadmin but nothing was inserted. How do I load and insert my data into mysql database?
Here's my 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 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>
If this is text data then you forgot ' around data
$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')");
Here's how to parameterise SQL statements with untrusted input.
$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();
See http://codular.com/php-mysqli for more info on this.
If you don't understand why you should be doing it this way, look up SQL injection, and don't write another line of code until you do understand it.
You can do it with the library of this answer
$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);
}
}
One thing i noticed that in the insert query you must have some varchar fields in your database table and for that you are missing commas. Wrap the varchar fields with commas. This might be the problem and use die and mysql_error to see what the error really is.
What the following script does (or should do):
Connects to DB
Includes a function for creating a 5 digit code
User enters their email address
Checks if it's a valid email
Inserts the email into the 'email' column
Checks if email already exists, if so, let the user know and break script
Runs the function for creating a 5 digit code
Checks the 'unique_code' column if it already exists, if so, loop from 5 digit code creation function
If all is valid, hide the form and display the (ajax from a separate JS) thank you div
Display the unique code to the user
Everything runs, however the unique_code is not inserted into the DB and is not displayed when it does "Thank you! ".
What am I doing wrong and what needs to be modified?
Thank you!
Code
<?php
require "includes/connect.php";
function generateCode($length = 5) {
$characters = 'bcdfghjkmnpqrstvwxyz';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
}
$msg = '';
if($_POST['email']){
// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
try{
//validate email
if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
throw new Exception('Invalid Email!');
}
//insert email
$mysqli->query("INSERT INTO coming_soon_emails
SET email='".$mysqli->real_escape_string($_POST['email'])."'");
//if already exists in email column
if($mysqli->affected_rows != 1){
throw new Exception('You are already on the notification list.');
}
if($ajax){
die('{"status":1}');
}
//start creating unique 5 digit code
$unique_code = "";
$inserted = false;
// Keep looping until we've inserted a record
while(!$inserted) {
// Generate a code
$unique_code = generateCode();
// Check if it exists
if ($result = $mysqli->query("SELECT unique_code FROM coming_soon_emails WHERE unique_code = '$unique_code'")) {
// Check no record exists
if ($result->num_rows == 0) {
// Create new record
$mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('" . $mysqli->real_escape_string($_POST['email']) . "','$unique_code')");
// Set inserted to true to ext loop
$inserted = true;
// Close the result object
$result->close();
}
} else {
// Quit if we can't check the database
die('Something went wrong with select');
}
}
}
catch (Exception $e){
if($ajax){
die(json_encode(array('error'=>$e->getMessage())));
}
$msg = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>example</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body>
<div id="container">
<form id="form" method="post" action="">
<input type="text" id="email" name="email" value="<?php echo $msg?>" />
<input type="submit" value="Submit" id="submitButton" />
</form>
<div id="thankyou">
Thank you! <?php echo $unique_code;?></p>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>
You seem to be using the private keyword outside a class definition, which isn't allowed.
Do you have a primary key on 'email' in the coming_soon_emails table? Since you've already inserted one entry for the given email address, that would keep you from inserting a second entry with the unique value.
Why not do an UPDATE instead of an INSERT once you have determined the unique key?