Jump to content

[SOLVED] Query problem


whiskedaway

Recommended Posts

I'm having a problem with my second query. I've included the PHP around it as it might help to see what I'm trying to do.

$sql = "SELECT quantity, sku FROM ItemsOrdered
	        WHERE orderID = '$_POST[orderID]'";
	$result = mysqli_query($cxn, $sql)
	          or die ("Couldn't execute query 1.");
	$rowname = mysqli_fetch_assoc($result);
        extract ($rowname);

	$sql2 = "UPDATE Stock SET quantity = (quantity + '$quantity')
	        WHERE sku = '$sku'";
	$result2 = mysqli_query($cxn, $sql2)
                  or die ("Couldn't execute query 2.");

Can someone see what the problem is? I think it has to do with the quantity = (quantity + '$quantity') part.

Link to comment
Share on other sites

I keep getting the error "Couldn't execute query 2." which is why I think there's a problem with my query 2 syntax. I've tried these four changes to the query:

 

$sql2 = "UPDATE Stock SET quantity = (quantity + '$quantity')
              WHERE sku = '$sku'";
$sql2 = "UPDATE Stock SET quantity = quantity + '$quantity'
              WHERE sku = '$sku'";
$sql2 = "UPDATE Stock SET quantity = quantity + $quantity
              WHERE sku = '$sku'";
$sql2 = "UPDATE Stock SET quantity = (quantity + $quantity)
              WHERE sku = '$sku'";

 

It just won't work at all. The only other thing I can think of is that maybe the PHP before it is wrong, but I can't see an error in that either.

Link to comment
Share on other sites

please change this

 

$result2 = mysqli_query($cxn, $sql2)
                  or die ("Couldn't execute query 2.");

 

to

$result2 = mysqli_query($cxn, $sql2)
                  or die ("Couldn't execute query 2:".mysql_error());

 

for a better error description once you done that you should have the exact error and it will enable us to debug your code

Link to comment
Share on other sites

Oh gotcha. Here's the new error message:

 

Couldn't execute query 2. Unknown column 'quantity' in 'field list'.

 

And I think I just figured it out myself. The column is named quantity in ItemsOrdered, but it's quantityOnHand in Stock. Gah.

 

Thanks so much for your help, I can't believe I made such a stupid error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.