PHP数据库连接失败

标签 php mysql

我有一个 php 文件,其中包含两个函数,一个用于连接到数据库,一个用于为购物车设置 cookie。这是该文件:

<?php
$dbServer="localhost";
$dbName="test";
function ConnectToDb($server, $database){
    $s=@mysql_connect($server);
    $d=@mysql_select_db($database, $s);
    if(!$s || !$d)
    return false;
    else
    return true;
}

function GetCartId(){
    if(isset($_COOKIE["cartId"])){
    return $_COOKIE["cartId"];
}
else {
    session_start();
    setcookie("cartId", session_id(), time()+((3600*24)*30));
    return session_id();
}
}
?>

连接数据库的函数在这个特定程序的另一个 php 文件中运行良好。我在这个文件中遇到了问题:

<?php
include("db.php");

    switch($_GET["action"]) {
            case "add_item":
            {
                    AddItem($_GET["id"], $_GET["qty"]);
                    ShowCart();
            break;
            }
            case "update_item": {
                    UpdateItem($_GET["id"], $_GET["qty"]);
                    ShowCart();
            break;
            }
            case "remove_item": {
                    RemoveItem($_GET["id"]);
                    ShowCart();
            break;
            }
            default: {
                    ShowCart();
            }
    }

    function AddItem($itemId, $qty) {
            // Will check whether or not this item
            // already exists in the cart table.
            // If it does, the UpdateItem function
            // will be called instead


            $cxn = @ConnectToDb($dbServer, $dbName);
            // Check if this item already exists in the users cart table
            $result = mysql_query("select count(*) from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
            $row = mysql_fetch_row($result);
            $numRows = $row[0];

            if($numRows == 0) {
                    // This item doesn't exist in the users cart,
                    // we will add it with an insert query
                    @mysql_query("insert into cs368_cart(cookieID, itemId, qty) values('" . GetCartID() . "', $itemId, $qty)");
            }
            else {
                    // This item already exists in the users cart,
                    // we will update it instead

                    UpdateItem($itemId, $qty);
                    }
            }

    function UpdateItem($itemId, $qty) {
            // Updates the quantity of an item in the users cart.
            // If the qutnaity is zero, then RemoveItem will be
            // called instead

            $cxn = @ConnectToDb($dbServer, $dbName);

            if($qty == 0) {
                    // Remove the item from the users cart
                    RemoveItem($itemId);
            }
            else {
                    mysql_query("update cs368_cart set qty = $qty where cookieID = '" . GetCartID() . "' and itemId = $itemId");
                    }
            }

    function RemoveItem($itemId) {
            // Uses an SQL delete statement to remove an item from
            // the users cart
            $cxn = @ConnectToDb($dbServer, $dbName);
            mysql_query("delete from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
    }

    function ShowCart() {
            // Gets each item from the cart table and display them in
            // a tabulated format, as well as a final total for the cart
            $cxn = @ConnectToDb($dbServer, $dbName);
            $result = mysql_query("select * from cs368_cart inner join cs368_products on cart.itemId =
                    items.itemId where cart.cookieID = '" . GetCartID() . "' order by items.itemName asc")
                     or die("Query to get test in function ShowCart failed with error: ".mysql_error());
?>

我可以采取什么措施来解决这个问题?谢谢!

最佳答案

首先:丢失@,并在其中放置一些适当的错误处理(这些函数在出现问题时返回 false,并且您可以使用 mysql_error 和 mysql_errno 来记录它)。

第二:在有人通过 URL 潜入一些额外代码之前,对 $_GET 参数执行 mysql_real_escape_string 和 intval。

第三:您将 $dbServer 和 $dbName 作为函数 UpdateItem 的本地变量访问,而不是脚本的全局变量。您应该只连接到数据库一次(在原始 db.php 文件中),并让查询函数处理其余的事情(因为只有一个连接,所以它们都默认为该连接)。

关于PHP数据库连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556694/

相关文章:

PHP - 是否可以对数字数组进行哈希处理?

mysql - 在以下查询中包含周数中的日期

php - 我有一个连接到 phpmyadmin 的 laravel 网站,但现在我想将 android 应用程序连接到同一个数据库

php - XAMPP apache 与独立 MySQL

php - 外键约束 MySQL

php - CURLOPT_VERBOSE 不工作

javascript - 单击提交按钮后重新加载页面后如何滚动到特定的 div?

php - 使用php从mysql中删除重复项

php - 如何访问mysql_fetch_array返回值的元素?

mysql - Spring JPA 中的自动创建表失败