0

I'm trying to build a simple dropdown form and place the submissions in my database so I can check the results later.

Whenever, when I'm submitting the form with the submit button, nothing happens. The page just stays the same...

Chrome Console gives back an error 500 issue. What's the thing I'm doing wrong, cause I'm currently losing it. ;-)

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
    <title>V1</title>
</head>

<body>

<form method="post" action="">

    <label >I'm feeling...</label>

        <select name="subject_names">
            <option value="Angry">Angry</option>
            <option value="Anxious">Anxious</option>
            <option value="Bored">Bored</option>
            <option value="Cranky">Cranky</option>
            <option value="Crappy">Crappy</option>
            <option value="Depressed">Depressed</option>
            <option value="Energetic">Energetic</option>
            <option value="Exhausted">Exhausted</option>
            <option value="Frustrated">Frustrated</option>
            <option value="Happy">Happy</option>
            <option value="Motivated">Motivated</option>
            <option value="Okay">Okay</option>
            <option value="Relaxed">Relaxed</option>
            <option value="Sad">Stressed</option>
            <option value="Lazy">Lazy</option>
        </select>

    <button type="submit" name="submit" >Submit</button>

</form>

<?php
if(isset($_POST["submit"]))
{

 //Including dbconfig file.
include 'db-connect.php';

$subjectName=$_POST["feeling"];

mysql_query("INSERT INTO thefeels VALUES ('$feeling')"); 

echo " Added Successfully ";

}

 ?>

</body>
</html>

db-connect.php:

$hostname = "localhost";

$username = "USERNAME";

$password = "PASSWORD";

$dbname = "DBNAME";

 $conn = mysql_connect($hostname, $username, $password);

 if (!$conn)

 {

 die('Could not connect: ' . mysql_error());

 }

 mysql_select_db($dbname, $conn);


?>
MvanOeffel
  • 19
  • 1
  • 11
  • 4
    *Stop* using deprecated `mysql_*` API. Use `mysqli_*` or `PDO`. if you use php if you use an newer php version the API is already removedAlso 500 is an internal Server error. Look into the Server logfiles for more informations – Jens May 28 '18 at 14:08
  • Your first problem is using the `mysql_*` functions. Please [don't use `mysql_*`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1); the `mysql_*` functions are outdated, [deprecated](http://us3.php.net/manual/en/intro.mysql.php), and insecure - they have been removed entirely from modern versions of PHP (version 7.0 and higher). Use [`MySQLi`](http://us3.php.net/manual/en/book.mysqli.php) or [`PDO`](http://us3.php.net/manual/en/intro.pdo.php) instead. Beyond that, check your `error_log` for details about the error. – elixenide May 28 '18 at 14:09
  • In addition to the above - **that you should not use `mysql_` and that you should use a prepared statement** -. you have not defined `$feeling` anywhere, and the variable `$subjectName` is using the wrong index in the POST array. – Qirel May 28 '18 at 14:12
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel May 28 '18 at 14:12

1 Answers1

1

You are accessing a POST value that isn't set:

$subjectName=$_POST["feeling"];

You probably want:

$subjectName=$_POST["subject_names"];

You should stop using mysql_query though. It's deprecated and was removed in PHP7. Use mysqli or PDO instead.

Edit: Error 500 can be caused by bad file permissions, for example. Did you check the permissions?

Matthias Bö
  • 449
  • 3
  • 12