日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】

發(fā)布時間:2023/12/9 php 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這是我自己的一個作業(yè),用的是很基礎的代碼。

有錯誤的地方歡迎批評和指正!

?

這里最容易出錯的地方在讀取數(shù)據(jù)后向數(shù)據(jù)庫表中插入數(shù)據(jù)是的數(shù)據(jù)格式!

?

文件上傳的頁面

uploading.php

<html>
<body align = "center">
<form action = "DBConnect.php" method = "post" enctype = "multipart/form-data">
Upload a text file <input name = "myfile" type = "file" /></br></br>
<input type = "submit" value = "Upload this file"/></br></br>
</form>
<a href = "">Click here to find the format of text</a>
</body>
</html>

?

接收(對數(shù)據(jù)的讀取)與對數(shù)據(jù)庫的操作界面

DBConnect.php

<?php
$content = $_FILES['myfile']; // 對文件格式的讀取,$_FILES方法
//print_r($content);

$string = file($content['name']);

//print_r($string);
//echo $string[0];
//exit(0);
$link = mysqli_connect("localhost","root","1234");

//create database sampleDB
mysqli_query($link,"create database $string[0]");

//create table mytbl
mysqli_query($link,"use $string[0]");

$tblQuery = "create table $string[1](";
$tblString="";
for($i=3;$i<count($string);$i++){

if(trim($string[$i]) == "row"){ // trim means remove space
break;
}else{
$col = explode(",",$string[$i]);
$tblString.=$col[0]." ".$col[1].",";
}
}
$tblQuery1 = $tblQuery.substr($tblString,0,-1).");";
mysqli_query($link,$tblQuery1);


//insert data to mytbl

$data = "insert into $string[1] values";
$data1String = "";
for($i=10;$i<count($string);$i++){
$data1 = explode(",",$string[$i]);
$data1String.="(".$data1[0].","."'".$data1[1]."'".",".$data1[2].",".$data1[3].","."'".$data1[4]."'".","."'".$data1[5]."'"." "."),";
}
// echo $data1String;
$tbldata = $data.substr($data1String,0,-1).";";
//echo $tbldata;
mysqli_query($link,$tbldata);

?>

?

info.txt 【這是你自己要讀取的文件,里面的內(nèi)容根據(jù)自己需求寫】

sampleDB
mytbl
columns
sno,int
sname,varchar(15)
age,int
gender,boolean
address,text
cellphone,varchar(11)
row
1,john,12,0,california,1212111
2,alice,15,1,new york,1241512
3,cindy,13,1,LA,1251455
4,joshua,14,0,philadelphia,2154412

轉(zhuǎn)載于:https://www.cnblogs.com/mz-xiansheng/p/9211287.html

總結

以上是生活随笔為你收集整理的PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。