0

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!";
}
asked Sep 6, 2024 at 19:03
8
  • keep the database file outside the container Commented Sep 6, 2024 at 19:47
  • @nbk do you know how to make it? Commented Sep 6, 2024 at 20:08
  • see stackoverflow.com/questions/68278116/… Commented Sep 6, 2024 at 20:29
  • @nbk database's structures are saved, but still I cannot insert any data. Commented Sep 6, 2024 at 20:55
  • For a start, restrict yourself to one container running PHP code and another with the DB. Then, how exactly are you running that code? Also, how exactly are you determining that it does not insert in database? Howe did you even set up the DB for storage? All those details with irrelevant parts removed make up a minimal reproducible example. Please, as a new user here, also take the tour and read How to Ask. Commented Sep 6, 2024 at 21:35

1 Answer 1

0

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();
}
answered Sep 20, 2024 at 19:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.