I am writing here because I am new in Docker world. In the past, I was using Vagrant in developing stuff. Installing OS + packages that I need was very easy. Now, Vagrant is load more resources than I expected. I am changing to Docker and I made PHP environment. In docker-compose.yml load LAMP: Linux, Apache, MariaDB, and PHP as below.
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
and in Dockerfile, I made this:
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
Now in my sign up page, I made this:
<?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();
}
In this post, I delete lines for validation to shortcut the question. In example above, if I test the sign up, the inserting not make insert in database.
Q1. How insert into database?
Q2. How save changes in database’s structures after finish or for completing development in the future?
Updating:
Here, how I am connecting to database:
<?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!";
}
8
To save Database make volume for database files by docker-compose.yml file:
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
to write in database, the php was missing records:
<?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 was missing.
$record_db->close(); //this for closing.
header("Location: ../index.php?status=User Registe Seccessfully!");
exit();
}
}else{
header("Location: ../index.php");
exit();
}