php - create table from filename -


i attempting write script in php upload csv files , store them in mysql database. stumbling block appears when try pass filename variable table name created...

<?php if ($_files["file_source"]["error"] > 0) {   echo "error: " . $_files["file_source"]["error"] . "<br>"; } else {   echo "upload: " . $_files["file_source"]["name"] . "<br>";   echo "type: " . $_files["file_source"]["type"] . "<br>";   echo "size: " . ($_files["file_source"]["size"] / 1024) . " kb<br>";   echo "stored in: " . $_files["file_source"]["tmp_name"] . "<br>"; }  //check errors if($_files['file_source']['error'] > 0) {     die('an error occurred when uploading.'); } else { echo "file uploaded <br />"; } //check file type csv if($_files['file_source']['type'] != 'text/csv'){     die('unsupported filetype uploaded.');     echo "file not in csv format"; } else { echo "file in csv format<br />"; }  $tablename = $_files["file_source"]["name"]; echo "table called $tablename<br>";  $create_table = "create table if not exists `$tablename` (     id int not null auto_increment,     partnumber int not null,     description varchar (200) not null,     price int not null,     per varchar (2) not null,     uoi int not null,     brand varchar (30) not null,     lrretail int not null,     weight int not null,     length int not null,     width int not null,     thickness int not null,     primary key(id) )";  // create table $create_tbl = $db->query($create_table); if ($create_table) {     echo "table has created"; } else {         echo "error!!";   } $db->close(); ?> 

you notice haven't gotten far inject csv data table stuck @ part of learning curve. appreciated.

got little further in have stripped .csv extension filename create table, have included db connection in there , seems choking on actual table creation starting @ "//create table"...

<?php if ($_files["file_source"]["error"] > 0) {   echo "error: " . $_files["file_source"]["error"] . "<br>"; } else {   echo "upload: " . $_files["file_source"]["name"] . "<br>";   echo "type: " . $_files["file_source"]["type"] . "<br>";   echo "size: " . ($_files["file_source"]["size"] / 1024) . " kb<br>";   echo "stored in: " . $_files["file_source"]["tmp_name"] . "<br>"; }  //check errors if($_files['file_source']['error'] > 0) {     die('an error occurred when uploading.'); } else { echo "file uploaded <br />"; }      $tname = $_files["file_source"]["name"]; $tablename = preg_replace('/\.csv$/','',$tname); echo "table called $tablename<br />";  // connect db $mysqli = new mysqli('***', '***', '***', '***'); if ($mysqli->connect_errno) { echo "failed connect mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo $mysqli->host_info . "\n <br />";  $create_table = "create table if not exists '$tablename' (     `id` int not null auto_increment,     `partnumber` int not null,     `description` varchar (200) not null,     `price` int not null,     `per` varchar (2) not null,     `uoi` int not null,     `brand` varchar (30) not null,     `lrretail` int not null,     `weight` int not null,     `length` int not null,     `width` int not null,     `thickness` int not null,     primary key(id) )";  // create table $create_tbl = $db->query($create_table); if ($create_table) {     echo "table has created"; } else {     echo "error!!";   } $db->close(); ?> 

the output is...

upload: file0414.csv type: text/csv size: 1430.587890625 kb stored in: /tmp/phpsebkp4 file uploaded table called file0414 localhost via unix socket  

update

fixed , working...

realised $db unknown variable, replaced instances of $mysqli , running expect. guys!


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -