如何在执行PHP脚本时修复服务器错误500?

如何在执行PHP脚本时修复服务器错误500?,php,mysql,database,pdo,Php,Mysql,Database,Pdo,我正试图通过PHP插入数据库。然而,当我连接到PHP文件时,我得到Server500错误。有人能发现我做错了什么吗 <?php include 'db-security.php'; function db_login() { $userName = filter_input(INPUT_POST, "userName"); $password = filter_input(INPUT_POST, "password");

我正试图通过PHP插入数据库。然而,当我连接到PHP文件时,我得到Server500错误。有人能发现我做错了什么吗

<?php

    include 'db-security.php';
    function db_login() 
    {
        $userName = filter_input(INPUT_POST, "userName");  
        $password = filter_input(INPUT_POST, "password");

       //binding the variable to sql.
       $statement = $link->prepare("INSERT INTO user(username, password)        
       VALUES($userName, $password)");

       //execute the sql statement.
       $statement->execute();
}
db_login();
?>

更新:

我发现在将filer\u输入或$\u post添加到php时会发生错误

<?php

include 'db-security.php';
function db_login() {
        global $conn;
        // use my eaxmple to filter input to get the data out of the form, because security.
        //$userName = filter_input(INPUT_POST, "userName");  
        $userName = $_POST['userName'];
        $password = $_POST['password'];
        //$password = filter_input(INPUT_POST, "password"); 

    //binding the variable to sql.

    $stmt = $conn->prepare("INSERT INTO user(username, password)VALUES(:usrname, :pswd)");
    $stmt->bindParam(':pswd', $password);
    $stmt->bindParam(':usrname', $userName);
    $stmt->execute();
    //execute the sql statement.
}
db_login();
?>

db-security.php

<?php

include_once 'conf.php';
function db_connect() {
 // Define connection as a static variable, to avoid connecting more than once 
    static $conn;

    // Try and connect to the database, if a connection has not been established yet
    if(!isset($conn)) {
         // Load configuration as an array. Use the actual location of your configuration file  
try 
      {
          $conn = new PDO("mysql:host=localhost;port=3307;dbname=database", DB_USERNAME,DB_PASSWORD);
              // stores the outcome of the connection into a class variable
          $db_msg = 'Connected to database';
      }
      catch(PDOException $e)
      {
          $conn = -1;
          $db_msg = $e->getMessage();
      }

//$conn = new PDO(DB_HOST,DB_USERNAME,DB_PASSWORD , MAIN_DB);



    }
}
db_connect();
?>
<?php
include_once 'conf.php';

try
{
 $conn = new pdo("mysql:host=localhost; dbname=test; charset=utf8", DB_USERNAME, DB_PASSWORD);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e)
{
 die('Unable to connect to database!');
}
?>

所以您需要在prepare语句之后绑定参数

$stmt = $link->prepare("INSERT INTO user(username, password)VALUES(:usrname, :pswd)");
$stmt->bindParam(':pswd', $password);
$stmt->bindParam(':usrname', $userName);
$stmt->execute();

$link在哪里定义?在“db security.php”中?如果是,则存在可变范围问题。只需在函数调用中传递$link。所有功能都必须这样做

define function as = function db_login($link)
call function like = db_login($link);
编辑:

不要将函数用于“db security.php”,它应该是这样的:

<?php
$conn = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
?>
db-security.php

<?php

include_once 'conf.php';
function db_connect() {
 // Define connection as a static variable, to avoid connecting more than once 
    static $conn;

    // Try and connect to the database, if a connection has not been established yet
    if(!isset($conn)) {
         // Load configuration as an array. Use the actual location of your configuration file  
try 
      {
          $conn = new PDO("mysql:host=localhost;port=3307;dbname=database", DB_USERNAME,DB_PASSWORD);
              // stores the outcome of the connection into a class variable
          $db_msg = 'Connected to database';
      }
      catch(PDOException $e)
      {
          $conn = -1;
          $db_msg = $e->getMessage();
      }

//$conn = new PDO(DB_HOST,DB_USERNAME,DB_PASSWORD , MAIN_DB);



    }
}
db_connect();
?>
<?php
include_once 'conf.php';

try
{
 $conn = new pdo("mysql:host=localhost; dbname=test; charset=utf8", DB_USERNAME, DB_PASSWORD);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e)
{
 die('Unable to connect to database!');
}
?>

主脚本

<?php
include 'db-security.php';

function db_login()
{
 global $conn;
 $userName = $_POST['userName'];
 $password = $_POST['password'];
 $stmt = $conn->prepare("INSERT INTO user(username, password) VALUES(:usrname, :pswd)");
 $stmt->bindParam(':usrname', $userName);
 $stmt->bindParam(':pswd', $password);
 $stmt->execute();
}
db_login();
?>

我一直在查看您的代码,我建议您尝试不同的方法。在学习PHP的时候,我对这个问题已经绞尽脑汁了一段时间。我得到的最好的建议是,当每次都使用try/catch语句从数据库获取信息时,您可以进行最好的尝试。听起来很烦人或有问题,但很容易忽略和编写良好的维护代码,因为您知道每个try-catch块都将执行或至少捕获错误

PDO是最好的解决方案之一,因为它可以连接多个数据库。执行从数据库获取信息的最佳方法是:*

我要给你举一个我写的东西的例子。我不想在你的情况下把它全部写出来,因为我觉得这是你最好做的事情,以了解哪里出了问题,我希望这能让你朝着正确的方向迈出一步

database.php

$serverName = "";
$dbName = "";
$userName = "";
$password = "";

try {

        $db = new PDO("mysql:host=$serverName;dbname=$dbName", $userName, $password);
        // Set the PDO error mode to exception
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->exec("SET NAMES 'utf8'");

    }
    catch(PDOException $e){

        echo"Connection failed: " . $e->getMessage();
        exit;
    }

?>
index.php执行简单命令并从雇主处获得名字

<?php 
require_once 'database.php';

try 
{ 
    $sQuery = " 
        SELECT 
            firstName 
        FROM 
            employees 
    "; 

    $oStmt = $db->prepare($sQuery); 
    $oStmt->execute(); 

    while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC)) 
    { 
        echo $aRow['firstName'].'<br />'; 
    } 
} 
catch(PDOException $e) 
{ 
    $sMsg = '<p> 
            Regelnummer: '.$e->getLine().'<br /> 
            Bestand: '.$e->getFile().'<br /> 
            Foutmelding: '.$e->getMessage().' 
        </p>'; 

    trigger_error($sMsg); 
} 



