我是新來的PHP,並希望你們能幫助我與這裏的情況..內容PHP:上傳文本文件,並保存在數據庫
我有我要上傳並保存的報告文件內容到數據庫..我已經成功上傳,但沒有保存在數據庫與單獨的領域..我只能上傳到1字段只有
文本文件的內容是這樣的(與固定格式) :
text.txt
2010000130QUEEN CONSOLIDATED 45119700005794493080171527OLIVER 800000.0029/01/1622:27:069501
2010000130QUEEN CONSOLIDATED 45119700008408176500068670QUEEN 500000.0029/01/1622:07:30373L
從上面可知,前3列分別爲no,next 7代表公司名稱,30爲公司名稱,16爲userid,10爲usernumber,35爲名稱,10爲trans,8爲日期,8爲時間最後是位置..
我試過這麼多的代碼,但我認爲這是我的need.The代碼越接近看起來像下面
upload.php的
<?php include "header.php";
?>
<form action="do_upload.php" method="post" enctype="multipart/form-data">
\t <table width="409" height="199" border="0" align="center">
<tr>
\t <td width="161">Upload File (.txt only)</td>
<td width="14">:</td>
<td width="218"><input type="file" name="upload"></td>
</tr>
<tr>
\t <td colspan="3"><button type="submit">Upload</button></td>
</tr>
<tr>
\t <td colspan="3">
\t \t \t \t \t \t <?php if(!empty($_GET['msg'])){
\t \t \t \t \t \t \t echo $_GET['msg'];}
\t \t \t \t \t \t ?>
</td>
</tr>
</table>
</form>
<?php include "footer.php";?>
這是我的php代碼,它不工作看起來像下面。
do_upload.php
<?php include "../connection.php";
\t $location_file=$_FILES['upload']['tmp_name'];
$name_file=$_FILES['upload']['name'];
$type_file=$_FILES['upload']['type'];
$size_file=$_FILES['upload']['size'];
$content = file_get_contents($_FILES['upload']['tmp_name']);
$handle = fopen($_FILES['upload']['tmp_name'],"r");
$lines = explode("\n", $content);
if($location_file==""){
\t header("location:upload.php?msg=Please Choose the File");
}else if ($type_file == '.txt'){
\t header("location:upload.php?msg=Upload file with .txt extension only");
}else if($size_file > 104857600){
\t header("location:upload.php?msg=Max File Size is 100MB");
}
else{
\t
\t $data = array();
\t while (false !== ($line = fgets($handle)))
\t {
\t \t $data[] = array(
\t \t \t 'no' => trim(substr($line, 0, 3)),
\t \t \t 'code' => trim(substr($line,3,7)),
\t \t \t 'companyname' => trim(substr($line, 10, 30)),
\t \t \t 'userid' => trim(substr($line, 40, 16)),
\t \t \t 'usernumber' => trim(substr($line, 56, 10)),
\t \t \t 'name' => trim(substr($line, 66, 35)),
\t \t \t 'trans' => trim(substr($line, 101, 10)),
\t \t \t 'date' => trim(substr($line, 111, 8)),
\t \t \t 'time' => trim(substr($line,119,8)),
\t \t \t 'location' => trim(substr($line,127))
\t \t);
\t }
\t fclose($handle);
\t
\t foreach($data as $row){
\t \t $sql="INSERT INTO report (no, code, companyname, userid, usernumber, name, trans, date, time, location) VALUES ('{$row[0]}','{$row[1]}','{$row[2]}','{$row[3]}','{$row[4]}','{$row[5]}','{$row[6]}','{$row[7]}','{$row[8]}','{$row[9]}')";
\t \t if (!mysqli_query($connect,$sql))
\t \t {
\t \t die('Error: ' . mysqli_error($connect));
\t \t }
\t }
\t mysqli_close($connect);
\t header("location:upload.php?msg=Upload Success! Please View it at 'VIEW REPORT' Menu");
}
?>
由於我是新來的PHP,我想我應該用substr()
功能,因爲會有一個白色的空間,我會使用trim()
,因爲這是一個固定的格式。
問題是我上傳文本後,它插入一個空值到數據庫中。我真的不知道我該怎麼做
請指導我如何去做。我仍然是一個新手在PHP,所以任何幫助將非常感激..謝謝!
UPDATE ..陣列工作,但在數據庫中沒有添加值
$data[] = array();
\t \t $i = 0;
\t \t while (false !== ($lines = fgets($handle)))
\t \t {
\t \t \t $data[$i] = array(
\t \t 'no' => trim(substr($line, 0, 3)),
\t \t 'code' => trim(substr($line,3,7)),
\t \t 'companyname' => trim(substr($line, 10, 30)),
\t \t 'userid' => trim(substr($line, 40, 16)),
\t \t 'usernumber' => trim(substr($line, 56, 10)),
\t \t 'name' => trim(substr($line, 66, 35)),
\t \t 'trans' => trim(substr($line, 101, 10)),
\t \t 'date' => trim(substr($line, 111, 8)),
\t \t 'time' => trim(substr($line,119,8)),
\t \t 'location' => trim(substr($line,127))
\t );
\t \t \t $i++;
\t \t }
\t \t fclose($handle);
\t \t
// THIS IS THE PROBLEM,if this not commented then the loop will not work
\t \t /*foreach($data as $data){
\t \t \t $sql="INSERT INTO report (no, code, companyname, userid, usernumber, name, trans, date, time, location) VALUES ('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}','{$data[4]}','{$data[5]}','{$data[6]}','{$data[7]}','{$data[8]}','{$data[9]}')";
\t \t \t if (!mysqli_query($connect,$sql))
\t \t \t {
\t \t \t die('Error: ' . mysqli_error($connect));
\t \t \t }
\t \t }*/
\t \t
\t \t //echo "<pre>";
\t \t //print_r($data); Checking the values
\t \t //echo "</pre>";
mysqli_close($connect);
header("location:upload.php?msg=Upload Success!");
現在,我的'SUBSTR()函數的工作,唯一的問題是我不能循環當我插入數據庫的價值,我顯然不知道..當我可以插入的價值,它會像這樣插入
Image inside of phpmyadmin Database
我檢查我的$ data數組,和價值就在那裏,但該數據僅是從文本文件的第一行,並不能進入數據庫 – Reed