備忘録

unity、python
環境設定など調べたことを備忘録として載せておきます。

PHPでEXCELデータをMysqlにimportする part2(index2.php sorce)

2021-05-20 17:21:31 | 日記
(index2.php)

<!DOCTYPE html>
<html lang="ja">
<head>

<title> Export Data From Mysql to Excelusing PHPpredsheet </title>
</head>
<body>
<form action="#" method="POST" enctype="multipart/form-data">
<input type="file" name="excel">
<input type="submit" name="submit">
</form>


if(isset($_FILES['excel']['name'])){
$con=mysqli_connect("localhost","blog_user2","skytree634","testing");
include "xlsx.php";
if($con){
$excel=SimpleXLSX::parse($_FILES['excel']['tmp_name']);
echo "
";
//print_r($excel->rows(1));
//print_r($excel->dimension(2));
//print_r($excel->sheetNames());
for ($sheet=0; $sheet < sizeof($excel->sheetNames()) ; $sheet++){
$rowcol=$excel->dimension($sheet);
$i=0;
if($rowcol[0]!=1 && $rowcol[1]!=1){
//----------
foreach ($excel->rows($sheet) as $key => $row){
//print_r($row);
$q="";
foreach ($row as $key => $cell){
//print_r($cell);echo "
";
if($i==0){
$q.=$cell. " varchar(50),";
}else{
$q.="'".$cell. "',";
}
}
if($i==0){
$query="CREATE table ".$excel->sheetName($sheet)." (".rtrim($q,",").");";
//---------
}else{
$query="INSERT INTO ".$excel->sheetName($sheet)." values (".rtrim($q,",").");";
}
echo $query;
if(mysqli_query($con,$query)){
echo "true";
}
echo "
";
$i++;
}
}
}
}
}
?>
</body>
</html>

----------

PHPでEXCELデータをMysqlにimportする

2021-05-16 00:05:21 | 日記
1.xamppをインストールする。
 (xamppを検索し、downloadする)
2.xamppの下のhtdocsフォルダに次のファイルを作成する。
(1)Book1.xlsx
(2)xlsx.php
(3)index2.php
3.Book1.xlsxにデータを作成する。
 [A,1]”empid”、[A,2]1、[A,3]2、[A,4]3
 [B,1]”name”、[B,2]"cody”、[B,3]"miller"、[B,4]"chris”
 [C,1]"role"、[C,2]"coder"、[C,3]"developer”、[C,4]"manager”
4.xlsx.phpを作成する。
(1)”simplexlsx”と検索し”GitHub - shuchkin/simplexlsx: Parse and retrieve data from ...”を開く
(2)”shuchkin x10 fastest getCell”の”src”の中の”SimpleXLSX.php”を開いて内容をすべてxlsx.phpにコピーする。
5.index2.phpをコーディングする。
---------
sorce codeはblogの制約から次の記事に載せる。
---------
6.xamppフォルダーのxampp-control.exeを起動しAppache(web Server)とMySQL(DB Server)のStartボタンを押し緑色にする。
7.WEBのURLに”http://localhost/”と入力し実行するとXAMPPのdashbordが表示されるので右上のphpMyAdminを押す。
8.phpMyAdminの新規作成を押し"youtube”データベースを作成する。
9.”youtube”データベースを選んで上の
7.index2.phpをhtdocsフォルダーに保存する。
8.webのuriに”http://localhost/index2.php”と入力しenterを押す。
9.webに
 [ファイルの選択]ファイルが選択されていません[送信]
 と表示されるので、[ファイルの選択]を押してダイヤグラムからxamppのhtdocsの中のBook1.xlsxを選択する。
10.[ファイルが選択されていません]のところに”Book1.xlsx"と表示されるので、[送信]を押す。
11.xamppのphpMyAdminの