Zapisywanie danych php do pliku php

PHP  Założony przez  Patryk Stefański.

Chcę zrobić instalator do apki i nie mogę zapisać danych bazy do pliku php
Obecnie mam to, ale pewnie wszystko źle
if (file_exists(INC.'dbcfg.php')) {
        require_once INC"dbcfg.php";
    } else {
        $file fopen(INC."dbcfg.php""a");
        $content[] = [
            '<?php',
            '$db_host = "'.$dbhost.';"',
            '$db_user = "'.$dbuser.';"',
            '$db_pass = "'.$dbpass.';"',
            '$db_name = "'.$dbname.';"',
        ];
        fwrite($file$content);
        fclose($file);
    
fwrite przyjmuje jako drugi parametr stringa, a Ty aktualnie podajesz mu tablicę.
Okej, a da się jakoś zrobić tak, żeby zrobić to za pomocą tablicy? Albo przekonwertować tablice do stringa,
Czy trzeba na około i każdą linijkę kodu osobno ?
https://www.php.net/manual/en/function.implode.php
Rozdrobniłem sobie to wszystko, ale mam teraz problem. W ogóle kod nie chce się wykonać
Tylko ten if się wykonuje
if (file_exists(INC.'dbcfg.php')) { 
Normalnie i z else
<?php
ini_set
('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);
require_once 
'../engine.php';
require_once 
INC'incCSS.html';
require_once 
INC'incJS.html';
if (
file_exists(INSTALL'lock')) {
    echo 
'<script>window.location.href = "/index.php"</script>';
}

if (isset(
$_POST['dbhost']) && isset($_POST['dbuser']) && isset($_POST['dbpass']) && isset($_POST['dbname']) && isset($_POST['user']) && isset($_POST['email']) && isset($_POST['pass']) && isset($_POST['pass1'])) {
    
$dbhost filter_input(INPUT_POST'dbhost'FILTER_SANITIZE_STRING);
    
$dbuser filter_input(INPUT_POST'dbuser'FILTER_SANITIZE_STRING);
    
$dbpass filter_input(INPUT_POST'dbpass'FILTER_SANITIZE_STRING);
    
$dbname filter_input(INPUT_POST'dbname'FILTER_SANITIZE_STRING);
    
$content1 '<?php';
    
$content2 '$db_host = "'.$dbhost.'";';
    
$content3 '$db_user = "'.$dbuser.'";';
    
$content4 '$db_pass = "'.$dbpass.'";';
    
$content5 '$db_name = "'.$dbname.'";';

    
$user filter_input(INPUT_POST'user'FILTER_SANITIZE_STRING);
    
$email filter_input(INPUT_POST'email'FILTER_VALIDATE_EMAIL);
    
$email filter_var($emailFILTER_SANITIZE_EMAIL);
    
$pass filter_input(INPUT_POST'pass'FILTER_SANITIZE_STRING);
    
$pass1 filter_input(INPUT_POST'pass1'FILTER_SANITIZE_STRING);

    if (
file_exists(INC.'dbcfg.php')) {
        require_once 
PAGES'database.php';
    } else {
        
$file fopen(INC."dbcfg.php""a");
        
fwrite($file$content1);
        
fwrite($file"\n");
        
fwrite($file$content2);
        
fwrite($file"\n");
        
fwrite($file$content3);
        
fwrite($file"\n");
        
fwrite($file$content4);
        
fwrite($file"\n");
        
fwrite($file$content5);
        
fwrite($file"\n");
        
fclose($file);
        require_once 
PAGES'database.php';
    }
    
    
//Sprawdzenie długości nicka
    
if ((strlen($user)<3) || (strlen($user)>20)) {
        
$reg_ok false;
        
$_SESSION['register_error'] = "Twój login musi posiadać od 3 do 20 znaków !";
        echo 
'<script>window.location.href = "/install/install.php"</script>';
    }
    
//Sprawdź poprawność nick'a
    
if (ctype_alnum($user) == false) {
        
$reg_ok false;
        
$_SESSION['register_error'] = "Twój login musi się składać tylko z liter i cyfr bez polskich znaków !";
        echo 
'<script>window.location.href = "/install/install.php"</script>';
    }
    
// Sprawdź poprawność adresu email
    
$email filter_input(INPUT_POST'email'FILTER_VALIDATE_EMAIL);
    
$email filter_var($emailFILTER_SANITIZE_EMAIL);
    if (empty(
$email)) {
        
$reg_ok false;
        
$_SESSION['register_error'] = "Twój email nie jest poprawny !";
        echo 
'<script>window.location.href = "/install/install.php"</script>';
    }
    
//Sprawdzenie długości hasła
    
if ((strlen($pass)<8) || (strlen($pass)>20)) {
        
$reg_ok false;
        
$_SESSION['register_error']="Hasło musi posiadać od 8 do 20 znaków!";
        echo 
'<script>window.location.href = "/install/install.php"</script>';
    }
    
//Sprawdź poprawność hasła
    
if ($pass != $pass1) {
        
$reg_ok false;
        
$_SESSION['register_error'] = "Podane hasła różnią się !";
        echo 
'<script>window.location.href = "/install/install.php"</script>';
    }
    
    if (
$reg_ok == true) {
        
$sql1 "CREATE TABLE `_news` (
            `nid` int(11) NOT NULL,
            `title` text NOT NULL,
            `body` varchar(128) NOT NULL,
            `author` text NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
;
        
$sql2 "CREATE TABLE `_users` (
            `uid` int(11) NOT NULL,
            `created` date NOT NULL,
            `name` text NOT NULL,
            `password` varchar(128) NOT NULL,
            `email` varchar(32) NOT NULL,
            `gid` int(11) NOT NULL,
            `avatar` varchar(32) NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
;
        
$sql3 "ALTER TABLE `_news`
        ADD PRIMARY KEY (`nid`);ALTER TABLE `_users`
        ADD PRIMARY KEY (`uid`);ALTER TABLE `_news`
        MODIFY `nid` int(11) NOT NULL AUTO_INCREMENT;ALTER TABLE `_users`
        MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT;"
;
        try {
            
$stmt $dbh->prepare($sql1);
            
$stmt->execute();
            
$stmt $dbh->prepare($sql2);
            
$stmt->execute();
            
$stmt $dbh->prepare($sql3);
            
$stmt->execute();
            
$pass_hash password_hash($passPASSWORD_DEFAULT);
            
$lock fopen('lock'"a");
            
$txt 'lock';
            
fwrite($lock$txt);
            
fclose($lock);
        } catch (
PDOException $error) {
            exit(
$error->getMessage());
        }
    }
}

?>
Nie rozumiem w czym problem w takim razie? Jeżeli jakiś jeszcze warunek miał się wykonać a nie wykonuje się to debuguj poszczególne warunki. Najlepszym narzędziem jest xdebug ale możesz też sobie var_dump robić poszczególnych warunków.

Poza problemem to te sprawdzanie locka jest słabe bo skrypt dalej się wykonuje i tak. Poza tym możesz zrobić przekierowanie w php header() zamiast printować js.
Przekierowanie w header() nie zawsze mi działało, chociaż postaram się to jeszcze poprawić
Wszystko poniżej tego
if (file_exists(INC.'dbcfg.php')) {
        require_once 
PAGES'database.php';
    } else {
        
$file fopen(INC."dbcfg.php""a");
        
fwrite($file$content1);
        
fwrite($file"\n");
        
fwrite($file$content2);
        
fwrite($file"\n");
        
fwrite($file$content3);
        
fwrite($file"\n");
        
fwrite($file$content4);
        
fwrite($file"\n");
        
fwrite($file$content5);
        
fwrite($file"\n");
        
fclose($file);
        require_once 
PAGES'database.php';
    } 
Nie działa, jest to ostatni warunek, który się wykonuje
Jakiś exit w database.php? Lub po uzupełnianiu pliku ma on nieodpowiednią zawartość.
Cały database.php
<?php

require_once INC"dbcfg.php";
try {
    
$dns "mysql:host=" $db_host ";dbname=" $db_name";charset=utf8";
    
$options = [
        
PDO::ATTR_EMULATE_PREPARES => false,
        
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    
];
    
$dbh = new PDO($dns$db_user$db_pass$options);
} catch (
PDOException $error) {
    exit(
'Database error'$error->getMessage());

A czy masz w ogóle włączone raportowanie/wyświetlanie wszystkich błędów? Nic nie pokazuje? A jak wygląda dbcfg.php po tym jak zapisujesz go skryptem?
Cały plik install.php I nic nie wyświetla
   
<?php
ini_set
('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);
require_once 
'../engine.php';
require_once 
INC'incCSS.html';
require_once 
INC'incJS.html';
if (
file_exists(INSTALL'lock')) {
 
   echo '<script>window.location.href = "/index.php"</script>';
}

if (isset(
$_POST['dbhost']) && isset($_POST['dbuser']) && isset($_POST['dbpass']) && isset($_POST['dbname']) && isset($_POST['user']) && isset($_POST['email']) && isset($_POST['pass']) && isset($_POST['pass1'])) {
 
   $dbhost filter_input(INPUT_POST'dbhost'FILTER_SANITIZE_STRING);
 
   $dbuser filter_input(INPUT_POST'dbuser'FILTER_SANITIZE_STRING);
 
   $dbpass filter_input(INPUT_POST'dbpass'FILTER_SANITIZE_STRING);
 
   $dbname filter_input(INPUT_POST'dbname'FILTER_SANITIZE_STRING);
 
   $content1 '<?php';
 
   $content2 '$db_host = "'.$dbhost.'";';
 
   $content3 '$db_user = "'.$dbuser.'";';
 
   $content4 '$db_pass = "'.$dbpass.'";';
 
   $content5 '$db_name = "'.$dbname.'";';

 
   $user filter_input(INPUT_POST'user'FILTER_SANITIZE_STRING);
 
   $email filter_input(INPUT_POST'email'FILTER_VALIDATE_EMAIL);
 
   $email filter_var($emailFILTER_SANITIZE_EMAIL);
 
   $pass filter_input(INPUT_POST'pass'FILTER_SANITIZE_STRING);
 
   $pass1 filter_input(INPUT_POST'pass1'FILTER_SANITIZE_STRING);

 
   if (file_exists(INC.'dbcfg.php')) {
 
       require_once PAGES'database.php';
 
   } else {
 
       $file fopen(INC."dbcfg.php""a");
 
       fwrite($file$content1);
 
       fwrite($file"\n");
 
       fwrite($file$content2);
 
       fwrite($file"\n");
 
       fwrite($file$content3);
 
       fwrite($file"\n");
 
       fwrite($file$content4);
 
       fwrite($file"\n");
 
       fwrite($file$content5);
 
       fwrite($file"\n");
 
       fclose($file);
 
       require_once PAGES'database.php';
 
   }
 
   
    $reg_ok 
true;
 
   //Sprawdzenie długości nicka
 
   if ((strlen($user)<3) || (strlen($user)>20)) {
 
       $reg_ok false;
 
       $_SESSION['register_error'] = "Twój login musi posiadać od 3 do 20 znaków !";
 
       echo '<script>window.location.href = "/install/install.php"</script>';
 
   }
 
   //Sprawdź poprawność nick'a
 
   if (ctype_alnum($user) == false) {
 
       $reg_ok false;
 
       $_SESSION['register_error'] = "Twój login musi się składać tylko z liter i cyfr bez polskich znaków !";
 
       echo '<script>window.location.href = "/install/install.php"</script>';
 
   }
 
   // Sprawdź poprawność adresu email
 
   if (empty($email)) {
 
       $reg_ok false;
 
       $_SESSION['register_error'] = "Twój email nie jest poprawny !";
 
       echo '<script>window.location.href = "/install/install.php"</script>';
 
   }
 
   //Sprawdzenie długości hasła
 
   if ((strlen($pass)<8) || (strlen($pass)>20)) {
 
       $reg_ok false;
 
       $_SESSION['register_error']="Hasło musi posiadać od 8 do 20 znaków!";
 
       echo '<script>window.location.href = "/install/install.php"</script>';
 
   }
 
   //Sprawdź poprawność hasła
 
   if ($pass != $pass1) {
 
       $reg_ok false;
 
       $_SESSION['register_error'] = "Podane hasła różnią się !";
 
       echo '<script>window.location.href = "/install/install.php"</script>';
 
   }
 
   
    if 
($reg_ok == true) {
 
       $sql1 "CREATE TABLE `_news` (
            `nid` int(11) NOT NULL,
            `title` text NOT NULL,
            `body` varchar(128) NOT NULL,
            `author` text NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
;
 
       $sql2 "CREATE TABLE `_users` (
            `uid` int(11) NOT NULL,
            `created` date NOT NULL,
            `name` text NOT NULL,
            `password` varchar(128) NOT NULL,
            `email` varchar(32) NOT NULL,
            `gid` int(11) NOT NULL,
            `avatar` varchar(32) NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
;
 
       $sql3 "ALTER TABLE `_news`
        ADD PRIMARY KEY (`nid`);ALTER TABLE `_users`
        ADD PRIMARY KEY (`uid`);ALTER TABLE `_news`
        MODIFY `nid` int(11) NOT NULL AUTO_INCREMENT;ALTER TABLE `_users`
        MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT;"
;
 
       try {
 
           $stmt $dbh->prepare($sql1);
 
           $stmt->execute();
 
           $stmt $dbh->prepare($sql2);
 
           $stmt->execute();
 
           $stmt $dbh->prepare($sql3);
 
           $stmt->execute();
 
           $pass_hash password_hash($passPASSWORD_DEFAULT);
 
           $lock fopen('lock'"a");
 
           $txt 'lock';
 
           fwrite($lock$txt);
 
           fclose($lock);
 
       } catch (PDOException $error) {
 
           exit($error->getMessage());
 
       }
 
   }
}

?>

<html>
    <head>
        <meta charset="utf8">
    </head>
    <body style="width: 100%">
        <section class="container-fluid">
            <?php
            if 
(isset($_SESSION['register_error'])) {
 
               echo "<h1>BŁAD: ".$_SESSION['register_error'];
 
               unset($_SESSION['register_error']);
 
               echo "</h1>";
 
            ?>
        <form method="post" action="">
            <h1 class="text-center text-danger">Baza Danych</h1>
            <div class="form-group text-center margin-aa" style="width: 40% !important; margin: auto auto !important;">
                <label>Host</label>
                <input type="text" name="dbhost" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Użytkownik</label>
                <input type="text" name="dbuser" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Hasło</label>
                <input type="password" name="dbpass" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Nazwa bazy danych</label>
                <input type="name" name="dbname" class="form-control">
            </div>
            <h1 class="text-center text-danger">Administrator</h1>
            <div class="form-group text-center margin-aa" style="width: 40% !important; margin: auto auto !important;">
                <label>Login</label>
                <input type="text" name="user" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Email</label>
                <input type="text" name="email" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Hasło</label>
                <input type="password" name="pass" class="form-control">
            </div>
            <div class="form-group text-center" style="width: 40% !important; margin: auto auto !important;">
                <label>Powtórz Hasło</label>
                <input type="password" name="pass1" class="form-control">
            </div>
            <div style="width: 40% !important; margin: auto auto !important; margin-top: 30px !important;">
                <button type="submit" class="btn btn-primary text-center" style="width: 40% !important; margin: auto auto !important; display:block !important">Submit</button>
            </div>
        </form>
        </div>
        <div class="container-fluid text-center">
            <p>&copy; ZnaQu.inc 2019</p>
        </div>
    </body>
</html> 

plik dbcfg.php specjalnie zapisałem go teraz skryptem
<?php
$db_host 
"localhost";
$db_user "root";
$db_pass "";
$db_name "zqscript"
I nawet jak zrobisz sobie po tym ifie var_dump('asda'); exit; to tego nie wykonuje? Nie widzę tutaj błędu z samego czytania kodu poza niepoprawnymi danymi z formularza które przesyłasz a później validujesz. Podeślij ewentualnie cały projekt abym mógł go sobie odpalić.
Nie wykonuje, po ifie, jak i w ifie po require_once PAGES. 'database.php';
if (file_exists(INC.'dbcfg.php')) {
        require_once 
PAGES'database.php';
    } else {
        
$file fopen(INC."dbcfg.php""a");
        
fwrite($file$content1);
        
fwrite($file"\n");
        
fwrite($file$content2);
        
fwrite($file"\n");
        
fwrite($file$content3);
        
fwrite($file"\n");
        
fwrite($file$content4);
        
fwrite($file"\n");
        
fwrite($file$content5);
        
fwrite($file"\n");
        
fclose($file);
        require_once 
PAGES'database.php';
        
var_dump('aaaaasdasd');
        exit();
    } 
https://github.com/ZnaQu/zqscript
Cały projekt
Poza tym że zakomentowałem przekierowanie w engine bo w pętlę wpadło to u mnie działa. Wywala sie dalej na
QLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE `_users` ADD PRIMARY KEY (`uid`);ALTER TABLE `_news` ' at line 2
Z ciekawości, jak sprawdziłeś błąd? Poprawiłem błąd rodzielając kolejne zapytania, ale dalej coś nie działa.
Wgrałem php xdebug do vscode ale mi coś nie działa

Albo bym potrzebował jakiegoś gotowego skryptu na którym bym mógł się wzorować, próbowałem na tym od MyBB, ale to jednak nie to czego szukałem

Na githubie poprawiona wersja, przeniosłem przekierowanie do index i nie wpada już w pętle



Użytkownicy przeglądający ten wątek:

1 gości