0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pdo prepared statment(データをdbに格納するまで)

Last updated at Posted at 2022-09-07

基本のpdoでisnert

insert.php
<?php
mb_internal_encoding("utf8"); //DBへデータ格納の際の文字化け防止

$pdo = new PDO(" mysql:dbname=db名; host=localhost;" , "root","root");
//$pdo=new PDO  DB接続の決まり文句
//mysql~db名; mysqlに接続し、db名を使用する
//host~ 使用するDB用のサーバー名を記述する (今回はmampのためlocalhost)
//IDとpw pwは""の場合もあり
//実際はスペースなしで記述

$pdo->exec("insert into テーブル名(カラム名、カラム名) values(' " .$_POST['箱の名前']." ' , ' " .$_POST['箱の名前']. " ');" );
//insert intoは通常のsqlと同じ
//~valuesの後は 「'」「"」「.」で囲む
?>

prepared statment版

・予め(prepare) SQL文の型を書いておくこと
・place holderは疑問文型

insert.php
<?php
 mb_internal_encoding("utf8");

$pdo = new PDO("mysql:dbname=test;host=localhost;","root","root");

$stmt = $pdo->prepare("insert into テーブル名 (name,mail,password,picture,comments) value(?,?,?,?,?)" );

$stmt->bindValue(1,$_post['name']);
$stmt->bindValue(2,$_post['mail']);
$stmt->bindValue(3,$_post['password']);
$stmt->bindValue(4,$_post['picture']);
$stmt->bindValue(5,$_post['comments']);

$stmt->execute(); //insert実行
$pdo=NULL; //pdo切断

?>

・bindValue プリペアードステートメントSQL文で、対応する疑問符(名前)に値をバインド(紐付け)する

execute 実行という意味
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?