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

时间:2016-02-10 16:24:56

标签: php mysql database pdo

我试图通过PHP插入数据库。但是,当我连接到PHP文件时,我收到服务器500错误。有人能够发现我做错了吗?

<?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_input或$ _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();
?>

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

$ link定义在哪里?在&#39; db-security.php&#39;?如果是,则您有可变范围问题。只需在函数调用中传递$ link。这必须为所有功能完成。

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

修改

不要使用“db-security.php&#39;&#39; db-security.php&#39;它应该是这样的:

<?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);
?>

这不是完整的代码,只是一个示例。现在$ conn在全局变量范围内,并且在函数中使用global将起作用。或者只是将$ conn传递给函数,而不是全局使用。

<强> EDIT2:

以下是工作示例脚本。您需要更改一些信息以符合您的设置。我不确定为什么函数被称为db_login(),因为该函数实际上将用户/密码添加到用户&#39;表

conf.php

<?php
define('DB_USERNAME', 'test');
define('DB_PASSWORD', '123456');
?>

DB-security.php

<?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();
?>

答案 2 :(得分:0)

我一直在查看您的代码,我建议您尝试不同的方法。在学习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 执行简单的命令从雇主获取firstName

<?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能帮助您了解我如何找到与数据库交流的最佳方式。