从文件名创建表

时间:2014-06-09 15:40:00

标签: php mysql csv

我试图在php中编写一个脚本来上传csv文件,然后将它们存储在mysql数据库中。绊脚石似乎是当我尝试将文件名作为将要创建的表名的变量传递时...

<?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 for errors
if($_FILES['file_source']['error'] > 0) {
    die('An error occurred when uploading.');
} else {
echo "File uploaded successfully <br />";
}
//Check file type is csv
if($_FILES['file_source']['type'] != 'text/csv'){
    die('Unsupported filetype uploaded.');
    echo "File is not in csv format";
} else {
echo "File is in csv format<br />";
}

$tableName = $_FILES["file_source"]["name"];
echo "Table will be 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();
?>

你会注意到我没有把csv数据注入表中,因为我被困在学习曲线的这一部分。任何帮助将不胜感激。

进一步说,我已经从文件名中删除了.csv扩展名来创建表,我已经在那里包含了我的数据库连接,现在它似乎在实际的表创建中窒息,从“//创建”开始表” ...

<?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 for errors
if($_FILES['file_source']['error'] > 0) {
    die('An error occurred when uploading.');
} else {
echo "File uploaded successfully <br />";
}    

$tName = $_FILES["file_source"]["name"];
$tableName = preg_replace('/\.csv$/','',$tName);
echo "Table will be called $tableName<br />";

// Connect to db
$mysqli = new mysqli('***', '***', '***', '***');
if ($mysqli->connect_errno) {
echo "Failed to connect to 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();
?>

这个输出是......

Upload: FILE0414.csv
Type: text/csv
Size: 1430.587890625 kB
Stored in: /tmp/phpSEbKp4
File uploaded successfully
Table will be called FILE0414
Localhost via UNIX socket 

更新

修复并正常工作......

意识到$ db是一个未知变量,用$ mysqli替换了所有实例,并且所有实例都按照我的预期运行。谢谢你们!

0 个答案:

没有答案