我經歷了一個艱難的過程,弄清楚如何在配置文件中綁定正確的地址,並將mysql連接到遠程服務器。昨天它使用不同的代碼,現在它不連接。我得到了死亡「連接失敗:」但它沒有向我顯示connect_error,因爲我甚至找不到這個問題?有人看到我的代碼有問題嗎?注意:我知道這是不安全的,我通常會準備好聲明,但現在只是嘗試與db連接問題一起工作。Mysql配置正確的遠程連接,但PHP代碼失敗連接?
<?php
$server = "174.---.--.187";
$username = "dylanto";
$pass = "------";
$db = "survey";
//$port = 3306;
//create connection
$conn = new mysqli($server, $username, $pass, $db);
//check connection
if (!$conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
$user = $_POST['user'];
$pass = $_POST ['pass'];
$sql = "insert into login (user, pass) values ('$user','$pass')";
if ($conn->query($sql)==TRUE) {
echo "Account created";}
else {echo "something went wrong";}
$con->close;
?>
HTML代碼:
<html><head><title>Log-in</title>
<link rel="stylesheet" type="text/css" href="sytle.css"></head>
<body>
<center><u><strong><h2>Login</h2></u></strong></center>
<br />
<center>
<form action="signup_process.php" method="POST">
Pick Username: <br>
<input type ="text" name = "user"><br>
Pick Password:<br>
<input type ="password" name ="pass"><br>
<input type="submit" name="submit" value="Sign-up">
</form></center>
</body>
</html>
'如果(!$ conn-> connect_error){'這個=不存在連接錯誤刪除'!' – cmorrissey
此外,您的代碼對於需要學習PDO或mysqli的SQL注入攻擊是開放的。 – cmorrissey
您已經開放了SQL注入。由於您使用的是mysqli,請利用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[bind_param](http://php.net/手動/ EN/mysqli的-stmt.bind-param.php)。 **這將照顧任何可能發生討厭的報價問題。** – aynber