Jump to content

PHP/MySQL - How do I check for contents of a Slelct query


nitefall

Recommended Posts

Hi,
I am building an asset tracking database and I have an issue.  When checking an asset in, I want to be able to query the asset table to see if the asset exists, before creating a row in the transaction table indicating the asset has been returned.

My issue is I don't know what functions to use, or the correct order to use the in.  As a result I didn't know what to type in when it was time to search for help.  So... Please forgive if this has already been answered.

Here is my code:
[code] if ($HTTP_POST_VARS['submit'] == "Check-In") {

// Create short variables from form data
$asset_tag=$HTTP_POST_VARS['asset_tag'];
$user_id=$HTTP_POST_VARS['user_id'];

// Add Slashes to escape control characters
$asset_tag = addslashes($asset_tag);
$user_id = addslashes($user_id);


// Check for asset in database
db_connect();
mysql_select_db('aws_assets');

$lookup = "SELECT * FROM assets WHERE asset_tag='$aset_tag'";
$lookup_result = mysql_query($lookup);

if (!lookup) {
echo('<p>Asset not in database!  Add Asset?</p>');
exit();

} else {

// Write asset to Database
$update = "UPDATE assets SET status_id='1' WHERE asset_tag='$asset_tag'";
$update_result = mysql_query($update);

if ($update_result) {
$insert = "INSERT INTO transactions SET
asset_id='$asset_tag',
user_id='$user_id',
status_id='1'";
$insert_result = mysql_query($insert);
if ($insert_result)
echo('<p>Your transaction has been recorded</p>');

}
}[/code]


So... As it is currently written, if a valid asset tag is used at check in, all goes according to plan, the assets table is updated, and the transactions table has a new row added to it.

If however a bogus asset tag is entered, the assets table is not updated (as it should not be) but a new row is added to the transactions table with the bogus asset tag.

I am sure the issue lies in these lines... but I don't know what to do.

[code] if (!lookup) {
echo('<p>Asset not in database!  Add Asset?</p>');
exit();

}
[/code]

Please Help.

Thank You
Link to comment
Share on other sites

So I did... So I did...  But this still does not seem to solve my issue.  I am still able to enter a bogus asset tag # when checking an asset in.

I need some method of looking at the assets table to confirm that the asset I am trying to check in is actually in the assets table.  If it is there, then the script should go on... If it is not however, there should be an error generated.

When the lookup is performed with a good asset tag, the query should return 1 row (asset tags are unique) and only one row.

How do I catch it if 0 rows are returned?  Wouldn't that be the result if the lookup was performed with a bogus asset_tag?

Link to comment
Share on other sites

And if I try this....

[code] $lookup = "SELECT * FROM assets WHERE asset_tag='$aset_tag'";
$lookup_result = mysql_query($lookup);
mysql_num_rows($lookup_result);


if (mysql_num_rows($lookup_result) <= 0) {
echo('<p>Asset not in database!  Add Asset?</p>');
exit();
}
[/code]

It fails all the time... even with good asset_tags...  so I am very confused.
Link to comment
Share on other sites

[code]$lookup = "SELECT * FROM assets WHERE asset_tag='$aset_tag'";[/code]

But your posted value is retrieved as $as[b]s[/b]et_tag, so the code should be:
[code]$lookup = "SELECT * FROM assets WHERE asset_tag='$asset_tag'";[/code]
Link to comment
Share on other sites

That is my answer!!!   

Thank you for catching that...  It works now!  Wooo Hoo...  The best part is learning that the way I thought it should be, is the way it is...  I just need to be better at spotting the little errors that I make.

Thank you very much.
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.