1
shortman
SQL insert error
  • 2006/1/5 17:24

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


Hello everybody!

I need your assistance. I am developing a shopping cart module, and I am runnin into a perculiar problem.

Whenever a user tries to add a product to their cart, the insert statment fails, but does not give any indication of what the problem is.

The function that gets called to perform the insert is as follows:

function newprod($cartid, $prodid){
global $xoopsDB;
$sql = "insert into " . $xoopsDB->prefix('cart') . " (session, prodid) values ('" . $cartid . "', '" . $prodid . "'";
$result = $xoopsDB->query($sql) or die('SQL Error: ' . mysql_error());
}


I know the insert statement works, becuase I have something similar in the admin control panel to add categories and products.

Any help will be greatly appreciated!

TIA

2
m0nty
Re: SQL insert error
  • 2006/1/5 17:50

  • m0nty

  • XOOPS is my life!

  • Posts: 3337

  • Since: 2003/10/24


where's the closing bracket?

values ('" . $cartid . "', '" . $prodid . "')";

3
shortman
Re: SQL insert error
  • 2006/1/5 18:07

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


Opps - forgot to add the closing bracket when I posted this topic, but it is included in my php script.

4
m0nty
Re: SQL insert error
  • 2006/1/5 18:43

  • m0nty

  • XOOPS is my life!

  • Posts: 3337

  • Since: 2003/10/24


how are the strings populated? through $_POST? or whatever.

i'm clutching at straws tho..

you could try queryF instead of query which will force the query whatever, but have you checked whether the $prodid & $cartid actually contain data when they are passed to the function?

maybe an echo statement to give output till you're satisfied it works ok.

5
shortman
Re: SQL insert error
  • 2006/1/5 19:01

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


I added echo statements to make sure that the valuse in the variables are being passed, and also to check the SQL statement, and this is the result:

$prodid: 1
$cartid: 09d73396846d66242b793df10a7d9ea8
SQL insert statement: insert into xoops_xshop_cart (session) VALUES ('09d73396846d66242b793df10a7d9ea8', '1')
SQL Error:

Here is the modified function that produced the above result:

function newprod($cartid, $prodid){
global $xoopsDB;
print "\$prodid: " . $prodid . "<br>\$cartid: " . $cartid . "<br>";
$sql = "insert into " . $xoopsDB->prefix('xshop_cart') . " (sessionid) VALUES ('" . $cartid. "', '" . $prodid . "')";
echo "SQL insert statement: " . $sql . "<br>";
$result = $xoopsDB->query($sql) or die('SQL Error: ' . mysql_error());
main($cartid);
}

6
m0nty
Re: SQL insert error
  • 2006/1/5 19:13

  • m0nty

  • XOOPS is my life!

  • Posts: 3337

  • Since: 2003/10/24


ok but here >

SQL insert statement: insert into xoops_xshop_cart (session) VALUES ('09d73396846d66242b793df10a7d9ea8', '1')

we have 2 values to be entered, but only 1 field to contain values specified.. (session)

7
shortman
Re: SQL insert error
  • 2006/1/5 19:34

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


if only it were that simply...

I added in the second variable, but still no joy...


Script:

function newprod($cartid, $prodid){
global $xoopsDB;
print "\$prodid: " . $prodid . "<br>\$cartid: " . $cartid . "<br>";
$sql = "insert into " . $xoopsDB->prefix('xshop_cart') . " (session, prodid) VALUES ('" . $cartid. "', '" . $prodid . "')";
echo "SQL insert statement: " . $sql . "<br>";
$result = $xoopsDB->query($sql) or die('SQL Error: ' . mysql_error());
main($cartid);
}




Output:

$prodid: 1
$cartid: 09d73396846d66242b793df10a7d9ea8
SQL insert statement: insert into xoops_xshop_cart (session, prodid) VALUES ('09d73396846d66242b793df10a7d9ea8', '1')
SQL Error:

8
wtravel
Re: SQL insert error

Did you try using queryF instead of query? You can see if that works or not.

A second thing to check out would be the field types and (if any) the length of the fields in the table structure. Could you check both fields?

9
shortman
Re: SQL insert error
  • 2006/1/5 20:22

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


Quote:

wtravel wrote:
Did you try using queryF instead of query? You can see if that works or not.

A second thing to check out would be the field types and (if any) the length of the fields in the table structure. Could you check both fields?


session is tinytest
prodid is int(4)

I'll try queryF, though I thought I read in one of the FAQs, that it was more advisable to use query

10
shortman
Re: SQL insert error
  • 2006/1/5 20:29

  • shortman

  • Just popping in

  • Posts: 10

  • Since: 2003/10/30


queryF does work, so I guess I'll use that. Thanks for all you help. I'll post the module in about a month when I'm done.

Login

Who's Online

187 user(s) are online (84 user(s) are browsing Support Forums)


Members: 0


Guests: 187


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits