質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

3回答

1723閲覧

PHP データベースにinsertできない

kokok

総合スコア145

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

1グッド

0クリップ

投稿2019/11/19 05:50

php

1//MyPDO 2 3<?php 4namespace DBIO; 5 6use PDO; 7use PDOException; 8 9class MyPDO{ 10 11 protected $dsn; 12 protected $user; 13 protected $pass; 14 protected $pdo; 15 16 function __construct(){ 17 18 $this->dsn = "mysql:dbname=test;host=localhost;charset=utf8mb4"; 19 $this->user = "root"; 20 $this->pass = ""; 21 $this->connect(); 22 } 23 24 function connect(){ 25 26 $this->pdo = new PDO($this->dsn, $this->user, $this->pass, array( 27 PDO::ATTR_PERSISTENT => true 28 )); 29 } 30 31 function close(){ 32 33 $this->pdo = null; 34 } 35} 36

php

1 2//UserMyPDO 3<?php 4 5 6require_once 'MyPDO.php'; 7 8use DBIO\MyPDO; 9 10class UserMyPDO extends MyPDO{ 11 12 13 14 15 //ユーザ登録 16 17 function userRegistration($name,$pass,$user_or_clerk){ 18 19 20 try { 21 22 $sql = "INSERT INTO users(user_name, password, user_or_clerk) VALUES(?,?,?)"; 23 24 $this->connect(); 25 $stmt = $this->pdo->prepare($sql); 26 27 $stmt->bindParam(1,$name); 28 $stmt->bindParam(2,$pass); 29 $stmt->bindParam(3,$user_or_clerk); 30 31 // 実行結果 32 $result = $stmt->execute(); 33 } catch (PDOException $e) { 34 print('PDOException:' . $e->getMessage()); 35 } finally{ 36 37 $this->close(); 38 } 39 //実行結果が成功か失敗かを返す: true/flse 40 return $result; 41 } 42 43}

html

1 2//create_user.html 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6<meta charset="utf-8"> 7<link rel="stylesheet" href="style.css"> 8<link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet"> 9<title>ユーザ作成</title> 10</head> 11<body> 12<header class="top-header"> 13<h1 class="site-title">サイト名</h1> 14<nav class="top-nav"> 15 <ul> 16 <li><form action="registration-screen-transition.php"><button type="submit" name="button">画面登録</button></form></li> 17 <li><button type="button" name="button">ログイン</button></li> 18 </ul> 19</nav> 20</header> 21<div class="content-space"> 22</div> 23<div class="image-registration-space2" > 24<h1>ユーザ登録</h1> 25<form action="create_user_confirmation.php" method="post"> 26<table class="table-center" > 27<tr><td class="td1" align="center">ユーザ名</td> 28<td align="left"><input type="text" name="username" required ></td></tr> 29<tr><td class="td1" align="center"><label for="image-info" >パスワード</label></td> 30<td align="left"><input type="text" name="pass" required ></td></tr> 31</table> 32<button class="image-registration-button" type="submit">作成</button> 33</form> 34<form action="index.php"> 35<button type="submit">戻る</button> 36</form> 37</div> 38<div class="content-space"> 39</div> 40<div class="content-space"> 41</div> 42<footer class="top-footer"> 43<nav class="footer-nav"> 44 <ul class="table-ul"> 45 <li><i class="fab fa-twitter-square fa-2x my-skyblue fa-fw"></i><button>SNS</button></li> 46 <li><i class="fas fa-envelope-square fa-2x my-white fa-fw"></i><button>お問い合わせ</button></li> 47 <li><i class="far fa-arrow-alt-circle-right fa-2x my-white fa-fw"></i><button>etc...</button></li> 48 <li><i class="far fa-arrow-alt-circle-up fa-2x my-white fa-fw"></i><a href="#">TOP</a></li> 49 </ul> 50</nav> 51<img class="footer-logo" src="logo.png" alt=""> 52</footer> 53</body> 54</html> 55

php

1 2//create_user_confirmation.php 3 4<?php 5 6 7require_once 'UserMyPDO.php'; 8 9 10$pdo = new UserMyPDO(); 11 12 13require_once( __DIR__ . DIRECTORY_SEPARATOR.'smarty'. DIRECTORY_SEPARATOR. 'Smarty.class.php'); 14$smarty = new Smarty(); 15 16$smarty->template_dir = './templates/'; 17 18$smarty->compile_dir = './templates_c/'; 19 20 21$user_name = $_REQUEST["username"]; 22$pass = $_REQUEST["pass"]; 23 24//trueはユーザモード 25$user_or_clerk = true; 26 27 28$result = $pdo->userRegistration($user_name,$pass,$user_or_clerk); 29 30if($result == true){ 31 32 $smarty->assign('success', 'ユーザ登録完了しました'); 33 34 $smarty->display('create_user_success.html'); 35 36}else{ 37 $smarty->assign('failure', 'ユーザ登録失敗しました'); 38 39 $smarty->display('create_user_failure.html'); 40}

create_user.htmlで入力されたユーザ名とパスワードをcreate_user_confirmation.phpで受け取り

MyPDOを継承したUserMyPDO でデータベースと接続してデータを追加したいのですが

//UserMyPDOの
$result = $stmt->execute();
部分で結果がflaseになってしまいます。

xamppで開発していますが、phpMyadmin で 直接テーブルに対して

sql

1INSERT INTO users(user_name, password, user_or_clerk) VALUES('root','root',true)

と実行すると追加できます。

エラーも出ておらず、コードの見直しなど、何度も行ったのですが、原因が特定できなかったのでアドバイス頂けると助かります。

DDL

1CREATE TABLE users( 2 user_id INT(11) AUTO_INCREMENT NOT NULL, 3 user_name VARCHAR(20) NOT NULL, 4 password VARCHAR(20) NOT NULL, 5 user_or_clerk boolean, 6 PRIMARY KEY(user_id) 7); 8 9CREATE TABLE product( 10 product_id INT(11) NOT NULL, 11 product_name VARCHAR(20), 12 category VARCHAR(20), 13 price INT(11), 14 stock INT(11), 15 image VARCHAR(40), 16 PRIMARY KEY(product_id) 17); 18 19CREATE TABLE purchase_history( 20 user_id INT(11), 21 product_id INT(11), 22 purchase_quantity INT(11), 23 last_purchase_date date, 24 PRIMARY KEY(user_id,product_id), 25 FOREIGN KEY (user_id) REFERENCES users(user_id), 26 FOREIGN KEY (product_id) REFERENCES product(product_id) 27); 28
退会済みユーザー👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

function connect にて、

new PDOするところで例外を発生できるオプション(PDO::ERRMODE_EXCEPTION)を与えることと、

function userRegistrationにて、

bindParamはbindValueした方がいいのと、

かな。
そもそもphpからのDB接続単体のテストを行って
OKならいいんだけど、やってあるかな? って話。

投稿2019/11/19 06:18

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

try-catchするなら、MyPDOのconnect()でエラーモードの設定をしてください

PHP

1 function connect(){ 2 $this->pdo = new PDO($this->dsn, $this->user, $this->pass, array( 3 PDO::ATTR_PERSISTENT => true , 4 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 5 )); 6 }

投稿2019/11/19 06:02

yambejp

総合スコア114779

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kokok

2019/11/19 06:41

回答ありがとうございます。 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION を指定することによって、データベース名の指定が間違えていることに気づけました。 ありがとうございました。
guest

0

PHPの$stmt->の部分ですが、$this->stmt->にする必要はないでしょうか??
自分も勉強中の身なので、検討違いでしたらごめんなさいm(_ _)m

追記:
コメント欄にあるように、こちらは間違いでした。失礼いたしましたm(_ _)m

投稿2019/11/19 06:05

編集2019/11/19 10:39
H40831

総合スコア973

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2019/11/19 07:18

>$stmt = $this->pdo->prepare($sql); とあるように当該メソッドで初めて宣言されているので不要です。 $stmtがMyPDO、またはUserMyPDOのプロパティとして宣言されていれば$this->stmtが必要です。
H40831

2019/11/19 07:48

ほんとですね。 コードリーディングもコーディングもまだまだ未熟です。精進します。
m.ts10806

2019/11/19 10:31

いえ、回答取り消し線いれなくても良いとは思いますよ。 強いてなら「認識が違った部分があった」と追記するくらいにしておきましょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問