shinozaki0815
@shinozaki0815

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

DB接続できない(php、mysql)

解決したいこと

DB接続ができずに掲示板のユーザー名やパスワードなどのデータをDBに保存できない

例)
PHP,HTML,CSS,Docker,Smartyを使用しています。
解決方法を教えて下さい。

docker-compose.yml

services:
    app:
        build: 
            context: ./docker/php
            dockerfile: Dockerfile
        volumes:
            - ./application/docroot:/var/www/html
            - ./application/private:/var/www/private
        ports:
            - "8080:80"
        depends_on:
            - db
           
        
    db:
        build: 
            context: ./docker/mysql
            dockerfile: Dockerfile
        volumes:
            - ./docker/mysql/data:/var/lib/mysql
        ports:
            - "3306:3306"
        environment:
            -  MYSQL_ROOT_PASSWORD=pass

login.php

require_once '../private/common.php';
require_once '../private/smarty/smarty.php';

$smarty = getSmarty::getSmarty();


$smarty->display('login.tpl');

login_c.php

require_once '../private/common.php';
require_once '../private/smarty/smarty.php';

header('Location: thread_index.php');

$smarty = getSmarty::getSmarty();

$userEmail = isset($_POST['email']) ? $_POST['email'] : '';
$userPassword = isset($_POST['password']) ? $_POST['password'] : '';
if (empty($userEmail)) {
    echo $emailAlert = "<script type='text/javascript'>alert('メールアドレスを入力してください。');</script>";
    echo '<a href="login.php">戻る</a>';
} else if (empty($userPassword)){
    echo $pwAlert = "<script type='text/javascript'>alert('パスワードを入力してください。');</script><br />";
    echo '<a href="login.php">戻る</a>';
}

//DB接続
$mysqli = dbProcess::getConnection();
//DBからデータを取得
$userAccess = dbProcess::getUser($mysqli);
//DBとの接続を解除
$mysqli->close();

if (is_null($userAccess)) {
    echo $loginAlert = "<script type='text/javascript'>alert('メールアドレスまたはパスワードが正しくありません。');</script><br />";
    echo '<a href="login.php">戻る</a>';
} else {
    session_start();
    $_SESSION['userInfo'] = $userAccess;
}

common.php


if ( !defined('INCLUDES_DIR') ) {
    define( 'INCLUDES_DIR', dirname(__FILE__) );
}

class dbProcess
{

    public static function getConnection()
    {
        // データベース接続処理する        
        $mysqli = new mysqli('db','root','pass','keijiban_db');
        if($mysqli->connect_error){
            echo $mysqli->connect_error;
        }
        $mysqli->set_charset("utf8");
     
        return $mysqli;
    }

    
     public static function setUser($mysqli)
    {
        //登録するデータを用意
        $email = $_POST['signup_email'];
        $password = $_POST['signup_password'];
        $user_name = $_POST['signup_user_name'];

        // クエリ実行
        $query = "INSERT INTO users (email, password , user_name) VALUES (?, ?, ?)";

        // プリペアドステートメントの用意
        $stmt = $mysqli->prepare($query);

        // 登録するデータをセット
        $stmt->bind_param('sss', $email, $password, $user_name);

        // 登録実行
        $stmt->execute();

        return $mysqli;

    }


    public static function getUser($mysqli)
    {
        $email = $_POST['email'];
        $password = $_POST['password'];
        $stmt = $mysqli->prepare('SELECT email, user_name FROM users WHERE email = ? AND password = ? ');
        $stmt->bind_param('ss', $email, $password);
        $stmt->execute();

        //結果を取得

        $result = $stmt->get_result();

        $userAccess = null;
        if ($user = $result->fetch_array(MYSQLI_ASSOC)) {
            $userAccess = $user;
        }

        return $userAccess;
    }

smarty.php

require_once 'libs/Smarty.class.php';
    class getSmarty {
        public static function getSmarty()
        {
            $smarty = new Smarty();
            $smarty->template_dir = INCLUDES_DIR . "/smarty/templates/";
            $smarty->compile_dir =INCLUDES_DIR . "/smarty/templates_c/";
            return $smarty;
        }
    }

sign_up.php

require_once '../private/common.php';
require_once '../private/smarty/smarty.php';

$smarty = getSmarty::getSmarty();

$smarty->display('signup.tpl');

sign_up_c.php

require_once '../private/common.php';
require_once '../private/smarty/smarty.php';
require_once '../private/validation.php';

$smarty = getSmarty::getSmarty();

$userEmail = $_POST['signup_email'];
$userPassword = $_POST['signup_password'];
$userName = $_POST['signup_user_name'];
$confirmPassword = $_POST['password_cf'];

$signupValidation = Validation::signupCheck($userEmail);


if (!empty($userEmail && $userPassword && $userName && $confirmPassword) && ($userPassword == $confirmPassword) && count($signupValidation) == 0) {

$mysqli = dbProcess::getConnection();

$setUser = dbProcess::setUser($mysqli);

$mysqli->close();

} else if (count($signupValidation) > 0) {
    $smarty->assign('email_errors', $signupValidation['email']);
    $smarty->assign('password_errors', $signupValidation['password']);
    $smarty->assign('username_errors', $signupValidation['username']);
} else if (empty($userEmail || $userPassword || $userName)) {
    echo $signupAlert = "<script type='text/javascript'>alert('入力漏れがあります。もう一度ご確認ください。');</script><br />";
    echo '<a href="signup.php">戻る</a>';
    exit;
}

header('Location: ./login.php');
0

1Answer

Comments

  1. @shinozaki0815

    Questioner

    ありがとうございます。このコードを追加して、ターミナルで{docker-compose up}をすればよろしいのですか?

Your answer might help someone💌