Connecting Tech Pros Worldwide Help | Site Map

PHP error mysql

Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#1: Jun 15 '09
Expand|Select|Wrap|Line Numbers
  1. if (IN PHP) I call $cart->insert_Pcart($item1, $item2);
  2.  
  3.  
  4.  
  5. class Cart 
  6. {
  7. :
  8. function insert_Pcart($item_name,$p_qty)
  9.       {
  10. $insert = "INSERT INTO Cart (pname, qty) 
  11. VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
  12. Line69=>     if (mysql_query($insert, $conn)) { $cartID = mysql_insert_id($conn);
  13.  
  14. } else          { die ("Error inserting CART FORM data: " .mysql_error()); };
  15.  
  16. //$p_name=($_REQUEST["p_name"]);
  17. }
I get the error
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\project_shopping_cart.php on line 69
Error inserting CART FORM data:

Can anyone explain what this error means or how I might fix it?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Jun 15 '09

re: PHP error mysql


Quote:

Originally Posted by toadstool View Post

Expand|Select|Wrap|Line Numbers
  1. if (IN PHP) I call $cart->insert_Pcart($item1, $item2);
  2.  
  3.  
  4.  
  5. class Cart 
  6. {
  7. :
  8. function insert_Pcart($item_name,$p_qty)
  9.       {
  10. $insert = "INSERT INTO Cart (pname, qty) 
  11. VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
  12. Line69=>     if (mysql_query($insert, $conn)) { $cartID = mysql_insert_id($conn);
  13.  
  14. } else          { die ("Error inserting CART FORM data: " .mysql_error()); };
  15.  
  16. //$p_name=($_REQUEST["p_name"]);
  17. }
I get the error
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\project_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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#3: Jun 15 '09

re: PHP error mysql


Quote:

Originally Posted by toadstool View Post

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…
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#4: Jun 15 '09

re: PHP error mysql


Interestingly, this did not produce an error
Expand|Select|Wrap|Line Numbers
  1. function connect_cart()
  2.       { 
  3.         $conn = @mysql_connect($this->host,$this->user,$this->pass);  
  4.        if (!$conn) { die("Connection failed: " .mysql_error());}
  5.        if (mysql_select_db("test", $conn)) { 
  6.         } else                                { die ("Could not locate test database " .mysql_error());}
  7.       }
  8.  
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#5: Jun 15 '09

re: PHP error mysql


If I want to set a cookie from this wouldn't removing $conn affect this?
e.g.
$whatever = mysql_insert_id($conn);
setcookie("cookiename",$whatever);
Reply