Jump to content

[SOLVED] checking to see if a value exist in a column


kevincro

Recommended Posts

I'm using an if construct to see if a members name has been added to a list of applicants.  If they have been, my code is set to dispay an unapply button.  If they have not yet applied(added their names to the database) an apply button is displayed.  The code, in its present form, looks as follows:

<?
$query=mysql_query("SELECT Name FROM Applicants WHERE Number2='$data_key'");
$row=mysql_fetch_array($query);
if ($userdata['user_name']!= $row['Name'])
{
            print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n";
        }
        else
        {
                print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n";
}
?>

The expression for my if construct works great, but only if noone has applied, or the member is the first name on the list.  So my question is, is there an expression that will search through the column data and if the members name is already on the list, display the unapply button?

 

$data_key is given the value of the project number, $userdata['user_name'] is a global that is constantly set to the members user name. 

Try:

 

<?php
$query = mysql_query("SELECT COUNT(*) FROM `Applicants` WHERE `Name`='".$userdata['user_name']."'") or die(mysql_error();
$num = mysql_result($query,0);
if($num == 0){
print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n";
}else{
print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n";
}
?>

 

The idea is that you find out how many rows exist with that given name. If it is 0, then you know it's not in the database.

<?php
$query = mysql_query("SELECT `Name` FROM `Applicants` WHERE `Number2` = '".$data_key."' AND `Name` = '".$userdata['user_name']."'");
if (mysql_num_rows($query) == 0) {
print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n";
}
else {
print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.