Check for an existing value = $input

Codes here !

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 4
Joined: Mon Aug 12, 2002 8:52 am

Check for an existing value = $input

Postby Nico » Fri Aug 30, 2002 1:38 pm

Ok i have 3 tables that i'm querying from.

All three tables have a common column, RTA. Initially i was going to use this as referential integrity, but since mysql doesn't handle referential integrity (least I can't figure out how to do it if it does) I'm attempting to use php to help me out. This is a small application and more for test purposes than anything else. Ok enough background, here is the question.

User enters information from a page into a text field named "rga".

I want to check that this number ($rga) = an RTA number in the main table BEFORE inserting the rest of the information from the page. If there is not an RTA number that equals $rga then I want it to die.

I have this in my page...

$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'")
or die("RTA doesn't exist");

It continues the process regardless if the RMA does or doesn't exist. It either returns a value or returns an empty set, and if i'm correct, an empty set is still "true" so it continues on with the rest of the script.

I then put this after the query but it still continues on-- i don't know if this is even remotely close to what it should be:

$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'")
or die("RTA doesn't exist");
if ($res = $rga) {
} else {print "rta doesn't exist";}

and again it just continues on thru the script and doesn't even blink.

What I want is for it to stop if the number in $rga is not in the maindcp table's RTA column. How could I accomplish this? thanx!



Postby Jay » Sat Aug 31, 2002 2:57 am

You're missing one line of code. The $res value you're getting is just a reference link to the actual results, not the result itself! Try this:

Code: Select all

$result = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga' limit 1");
if (@mysql_fetch_rows($result)) {
list($res) = mysql_fetch_row($result);
//$res is now equal to what you want
} else {
print "\$res doesn't exist";

Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 3 guests