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!

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?