" name="description" /> " />

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

这是我自己的一个作业,用的是很基础的代码。

有错误的地方欢迎批评和指正!

 

这里最容易出错的地方在读取数据后向数据库表中插入数据是的数据格式!

 

文件上传的页面

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>

 

接收(对数据的读取)与对数据库的操作界面

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 【这是你自己要读取的文件,里面的内容根据自己需求写】

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

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

原文:https://www.cnblogs.com/mz-xiansheng/p/9211287.html

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

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