発生している問題・エラーメッセージ
Notice: Undefined variable: db in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/dbset.php on line 68 NULL Notice: Undefined variable: db in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/dbset.php on line 70 Fatal error: Uncaught Error: Call to a member function prepare() on null in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/dbset.php:70 Stack trace: #0 /Applications/MAMP/htdocs/Sunseer_PHP_BBS/finish.php(12): DBset->insert() #1 {main} thrown in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/dbset.php on line 70
該当のソースコード
php
1public function connect(){ 2 try { 3 var_dump($this->dsn,$this->user,$this->passwd); 4 $db = new PDO($this->dsn,$this->user,$this->passwd); 5 $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 6 //echo "connect完了"; 7 //$s=$db->query("select * from db_BBS"); 8 9 10 } catch (\PDOException $e) { 11 echo "connectエラー:{$e->getMessage()}"; 12 } 13} 14// TODO select 用のfunc作る 15// public function select(){ 16// 17// 18// 19// 20// } 21// TODO insert 用のfunc作る 22public function insert(){ 23 24 $this->connect(); 25 26 var_dump($db); 27 try { 28 $stat = $db->prepare("INSERT INTO db_BBS(id,name1,name2,sex,birth,mail,phone,college,subject,hobby,icon) 29 VALUES(:id,:name1,:name2,:sex,:birth,:mail,:phone,:college,:subject,:hobby,:icon)"); 30 echo "insert完了"; 31 $stat->bindValue(':id',NULL); 32 $stat->bindValue(':name1',$this->name1); 33 $stat->bindValue(':name2',$this->name2); 34 $stat->bindValue(':sex',$this->sex); 35 $stat->bindValue(':birth',$this->birth); 36 $stat->bindValue(':mail',$this->mail); 37 $stat->bindValue(':phone',$this->phone); 38 $stat->bindValue(':college',$this->college); 39 $stat->bindValue(':subject',$this->subject); 40 $stat->bindValue(':hobby',$this->hobby); 41 $stat->bindValue(':icon',"Image/icon.05.42.png"); 42 echo "insert完了"; 43 $stat->execute(); 44 45 // echo "<br>insert完了";}else{ 46 // echo "エラー"; 47 // } 48 // $stat->execute(); 49}catch (\PDOException $e) { 50 echo "insertエラー:{$e->getMessage()}"; 51} 52}
connect()関数をinsert()関数内で使いたいのですが、うまく機能しません。
おんなじ経験のある方どうかご教授おねがしいます。
connect();同様に機能しないです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/18 06:32