1

So my website keeps giving me a 500 Internal Server Error when I attempt to open my PHP file to write to the MySQL database

HTML

<div id="body">
<form action="index.php" method="post" />
  <input type="text" name="usertext" />
  <input type="submit" value="Submit" />
 </form>
</div>

PHP

<?php

 define('DB_NAME', '****');
 define('DB_USER', '****');
 define('DB_PASSWORD', '****');
 define('DB_HOST', '****');

 $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

 if (!$link) {
    die('Could not connect: ' . mysqli_error());
 }

$db_selected = mysqli_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Cannot access' . DB_NAME . ': ' . mysqli_error());
}

$value = $_POST['****'];

$sql = "INSERT INTO **** (****) VALUES ('$value')";

if (!mysqli_query($sql)) {
    die('Error: ' . mysqli_error());
}

mysqli_close();
?>

I'm still new to HTML and PHP, but I can't figure out what I did wrong even after hours of searching. My website keeps giving me that same error. I use goDaddy as a host if that helps at all.

Adam Hartman
  • 47
  • 1
  • 1
  • 2

3 Answers3

0
<?php

define('DB_NAME', '****');
define('DB_USER', '****');
define('DB_PASSWORD', '****');
define('DB_HOST', '****');

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
}

$db_selected = mysqli_select_db($link, DB_NAME);

if (!$db_selected) {
    die('Cannot access' . DB_NAME . ': ' . mysqli_error($link));
}

$value = $_POST['****'];

$sql = "INSERT INTO **** (****) VALUES ('$value')";

if (!mysqli_query($link, $sql)) {
    die('Error: ' . mysqli_error($link));
}

mysqli_close($link);
?>

You've missed to user the $link variable when you were cheking for errors and running the query. The $link variable / connection have to be used everytime you're running something with mysqli_*, since it's the connection.
The mysqli_select_db were also made wrong, you have to define the connection first, then the name of the database.

Jesper
  • 3,816
  • 2
  • 16
  • 24
-1

Most 500 errors are server-side errors.

There can be following probable reasons :

  1. Permission Error : Check permission of files and folders. In most of those cases, an incorrect permission on a PHP and CGI script is to blame. The permission should be set at 755.
  2. PHP Timeout : Timeout rules, or better error handling in your script, should help if this is the cause of the 500 error.
  3. Error in .htaccess : Check if .htaccess is structured properly.
Shubham Sinha
  • 79
  • 2
  • 12
-1

also check if you have enabled the mysql extension in php.ini