我剛剛計劃創建一個程序,我需要使用DBHost,DBUser,DBPassword和DBName連接USER的數據庫,以將未來需要的值保存在MySQL服務器上。 對於這一點,我已經寫了這樣的代碼:
<?php session_start(); ?>
<?php
require('functions.php');
iq_add_style('DBConForm_Styles' , 'assets/scripts/css/iq_styles' , 'stylesheet' , 'css');
function IQ_DB_Form(){
?>
<form id="iq-dbconform" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<label>Server Host: <input type="text" maxlength="15" placeholder="localhost" name="host-name" /></label>
<label>Database User: <input type="text" maxlength="15" placeholder="root" name="user-name" /></label>
<label>Password: <input type="password" maxlength="15" placeholder="User Password" name="user-pass" /></label>
<label>Name of the Database: <input type="text" maxlength="20" placeholder="developing_house" name="db-name" /></label>
<input type="submit" value="Connect" name="dbconbtn" /> <input type="reset" value="Reset" name="dbresetbtn" />
</form>
<?php
}
IQ_DB_Form();
// Database connection
$dbhost = $_POST['host-name'];
$dbuser = $_POST['user-name'];
$dbpass = $_POST['user-pass'];
$dbname = $_POST['db-name'];
$db_details = array(
'host' => $dbhost,
'user' => $dbuser,
'password' => $dbpass,
'dbname' => $dbname,
);
$IQcon = mysqli_connect($db_details['host'],$db_details['user'],$db_details['password'],$db_details['dbname']);
// Check connection
if (mysqli_connect_errno())
{
echo "Looks like an Error connecting IQubex Database: " . mysqli_connect_error();
}
// Store Values in Session
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
echo $_SESSION['db-name'];
echo $_SESSION['user-pass'];
echo $_SESSION['user-name'];
// Perform queries
if ($IQcon){
mysqli_query($IQcon,"CREATE TABLE `Persons` (
`FirstName` varchar(30) NOT NULL,
`LastName` varchar(60) NOT NULL,
`Age` int(255) NOT NULL,
);");
mysqli_query($IQcon,"CREATE TABLE `MadBoys` (
`FirstName` varchar(30) NOT NULL,
`LastName` varchar(60) NOT NULL,
`Age` int(255) NOT NULL,
);");
mysqli_close($IQcon);
}
else{
echo "There seems a problem with your server.";
}
?>
,這樣,當我寫echo $_SESSION['db-name']
,印在插入數據庫的名字我已經在會話中保存數據庫配置表單值。但是當頁面重新加載,在頁面上再次返回時,或者類似的東西,所有的值都會被重置!因此,我不希望我的用戶一次又一次地連接數據庫。我希望它永久保存,以便我可以爲下一個程序步驟工作。
還需要幫助嗎? –
@MasivuyeCokile是的,我需要!但是這裏有很多我討厭的手續。 –
你的意思是你討厭的手續是什麼? –