?>


祝你好运,我希望我的index.php有助于向你展示我如何找到与数据库即时对话的最佳方式。

@u\u mulder好的,解决方案是什么?在开始时声明变量?感谢您的回复。我已经将其添加到代码中,但仍然得到错误500。你认为这与filter_输入有关吗?对不起,我是个新手,但是我应该把define放在哪里,在代码中调用它呢?谢谢基于你的新代码,你不再需要我的示例代码了。我要说的一件事是,代码的编写方式是函数调用
db_login()将在页面加载时运行。这意味着用户名和密码将为空。您需要在isset测试中包装该行代码,以检查是否单击了submit按钮。你也可以发布HTML代码吗?我正在创建一个用C#编写的移动应用程序。一个数组被发送到PHP。一旦数组中有数据,页面就会加载。
byte[]InsertUser=client.UploadValues(“http://test.co.uk/db-login.php“,”发布“,”用户信息)我对C#一无所知,但假设传递到PHP页面的数组包含名称/值对,我不明白为什么它不起作用。PHP区分大小写,因此您确定
$\u POST['userName']
正确吗?也许应该是
$\u POST['username']
?你在这里做了很多无用的工作。事实上,您不需要catch块中的代码——PHP已经在这样做了。如果您只是从代码中删除try和catch,结果会完全一样:)感谢您的澄清,我正在学习php,这是在1教程中他们是如何教我的。我在你的个人资料中看到了一个PDO链接,我将尝试看看这是否是一个更好地解释PDO的版本。再次感谢你!很高兴你注意到了。请随时提问,我渴望得到反馈。我会详细解释你的问题。@YourCommonSene嗨,我已经浏览了你的网站,我正在寻找什么!作为PHP和PDO的初学者。但是,我仍然可以使用一个简单的insert语句。我正在用C#开发一个应用程序,并尝试使用PHP作为两者之间的桥梁。谢谢,山姆。