-3

I'm trying to setup a basic web page that allows the user to add a row in a mysql table in a database for the purpose of turning an LED light on and of with the use of an arduino. However when I try to submit the form, the page returns a 500 status and the mysql query isn't executed. I'm running a linux-apache-mysql-php server on my computer. Why would this be?

<!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);
        }

?>
  • 2
    This is a pretty normal situation you will face again and again. Some error in the script execution. It is not very effective to try to _guess_ what the issue _might_ be. Instead look into your http servers error log file. In there php will point out exactly into what problem it ran in which line in some script. An http status 500 is an "internal error", so most likely a script that crashed. The information you get from that error log file will help to to identify the issue immediately. You _cannot_ program php without monitoring that file. – arkascha Aug 05 '16 at 08:52
  • 1
    Apart from that a general hint: stop placing the html form and the processing logic in the same file. I know, many tutorials show it that way, but it is simply insane, wrong, unnecessary complex. Have one file (or set of scripts) that create and deliver the form, use another, separate file to process a form submission. That keeps separate things apart. – arkascha Aug 05 '16 at 08:53
  • 1
    If you use one of the later php versions it is because all mysql_ functions are removed, use mysqli_ functions or pdo instead – rypskar Aug 05 '16 at 08:57
  • @arkascha Could you tell me where that log file is located? – PetrosMarkopoulos Aug 05 '16 at 10:42
  • That is configured inside your http servers configuration files. As always, take a look at the official documentation to learn about things new for you: https://httpd.apache.org/docs/current/logs.html – arkascha Aug 05 '16 at 11:43

2 Answers2

0

HTTP status code 500 is an "Internal Server Error", thus, the appropriate place to figure out what's wrong with your code is by checking Apache error logs.

If you're using WAMP: Where can I find the WAMP error log?

If you're using MAMP: https://sites.google.com/site/mamppro/en/mamp/faq/where-can-i-find-the-logs/where-can-i-find-the-apache-error-log

If you're just on a flavor of linux: /var/log/apache2/error.log

Community
  • 1
  • 1
0

It could be anywhere starting from missing PHP-MySQL extension and ending with MySQL password misspelling and invalid query syntax.

Make your script do query output in the page or look into the logs of your server if they're available.

You should definitely find something we can help you with in apache logs.

Tim Connor
  • 97
  • 2