【问题标题】:Server get request fails服务器获取请求失败
【发布时间】:2016-12-11 14:56:13
【问题描述】:

我正在尝试设置一个基本网页,该网页允许用户在数据库的 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。
  • 除了一般提示:停止将 html 表单和处理逻辑放在同一个文件中。我知道,许多教程都以这种方式显示它,但这简直是疯狂、错误、不必要的复杂。拥有一个文件(或一组脚本)来创建和交付表单,使用另一个单独的文件来处理表单提交。这将分开的东西分开。
  • 如果你使用的是更高版本的 php 之一,那是因为所有 mysql_ 函数都被删除了,请改用 mysqli_ 函数或 pdo
  • @arkascha 你能告诉我那个日志文件在哪里吗?
  • 这是在您的 http 服务器配置文件中配置的。与往常一样,请查看官方文档以了解您的新知识:httpd.apache.org/docs/current/logs.html

标签: php html mysql


【解决方案1】:

它可能是从缺少 PHP-MySQL 扩展开始到以 MySQL 密码拼写错误和无效查询语法结束的任何地方。

让您的脚本在页面中查询输出或查看服务器日志(如果可用)。

您绝对应该在 apache 日志中找到我们可以帮助您的东西。

【讨论】:

    【解决方案2】:

    HTTP 状态代码 500 是“内部服务器错误”,因此,找出代码问题的适当位置是检查 Apache 错误日志。

    如果您使用 WAMP:Where can I find the WAMP error log?

    如果您使用 MAMP:https://sites.google.com/site/mamppro/en/mamp/faq/where-can-i-find-the-logs/where-can-i-find-the-apache-error-log

    如果你只是喜欢 linux:/var/log/apache2/error.log

    【讨论】:

      猜你喜欢
      • 2016-12-29
      • 1970-01-01
      • 2023-03-30
      • 2017-03-10
      • 2016-08-29
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多