PHP error mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toadstool
    New Member
    • Jun 2009
    • 16

    PHP error mysql

    Code:
    if (IN PHP) I call $cart->insert_Pcart($item1, $item2);
    
    
    
    class Cart 
    {
    :
    function insert_Pcart($item_name,$p_qty)
          {
    $insert = "INSERT INTO Cart (pname, qty) 
    VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
    [B]Line69=>     if (mysql_query($insert, $conn)) { $cartID = mysql_insert_id($conn);[/B]
    
    } else          { die ("Error inserting CART FORM data: " .mysql_error()); };
           
    //$p_name=($_REQUEST["p_name"]);
    }
    I get the error
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\p roject_shopping _cart.php on line 69
    Error inserting CART FORM data:

    Can anyone explain what this error means or how I might fix it?
    Last edited by Markus; Jun 15 '09, 08:15 PM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by toadstool
    Code:
    if (IN PHP) I call $cart->insert_Pcart($item1, $item2);
    
    
    
    class Cart 
    {
    :
    function insert_Pcart($item_name,$p_qty)
          {
    $insert = "INSERT INTO Cart (pname, qty) 
    VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
    [B]Line69=>     if (mysql_query($insert, $conn)) { $cartID = mysql_insert_id($conn);[/B]
    
    } else          { die ("Error inserting CART FORM data: " .mysql_error()); };
           
    //$p_name=($_REQUEST["p_name"]);
    }
    I get the error
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\p roject_shopping _cart.php on line 69
    Error inserting CART FORM data:

    Can anyone explain what this error means or how I might fix it?
    It means you're trying to supply a MYSQL-Link resource that isn't valid, that is, your $conn variable isn't a valid resource. You don't seem to set it anywhere, so why pass it? You can remove this and try again. If no resource is provided, MySQL will take the current (or last opened, I forget) link.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by toadstool
      Can anyone explain what this error means or how I might fix it?
      this means that the 2nd parameter ($conn) is of the wrong type (it must be a resource type)

      did you define $conn in the function (otherwise it is NULL, which is not a resource type)? probably the best is omitting that parameter (except you have more than one connection open, in this case you application needs aredesign)

      darn…

      Comment

      • toadstool
        New Member
        • Jun 2009
        • 16

        #4
        Interestingly, this did not produce an error
        Code:
        function connect_cart()
              { 
        	    $conn = @mysql_connect($this->host,$this->user,$this->pass);  
               if (!$conn) { die("Connection failed: " .mysql_error());}
        	   if (mysql_select_db("test", $conn)) { 
                } else                                { die ("Could not locate test database " .mysql_error());}
        	  }

        Comment

        • toadstool
          New Member
          • Jun 2009
          • 16

          #5
          If I want to set a cookie from this wouldn't removing $conn affect this?
          e.g.
          $whatever = mysql_insert_id ($conn);
          setcookie("cook iename",$whatev er);

          Comment

          Working...