我試圖設置一個基本的網頁,允許用戶在數據庫中的一個mysql表中添加一行以打開一個LED燈,並使用arduino。但是,當我嘗試提交表單時,頁面返回500狀態,並且不執行mysql查詢。我正在計算機上運行一個linux-apache-mysql-php服務器。爲什麼會這樣?服務器獲取請求失敗
<!doctype html>
<html>
<head>
<title> ARDUINO CONTROL CENTER </title>
<meta charset = "utf-8"/>
<link rel = "stylesheet" type = "text/css" href = "#" />
</head>
<body>
<h1> Welcome to the online arduino controller </h1>
<p> From here you can actually control an arduino in my room that will turn an LED light on and off. </p>
<form method = "get" action = "index.php">
<select name = "action">
<option value = "ON">ON</option>
<option value = "OFF">OFF</option>
</select>
<input type = "number" name = "duration"/>
<input type = "submit" />
</form>
</body>
</html>
<?php
$host = 'localhost';
$username = 'petros';
$password = '**********'; //can't give my password
$dbc = mysql_connect($host,$username,$password) or die("Unable to connect to server");
$db = 'ledrequests';
$sdb = mysql_select_db($db,$dbc) or die("Unable to connect to database.");
if(isset($_GET['action']) && isset($_GET['duration'])){
$action = $_GET['action'];
$duration = $_GET['duration'];
$query = "INSERT INTO requests(`act`,`duration`) VALUES ('$action',$duration)";
mysql_query($query);
}
?>
這是一個很正常的情況,你會一次又一次面對。腳本執行中的一些錯誤。嘗試討論可能存在的問題並不是非常有效。而是看看你的http服務器錯誤日誌文件。在那裏,php會指出它在某些腳本的哪一行出現的問題。 http狀態500是一個「內部錯誤」,所以很可能是崩潰的腳本。您從該錯誤日誌文件獲得的信息將有助於立即識別問題。您無法監控該文件,無法編程php。 – arkascha
除此之外,一般提示:停止將html表單和處理邏輯放在同一個文件中。我知道,很多教程都是這樣展示的,但它簡直是瘋狂,錯誤,不必要的複雜。有一個文件(或一組腳本)可以創建和傳遞表單,使用另一個單獨的文件來處理表單提交。這使分開的事情分開。 – arkascha
如果您使用後來的php版本之一,這是因爲所有mysql_函數都被刪除,請使用mysqli_函數或pdo代替 – rypskar