我想要做的是根据 html 表单的值连接到特定的数据库,首先可能吗?
我有一些代码要提出:
这是我在上一页的表格:
<form method="post" action="connexion.php">
<li> <strong><b>Aircraft</b></strong> : <input type="text" name="Aircraft" placeholder="MRJ" /><br /> </li>
<li> <strong><b>Systeme</b></strong> : <input type="text" name="Systeme" placeholder="ATAXX" /><br /> </li>
<li> <strong><b>Projet</b></strong> : <input type="text" name="Projet" placeholder="CMA" /><br /> </li>
<br />
<input type="submit" value="Submit">
我的 connexion.php 代码:
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '@Rugby3390');
define('DB_NAME', 'ata."$Systeme"');
/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
header("location: ../FailureCondition/failurecondition$_POST[Systeme].php");
?>
所以如果你能理解我,根据 Systeme 中输入的数据,我想连接到一个名为 ata(user input) 的特殊数据库并重定向到一个页面,其中包含名为 failurecondition(user).php 的用户输入的信息
我的代码有什么问题?
谢谢
虽然 Nirav 和 Arkits 的解决方案应该可以解决问题,但在我看来,从变量定义常量是根本错误的。我建议这是更正确的:
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD,$_POST['Systeme']);
此外,就像这里的情况一样,您的应用程序有多个数据库,在查询中使用隐式数据库可能会在以后造成痛苦。如果数据库的分离仅仅是组织问题,那么更好的方法是在查询中显式引用数据库:
$activeDB=$_POST['Systeme'];
$sql="SELECT * FROM ${activeDB}.atable";
OTOH 如果它旨在作为提供可扩展性的一种手段,那么您的代码不会解决这个问题 - 您应该使用类似的东西:
$dbs=array(
'db1'=>array(
'user'=>'mysqluser', 'host'=>'localhost', 'password'=>'s3cr3t'
),
'db2'=>array(
'user'=>'mysqluser', 'host'=>'localhost', 'password'=>'s3cr3t'
),
'db3'=>array(
'user'=>'other', 'host'=>'192.168.22.4', 'password'=>'swordfish'
)
...
);
$use=$dbs[$_POST['Systeme']];
if (!is_array($use)) {
...
}
$mysqli = new mysqli($use['host'], $use['user'], $use['password'], $_POST['Systeme']);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句