Jump to content

PHP & MySQL Query help


iain_jw

Recommended Posts

Hi,

 

Relative newbie here pulling his hair out!

 

I've just started to attempt to build a stock control system for my parent's business and am having problems.

 

I'm building a (what I though) simple page that will just return all the info from a table.

 

Each stock item has it's own stock code (5 characters) and also a barcode.

 

There may be more than one barcode assigned to each stock item so I created a seperate table with just two fields - barcode and stockcode - the barcode obviously has to be unique but the stockcode doesn't.

 

The way I have done it is with a form where you can enter the stock code - this then becomes $STOCKCODE and the rest of the page displays the result for that code - so far so good.

 

Now I want it to see if the data entered into the form is greater than 5 characters. If it is it will then assume that you have entered a barcode and look for the stockcode from the other table and assign that to $STOCKCODE instead.

 

The code below is the part that isn't working. Entering a stock code brings up the correct result, but enter a barcode and it just returns an incorrect product (always the same incorrect product irrespective of what you type in). Executing the barcode lookup query directly in mysql on it's own works and generates the correct stock code but there's something awry in the way I've done it that I can't seem to work out. The "echo "not recognised" at the end doesn't work either.

Googling the problem had just confused me even more!

 

Any help would be appreciated!

 

Cheers

 

//retrieve form result
$getcode=$_POST['item_entry'];

//Count characters in form result
$num_char=strlen($getcode); 

//barcode lookup query
$result = mysql_query("SELECT barcode.SKU FROM barcode WHERE $getcode = 'barcode.BARCODE'");

//se if the entered data was a stockcode or a barcode
if ($num_char == 5) {$STOCKCODE=$getcode;}

elseif ($num_char > 5) {$STOCKCODE=mysql_fetch_assoc($result);}

else echo "not recognised";

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.