如何使用docker向MariaDB中插入数据并保存数据库的更改?

问题描述 投票:0回答:1

我写在这里是因为我是 Docker 世界的新手。过去,我使用 Vagrant 来开发东西。安装我需要的操作系统和软件包非常简单。现在,Vagrant 加载的资源比我预期的要多。我正在更改为Docker并创建了PHP环境。在 docker-compose.yml 中加载 LAMP:Linux、Apache、MariaDB 和 PHP,如下所示。

services:
foo_php:
    build: .
    volumes:
        - ./src:/var/www/html
    ports:
        - 9002:80
foo_db:
    image: mariadb:latest
    restart: always
    environment:
        MARIADB_ROOT_PASSWORD: Password1234
        MYSQL_ROOT_HOST: '%'
    volumes:
        - ./mariadb:/var/lib/mysql
phpmyadmin:
  image: phpmyadmin:latest
  restart: always
  ports:
    - 9003:80
  environment:
    - PMA_ARBITRARY=1enter code here

在 Dockerfile 中,我做了这个:

FROM php:8.3-apache

MAINTAINER Senior Moe <[email protected]>

WORKDIR /var/www/html

RUN apt update -y && apt upgrade -y && apt install libmariadb-dev -y

USER root

RUN sed -i -e "/^bind-address/d" /etc/mysql/my.cnf

RUN docker-php-ext-install mysqli

现在在我的注册页面中,我做了这个:

    <?php
include_once "connection_db.php";

if(isset($_POST['your_name']) && isset($_POST['your_email']) && isset($_POST['your_passwrd']) && isset($_POST['repeat_passwrd']) ){
   function validation($input){
       $input = trim($input);
       $input = stripslashes($input);
       $input = htmlspecialchars($input);

       return $input;
   }

   $username = validation($_POST['your_name']);
   $user_email = validation($_POST['your_email']);
   $user_password = validation($_POST['your_passwrd']);
   $repeat = validation($_POST['repeat_passwrd']);

  
       $check_query = "SELECT * FROM users WHERE user_email='$user_email'";
       $result_check = mysqli_query($connection, $check_query);
       if(mysqli_num_rows($result_check) === 1 ){
           header("Location: ../index.php?registererror=The Email already exist");
           exit();
       }else{
           $hashed_password = password_hash($user_password, PASSWORD_DEFAULT);
           $record_db = $connection->prepare("INSERT INTO users (username, user_email, user_password) VALUES (?,?,?)");
           $record_db->bind_param("sss",$username, $user_email, $user_password);
           header("Location: ../index.php?status=User Registe Seccessfully!");
           exit();
       }
   
}else{
    header("Location: ../index.php");
    exit();
}

在这篇文章中,我删除了验证行以简化问题。在上面的示例中,如果我测试注册,则插入不会在数据库中插入。

Q1。如何插入数据库? Q2。完成后或将来完成开发时如何保存数据库结构的更改?

更新中: 在这里,我如何连接到数据库:

<?php

$server_db = [
    "server_name" => "foo_db",
    "server_user" => "root" ,
    "server_passwd" => "Password1234",
    "database_name" => "FOO_DB"
];

$connection = mysqli_connect($server_db['server_name'],$server_db['server_user'],$server_db['server_passwd'],$server_db['database_name']);

if(!$connection){
    echo "connection faild!";
}
php docker mariadb lamp
1个回答
0
投票

通过 docker-compose.yml 文件为数据库文件保存数据库创建卷:

services:
foo_php:
    build: .
    volumes:
        - ./src:/var/www/html
    ports:
        - 9002:80
foo_db:
    image: mariadb:latest
    restart: always
    environment:
        MARIADB_ROOT_PASSWORD: Password1234
        MYSQL_ROOT_HOST: '%'
    volumes:
        - ./mariadb:/var/lib/mysql #here
phpmyadmin:
  image: phpmyadmin:latest
  restart: always
  ports:
    - 9003:80
  environment:
    - PMA_ARBITRARY=1
volumes:
  mariadb: #here

写入数据库,php缺少记录:

<?php
include_once "connection_db.php";

if(isset($_POST['your_name']) && isset($_POST['your_email']) && isset($_POST['your_passwrd']) && isset($_POST['repeat_passwrd']) ){
   function validation($input){
       $input = trim($input);
       $input = stripslashes($input);
       $input = htmlspecialchars($input);

       return $input;
   }

   $username = validation($_POST['your_name']);
   $user_email = validation($_POST['your_email']);
   $user_password = validation($_POST['your_passwrd']);
   $repeat = validation($_POST['repeat_passwrd']);

  
       $check_query = "SELECT * FROM users WHERE user_email='$user_email'";
       $result_check = mysqli_query($connection, $check_query);
       if(mysqli_num_rows($result_check) === 1 ){
           header("Location: ../index.php?registererror=The Email already exist");
           exit();
       }else{
            $hashed_password = password_hash($user_password, PASSWORD_DEFAULT);
           $record_db = $connection->prepare("INSERT INTO users (username, user_email, user_password) VALUES (?,?,?)");
           $record_db->bind_param("sss",$username, $user_email, $hashed_password);
       $record_db->execute(); //this is was missing.
       $record_db->close(); //this for closing. 
           header("Location: ../index.php?status=User Registe Seccessfully!");
           exit();
       }
   
}else{
    header("Location: ../index.php");
    exit();
}
© www.soinside.com 2019 - 2024. All rights reserved.