新卒から文系エンジニア→人材業界に転職した人のブログ

新卒から文系エンジニア→人材業界に転職。技術・スキルがないためブログを通して勉強。その後、IT業界の業界知識が活かせる人材業界へ。異業種×異職種の転職経験有り。

このエントリーをはてなブックマークに追加

【php・Javascript・sql】DB連携~CUIでテーブルを定義する・ ②複数の値をPOSTし、DBへ登録する~

【今回の紹介】


今回はDBとWebtodoリストを連携する過程で勉強になったことを紹介します。


まとめると、


①CUIでテーブルを定義する

②複数の値をPOSTし、DBへ登録する

③現在のデータを見て、SQLを発行する

④再度リクエストしても整合性のあるidが発行される


 
ここらへんが勉強になりました。今回は、

①CUIでテーブルを定義する

②複数の値をPOSTし、DBへ登録する

を紹介します。


【内容】

①CUIでテーブルを定義する


すでに作成されているテーブルに値を入れたり、変更したりすることはあったが、

自分でテーブルそのものを作る機会は新鮮でした。



以下操作時のCUIでのコマンド。
 
//テーブルの定義
create database db_01; 
→create database DB名


//使用するDBの指定
use db_01;
→use DB名


//テーブルの作成とカラムの定義
create t_01(id smalist,name varchar(60),category char(11),del_flg(1),update_time datetime);

→create テーブル名(カラム名 データ型,カラム名 データ型,,,,,)


//カラムの定義の確認
show columns from t_01;
→show columns from テーブル名

describe t_01;
→describe テーブル名



//プライマリーキーの変更
alter table t_01 add primary key(id);
alter テーブル名 add primary key(カラム名);

//not null制約の変更
alter table t_01 modify column name varchar(60) not null;
alter table テーブル名 modify column カラム名 データ型 not null;



※私の場合は、not null制約・プライマリーキーの設定をテーブル定義に忘れてしまっていたため、
alterを使用し、変更かけています。


本来はテーブル定義時に同時に宣言するものです。


create table db_01.t_01(name varchar(60) not null primary key ,,,,,)
create db名.テーブル名(カラム名 データ型 not null primary key )



※このテーブル定義時に、データ型の事を調べたのが
です。

http://forse.hatenablog.com/entry/2014/02/08/182114


②複数の値をPOSTし、DBへ登録する


「,」区切りでパラメータを渡し、php側で「,」区切りに配列に代入し、DBへ格納します。


わかりやすいようにソースから抜粋します。


処理の流れとしては、


①「,」区切りで値を代入

phpファイルへPOST

③explode関数で「,」ごとに配列に代入

④SQL文の作成



というかんじです。



抜粋ソース



入力画面(①「,」区切りで値を代入・②phpファイルへPOST)

send_value=send_text1+","+send_text2+","+send_text3;
httpobj.send("send_value="+send_value);					


処理画面(③explode関数で「,」ごとに配列に代入・④SQL文の作成)

$input=$_POST['send_value'];
$inputs=explode(",",$input);    
$stmt=$mysqli->prepare("insert into t_01 values('$inputs[0]','$inputs[1]','$inputs[2]','0','$time')") or exit("error");


「メモ」
explode()関数は、区切り文字として値を与えればカンマだけでなく、
文字列やスペースなどもできるそうです。

参考:http://www.php.net/manual/ja/function.explode.php


全体ソース

入力画面

	send_value=send_text1+","+send_text2+","+send_text3;
	var url="insert_db.php";
	var httpobj =createHttpRequest();
	httpobj.onreadystatechange = function() {
  		if (httpobj.readyState == 4) {
    		alert(httpobj.statusText);
  		}
		};
	httpobj.open("POST",url);
	httpobj.setRequestHeader('Pragma', 'no-cache');
	httpobj.setRequestHeader('Cache-Control', 'no-cache');
	httpobj.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
	httpobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpobj.send("send_value="+send_value);

※全体とか言って全体じゃないですが、、、


処理画面

<?php
  header('Content-Type: text/html; charset=UTF-8');
    $input=$_POST['send_value'];
    $inputs=explode(",",$input);
    $time=date("Y-m-d H:i:s");
    $mysqli=new mysqli('localhost','sa','qwertyuiop@1','db_01');
		if($mysqli->connect_error){echo "DB接続できませーん";}
	$stmt=$mysqli->prepare("SET NAMES utf8");
	$stmt->execute();	
	$stmt=$mysqli->prepare("insert into t_01     
        values('$inputs[0]','$inputs[1]','$inputs[2]','0','$time')") or exit("error");
	$stmt->execute();
	$mysqli->close();

?>
.hatena-module:nth-of-type(10) { background: transparent; } .hatena-module:nth-of-type(10) .hatena-module-title{ display: none; } .hatena-module:nth-of-type(10) .hatena-module-body { padding: 0; }