我正在创建一个用户可以输入信息的表单,它将进入网站上的数据库:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
唯一的问题。它对我很有用,因为我可以手动输入我的数据库信息的领域
($servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";)
但是我打算向公众发布这个表单,我希望他们不必编辑每个.php文件(这将是大约五个php文件)
我怎样才能拥有它以便他们可以设置它#34;他们第一次在他们的网站上安装这些表格。
答案 0 :(得分:1)
您可以通过将所有数据库设置放在单独的文件中来解决此问题,例如: dbsettings.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
?>
在通常用于数据库连接的文件中,而不是仅使用以下命令连接数据库:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
首先include数据库设置文件。
<?php
include 'dbsettings.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
这是你在PHP中的表现。您将看到Wordpress中使用的类似设置,以及所有其他PHP框架,如Laravel,FuelPHP,CodeIgniter等(对于更大的项目,使用它们可能是一个好主意)。