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

在多个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.

我正在构建一个修改gui的数据库来执行基本查询。 用户必须在开始时输入现有数据库和表名的名称。 我正在使用字段来获取用户输入,然后将它们解析为php脚本。 我使用会话变量在不同的php文件中使用数据库名称和表名。 有没有办法使用 include code>或 include_once code>? 或者在这种情况下比会话变量更好的任何其他方法? 请记住,脚本将使用GET来获取字段中的输入。 p> div>

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!