2012-05-11 137 views
0

我有一个php文件,其中包含两个函数,一个连接数据库,一个设置为购物车烹饪。这是该文件:PHP数据库连接失败

<?php 
$dbServer="localhost"; 
$dbName="test"; 
function ConnectToDb($server, $database){ 
    [email protected]_connect($server); 
    [email protected]_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()); 
?> 

我能做些什么补救这个问题?谢谢!

+4

删除@符号,它会抑制错误,这样它会告诉你出了什么问题。 – Christophe

+2

避免使用jurassic'mysql_ *',使用PDO –

+0

你知道为什么在PHP中使用@符号吗?!它'压制错误信息!诊断您的问题的一个好的起点是将其删除! – GordyD

回答

2

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

第二:在有人通过URL偷偷摸摸一些额外的代码之前,在这些$ _GET参数上使用了mysql_real_escape_string和intval。

第三:您正在访问$ dbServer和$ dbName作为函数UpdateItem的局部变量,而不是全局脚本。你应该只连接数据库一次(在原始的db.php文件中),并让查询功能照顾其余的(因为只有一个连接,它们都默认为那个)。

+0

那么我将如何去连接一次以及查询文件如何访问db?使用'mysql_select_db()' –

+0

每个使用数据库的文件都调用require_once('db.php ')'而不是'include'。'db.php'应该在主脚本中以'mysql_connect'和'mysql_select_db'开头,而不是在另一个'connect'函数中。这样,脚本一开始就有一个数据库连接使用。 – Patrick

+0

谢谢,我很感激它。 –