LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with comparing user input to result of a query php (https://www.linuxquestions.org/questions/programming-9/problem-with-comparing-user-input-to-result-of-a-query-php-567901/)

orfiyus 07-09-2007 03:03 PM

Problem with comparing user input to result of a query php
 
Whats good

I am working on a php program that reads in user input from 2 drop down menus and a text field. What I am trying to accomplish is have the user input the data from drop downs and the text field which may or may not correspond to a record in the database. The program is supposed to check if there is a record in the database that corresponds to the details entered in by the user and then refreshes the page and says whether or not it exists. After that it outputs onto a new line the same exact menu allowing you to change the details of the records the user is searching through. So basically all the info the user queried before remains on the page and he is allowed to compare it to the new info and it keeps going like that. Anyway I got drop down menus and the respawning(new menu lines) parts to work.

My problem concerns the input for the text field. The user has to type in a number for the text field then that number is put into a variable. The variable is then put in as part of a query to the db. What I dont understand how to do is compare the input text to the result of the query. To reiterate I am trying to check if a number the user enters exists in the database.

I am looking for any suggestions on how to do this. Here is the part of the code I am working with. The whole program is too long to post. I am working with an oracle database and I only have access to it through the php.

Thanks for reading through all of this.

Code:

echo("<td>");
echo("<input type=text name=opt_strk onBlur='submit();' >");
$strike_number = $_REQUEST['opt_strk'];

$query1 = "select strike from surfaces where strike = $strike_number and undl_indx = $undl_indx and expire_date = ".$_SESSION['opt_exd_vals'][$opt_indx]." and call_put = '".$_SESSION['opt_cp_vals'][$opt_indx]."' ";
$parsed1 = ociparse($db_conn, $query1);
$succ1 = ociexecute($parsed1);

echo("</td>");


graemef 07-09-2007 05:20 PM

Quote:

Originally Posted by orfiyus
To reiterate I am trying to check if a number the user enters exists in the database.

I would suggest that you use the count to see if the number is on the database, such as:
Code:

SELECT count(*) FROM table WHERE number = number_entered
Also you may want to check that what has been entered in the text box is in fact a number before you progress with the database queries.

orfiyus 07-10-2007 09:05 AM

I dont understand if this statement returns the number of times the input appears or actually returns where the input appears.

For example if the user types in "42" and "42" appears twice does the statement return the number 2 signifying that it appears twice or does it return 2 rows where the "42" is located? If it returns the latter how would I compare it code wise? I am using severel functions to work with the database oci_parse and ociexecute. These also return 1 if true or 0 if false from what I understand about them. I think I should compare the user input to the actual row of the db where the number is located but I dont know how to do this.


Another way Im thinking of taking this is saying if the ociexecute returns true then the number exists. But I think this may be flawed if ociexecute returns true even if the number doesnt exist in the database. The PHP manual says this about ociexecute "Returns TRUE on success or FALSE on failure." Does that mean failure to connect to the db or failure to find anything in the db?

Any suggestions would be appreciated.


Heres a piece of code

Code:

//to create the text field.
echo("<input type=text name=opt_strk onBlur='submit();' >");
//assigns the the user input to a variable
$strike_number = $_REQUEST['opt_strk'];

//query the database with the appropriate details
$query1 = "select count(strike) from surfaces where strike = $strike_number and undl_indx = $undl_indx and expire_date = ".$_SESSION['opt_exd_vals'][$opt_indx]." and call_put = '".$_SESSION['opt_cp_vals'][$opt_indx]."' ";
$parsed1 = ociparse($db_conn, $query1);
//I think this returns 1 if it is true
$succ1 = ociexecute($parsed1);

//This is what I am trying to say.
if ($strike_number == <result of the query>)
    echo " variable strike_number exists in the db";
else
    echo " variable strike_number doesnt exist in the db";


graemef 07-10-2007 11:08 PM

count returns the number of occurrences, so you can see if it exists on the database and then proceed.


All times are GMT -5. The time now is 02:51 PM.