drhqkz3455 2016-04-22 12:12
浏览 42
已采纳

在多个PHP脚本中使用存储在常量中的字段数据

I'm building a database modifying gui that executes basic queries. The user have to input the name of existing database and table name at the start. I'm using fields to take user input and then parsing them to a php script. I was using session variables to use the database name and table name in different php files. Is there a way to do this using include or include_once? or any other method that serves better than session variables in this situation? Keeping in mind that the script will be using GET to grab the input in the fields.

  • 写回答

1条回答 默认 最新

  • douchixu3686 2016-04-22 12:20
    关注

    Yes you can. You can have your php code create a php file (with the information about the database and table) which you can then include into whichever file you wish to use those variables in. First create the form that will be used for the input, something similar to the following:

    index.php:

    <form method="POST" action="phpscript.php">
      <input type="text" name="databaseName">
      <input type="text" name="tableName">
      <input type="submit" name="Submit" value="Submit">
    </form>
    

    then create the php code in the page that the form takes you to that will create the new php file:

    phpscript.php:

    <?php
    $myFile = fopen("info.php", "w");
    $myStrig = "<?php $tableName = ".$_POST['tableName']."; $databaseName = ".$_POST['databaseName']."; ?>";
    fwrite($myFile, $myString);
    fclose($myFile);
    ?>
    

    Now whichever file that you would like to have access to variables $tableName and $databaseName would just need the following line of code:

    include('info.php');
    

    Also keep in mind that I am assuming that all the files are in the same directory. If you have them in different directories, you'll have to change the paths to those files.

    Let me know if that helps you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?