我试图通过这个html表单连接到mySQL数据库。当我点击连接时,它只是加载php文件而不执行代码。
<form action="SQLX.php" method="POST">
host: <input type="text" name="localhost" value="localhost:3306">
username: <input type="text" name="username">
password: <input type="password" name="password">
<input type="submit" name="check_submit" value="Connect">
</form>
-----------------------------
<?php
$username = {$_GETT["username"]};
$password = {$_GET["password"]};
$hostname = {$_GET["locahost"]};
$dbconnection = mysql_connect ("$hostname", "$username", "$password")
if (!dbconnection)
die("Unable to connect")
else {
echo "Connected to the database"
}
?>
答案 0 :(得分:1)
我要为你纠正一些事情,但最终这个应该适合你。
使用我的所有三个建议,这是您的代码应该是什么样的:
<form action="SQLX.php" method="POST">
host: <input type="text" name="localhost" value="localhost:3306">
username: <input type="text" name="username">
password: <input type="password" name="password">
<input type="submit" name="check_submit" value="Connect">
</form>
-----------------------------
<?php
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['localhost']))
{
$username = $_POST["username"];
$password = $_POST["password"];
$hostname = $_POST["locahost"];
$dbconnection = mysqli_connect ($hostname, $username, $password)
if (!dbconnection)
die("Unable to connect")
else {
echo "Connected to the database"
}
}
?>
如果您正在使用apt-get运行Ubuntu或其他任何东西,您可以通过执行以下操作轻松获取MySQLi:
sudo apt-get install php5-mysqlnd
sudo apt-get install php5-mysql
sudo apt-get install php5-cli
sudo service apache2 restart