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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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";
}
?>

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.