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?

phpのSQL処理で新規登録のidを取得

Last updated at Posted at 2025-03-26

参考処理

Person.php
<?php

require_once( dirname( __FILE__ , 2) . '/Mysql_root/MySQL_Data.php' );

class Person{

    public $sqlDB;
    public $dbname="XXXXX";//作成したデータベースの名前
    public $db;

    public function __construct(){
        $this->sqlDB=new MySQL_Data();
    }

    public function Insert_Member(){
        try {
            $TableName = 'person';
        
            $colum1 = 'person_id';//オートインクリメント
        
            $colum2 = 'name';
            $colum3 = 'name_kana';

            $colum4 = 'memo';
            $colum6 = 'deleted_flg';
        
            $colum_cmp1 = 'address';//company のテーブル
            $colum_cmp2 = 'postal_code';//company のテーブル
        //-------------------------------//
        
        
        $valueList=[
            //$colum1 = 'user_id';//オートインクリメント
            $colum2 =>  '',
            $colum3 =>  '',
            $colum4 =>  '',
            //$colum5 =>  '',
        ];
        
        foreach ($_POST as $key => $value) {
            if ( isset($_POST[$key]) ) {
                $valueList[$key]= $_POST[$key];
            }
        }
        //-----------------------------//
        
        
            $sql = 'INSERT INTO '.$TableName.' ( '.$colum2.','.$colum3.','.$colum4.') values (?, ?, ?)';//$colum11.','.$colum12.','.$colum13.','.$colum14.','.$colum15.','.$colum16.',) values (?, ?, ?, ?, ?,?,?,?,?, ?, ?, ?, ?,?)';
        
            //$img_data = file_get_contents($_FILES['photo']['tmp_name']);
        
            $count=$statement = $this->db->prepare($sql);

            $statement->bindParam(1, $valueList[$colum2], PDO::PARAM_STR); 
            $statement->bindParam(2, $valueList[$colum3], PDO::PARAM_STR);
            $statement->bindParam(3, $valueList[$colum4], PDO::PARAM_STR); 
            //$statement->bindParam(4, $icon_url_id, PDO::PARAM_INT); // intへのキャストはしない
            $res = $statement->execute();
            
        // 登録したデータのIDを取得して出力
        $id = $this->db->lastInsertId();
        var_dump($id);
        echo intval($id);
        return intval($id);
        // var_dump($statement->lastInsertId());
        // return $statement->lastInsertId();
    }catch (PDOException $e) {
        echo 'DB接続エラー: ' . $e->getMessage();
        return null;
    }

}



}
MySQL_Data.php
<?php 

 class MySQL_Data{


    public $IPaddress;
    public $User_Name;
    public $PassWord;

    public $dbname;//作成したデータベースの名前
    public $db;

    public function __construct(){


        $this->dbname = 'XXXXX';
        $this->IPaddress='XXXXX';
        $this->User_Name='XXXX';
        $this->PassWord='XXXX';
    
           // echo 'SQLへ接続';
            try {
                $this->db = new PDO("mysql:host={$this->IPaddress};dbname={$this->dbname}",$this->User_Name,$this->PassWord);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              //  echo"接続成功";
            } catch (PDOException $e) {
                echo 'DB接続エラー: ' . $e->getMessage();
                echo '現在のIPアドレスの設定 : '.$this->IPaddress;
                echo '現在のユーザー名 : '.$this->User_Name;
                echo '現在のパスワード : '.$this->PassWord;
                echo '現在のデータベース名 : '.$this->dbname;
            }
        }

}
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?