500 使用 php 和 mysql 的内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31259379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:20:05  来源:igfitidea点击:

500 Internal Server Error using php and mysql

phphtmlmysql

提问by Adam Hartman

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

因此,当我尝试打开 PHP 文件以写入 MySQL 数据库时,我的网站不断给我 500 Internal Server Error

HTML

HTML

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

PHP

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.

我还是 HTML 和 PHP 的新手,但即使经过数小时的搜索,我也无法弄清楚我做错了什么。我的网站不断给我同样的错误。如果有帮助的话,我使用 goDaddy 作为主持人。

回答by Jesper

<?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.

在检查错误和运行查询时,您错过了使用 $link 变量。每次使用 mysqli_* 运行某些东西时都必须使用 $link 变量/连接,因为它是连接。
mysqli_select_db 也弄错了,必须先定义连接,再定义数据库名。

回答by user8531003

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

还要检查您是否在 php.ini 中启用了 mysql 扩展

回答by Shubham Sinha

Most 500 errors are server-side errors.

大多数 500 错误是服务器端错误。

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.
  1. 权限错误:检查文件和文件夹的权限。在大多数情况下,对 PHP 和 CGI​​ 脚本的不正确权限是罪魁祸首。权限应设置为 755。
  2. PHP Timeout:如果这是导致 500 错误的原因,则超时规则或脚本中更好的错误处理应该会有所帮助。
  3. .htaccess 中的错误:检查 .htaccess 的结构是否正确。