php将php代码添加到php文件中

时间:2016-10-06 17:05:38

标签: php

PHP:

$errors = array();

$db = "";



if (isset($_POST['saveuserForm'])) {
    $host = $_POST['host'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $databasename = $_POST['databasename'];


    if($host == "") {
        $errors[] = "Host can not be blank";
    }
    if($username == "") {
        $errors[] = "Username can not be blank";
    }
    if($password == "") {
        $errors[] = "Password can not be blank";
    }
    if($databasename == "") {
        $errors[] = "Database Name can not be blank";
    }


    if ($host !== "" && $username !== "" && $password !== "" && $databasename !== "") {
            $db = mysqli_connect($host, $username, $password, $databasename);
            $connectionstring = "
            <?php
                \$dbname = \"d" . $databasename . "\";
                \$dbuser = \"w" . $username . "\";
                \$dbpass = \"f" . $password . "\";
                \$dbhost = \"s" . $host . "\";
                \$db = mysqli_connect(\$dbhost, \$dbuser, \$dbpass, \$dbname );


                if (mysqli_connect_errno()) {
                        echo \"Failed to connect to MySQL: \" . mysqli_connect_error();
                }
            ";

        echo file_put_contents("../includes/dbname.php", $connectionstring);

    }
    }

HTML:

<ul>
<?php
foreach ($errors as $value) {
    echo "<li>$value</li>";
}
?>
</ul>


<form action="install.php" method="post">
    <h3>Database Info:</h3>

    <div>
    <label class="desc" id="title3" for="host">
      Host:
    </label>
    <div>
      <input id="host" name="host" type="text" spellcheck="false" value="" maxlength="255" tabindex="3"> 
   </div>
  </div>

  <div>
    <label class="desc" id="title3" for="Username">
      Username:
    </label>
    <div>
      <input id="Username" name="Username" type="text" spellcheck="false" value="" maxlength="255" tabindex="3"> 
   </div>
  </div>


<div>
    <label class="desc" id="title3" for="password">
      Password
    </label>
    <div>
      <input id="password" name="password" type="Password" spellcheck="false" value="" maxlength="255" tabindex="3"> 
   </div>
  </div>

  <div>
    <label class="desc" id="title3" for="Database Name">
      Database Name:
    </label>
    <div>
      <input id="Database Name" name="Database Name" type="text" spellcheck="false" value="" maxlength="255" tabindex="3"> 
   </div>
  </div>


  <div>
        <div>
        <input id="saveForm" name="submitdb" type="submit" value="Submit">
    </div>
    </div>

</form>

结果:

<?php
    $dbname = "d";
    $dbuser = "w";
    $dbpass = "f";
    $dbhost = "s";
    $db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname );


    if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

您好,我需要一些帮助才能将php代码添加到php文件中,结果是没有显示来自表单的字符串,我需要它在结果中的正确位置显示。

如果你在connectionstring变量中看到我有可以在那里显示的变量,但它们不会在我的结果中显示字符串的内容。

我需要它来显示用户的输入谢谢

谢谢

0 个答案:

没有答案