php - LOAD DATA INFILE doesn't upload CSV data into MySQL table with no error -
update:
i attempted execute query generated php script in phpmyadmin's sql tab , got:
#7890 - can't find file 'c:wamp mpphpb4c4.tmp'.
it cut path between c:wamp , tmp file name. i'm escaping file path incorrectly?: $file = $_files['csv']['tmp_name'];
i'm writing cool webapp using php , mysql. 1 of functions upload contents of csv file table in database called suites.
csv file looks this:
1230,cool business,,1 3612,not-so-cool business ltd.,john smith,0
column meanings left right in csv go such: suite number, name of business operating @ suite, primary contact(optional), did go there?(0 - no, 1 - yes; optional also).
the database table, named suites, has following structure:
suite_id(primary), location_id(foreign referencing locations table), suite, business, contact, visited
as may have guessed i'm trying insert last 4 columns of suites table above csv example.
i attempt following php code:
<?php /** * class registration * handles user registration */ class suitecsv { /** * @var object $db_connection database connection */ private $db_connection = null; /** * @var array $errors collection of error messages */ public $errors = array(); /** * @var array $messages collection of success / neutral messages */ public $messages = array(); /** * function "__construct()" automatically starts whenever object of class created, * know, when "$registration = new registration();" */ public function __construct() { if(isset($_post["add_suite_csv"])){ $this->addsuitecsv(); } } /** * handles entire registration process. checks error possibilities * , creates new user in database if fine */ private function addsuitecsv() { if (empty($_post['suite_location_csv'])) { $this->errors[] = "must select location of suite"; }else{ // create database connection $this->db_connection = new mysqli(db_host, db_user, db_pass, db_name); if (!$this->db_connection->set_charset("utf8")) { $this->errors[] = $this->db_connection->error; } if (!$this->db_connection->connect_errno) { $suite_location_csv = $this->db_connection->real_escape_string(strip_tags($_post['suite_location_csv'], ent_quotes)); if ($_files['csv']['size'] > 0) { $file = $_files['csv']['tmp_name']; $query = <<<eof load data infile '$file' table sales.suites fields terminated ',' optionally enclosed '"' lines terminated '\r\n' (suite,business,contact,visited) set location_id = $suite_location_csv eof; $query_add_suite_csv = $this->db_connection->query($query); if ($query_add_suite_csv) { $this->messages[] = "csv has been uploaded."; } else { $this->errors[] = "sorry, couldn't upload csv. try again."; } var_dump($query_add_suite_csv); }else{ $this->errors[] = "must select csv file upload"; } }else{ $this->errors[] = "database connection error"; } } } }
$suite_location in case value user select drop down menu on upload form, in turn populated php script when page loaded. values of $suite_location location_id values in locations table has foreign key in suite table.
so, user picks location, selects csv file , uploads suites table.
nothing happening though. there no errors. i've looked @ load data infile documentation , tried apply i've understood it, still doesn't seem work.
form snippet:
<form class="form-horizontal" method="post" action="admin_add_location.php" enctype="multipart/form-data" name="addsuitecsv"> <div class="form-group"> <label class="control-label">select locations:</label> <div class=""> <select size="6" name="suite_location_csv"> <?php $location->getlocations(); ?> </select> </div> <label class="control-label">choose file:</label> <div class=""> <input name="csv" type="file" id="csv" /> </div> <button class="btn btn_success" name="add_suite_csv" type="submit"><span class="glyphicon glyphicon-upload" ></span>import csv</button> </div> </form>
add view php:
<?php // include configs / constants database connection require_once("config/db.php"); // load login class require_once("classes/login.php"); // create login object. when object created, login/logout stuff automatically // single line handles entire login process. in consequence, can ... $login = new login(); // administrators can see if ($login->isuserloggedin() == true && $_session['user_group'] == 0) { require_once("classes/location.php"); $location = new location(); require_once("classes/suite.php"); $suite = new suite(); require_once("classes/suitecsv.php"); $suitecsv = new suitecsv(); include("includes/header.php"); include("views/admin_add_location_view.php"); include("views/admin_add_suite_view.php"); include("views/admin_add_suite_csv_view.php"); include("includes/footer.php"); }else{ echo "<h1>access denied</h1>"; include("views/go_back_view.php"); } ?>
changing:
$file = $_files['csv']['tmp_name'];
to:
$file = addslashes($_files['csv']['tmp_name']);
fixed issue , csv gets uploaded.
basically had escape slashes in file path.
Comments
Post a Comment