Jump to content

Constructing an If statement


kevincro

Recommended Posts

I've been experimenting with if statements, and have run into something I can't figure out.  What I want to do is construct an if statement that will display one of two submit buttons.  The idea is for members to be able to click the apply button and are automatically added to a select box.  When the page reloads the button should now read unapply.  I've made the page and can make it work using both buttons on the same page but I can't seem to figure out the right if statement.  What I have looks something like this:

 

<?
$sql=("SELECT * From Applicants WHERE Project='$data_key'");
$Applicant=$row['Applicant_Name'];
$query=mysql_query($sql);
$row=mysql_fetch_array($query);
IF ($userdata['user_name']!=$Applicant){
        echo "<tr><td><INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\"></td></tr>\n";
}elseif($userdata['user_name']==$Applicant){echo "<tr><td><INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\"></td></tr>\n";
}

?>

 

This code will display the apply button and the link works well at adding the member's name to the database, yet when the page reloads the same submit button shows up and the action is the same. 

Link to comment
Share on other sites

<?php
if ($userdata['user_name']!=$Applicant){
        echo "<tr><td><INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\"></td></tr>\n";}

elseif($userdata['user_name']=$Applicant)
{echo "<tr><td><INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\"></td></tr>\n";
}?>

Link to comment
Share on other sites

Look at the logic in your code. This line....

 

$Applicant=$row['Applicant_Name'];

 

You use an array ($row) that is not actually defined for another two lines. Try...

 

<?php

 $sql = "SELECT * From Applicants WHERE Project='$data_key'"; // where does $dat_key come from?
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     $row = mysql_fetch_array($result);
     if ($userdata['user_name'] != $row['Applicant_Name']) { // where does $userdata come from?
       echo "<tr><td><input type=\"submit\" name=\"apply\" value=\"Apply\"></td></tr>\n";
     } else {
       echo "<tr><td><input type=\"submit\" name=\"unapply\" value=\"Unapply\"></td></tr>\n";
     }
   }
 }

?>

?>

Link to comment
Share on other sites

$data_key is a variable passed from a previous page using "get".

 

Then you'll need to use....

 

<?php

  if (isset($_GET['data_key'])) {
    $data_key = mysql_real_escape_string($_GET['data_key']);
    $sql = "SELECT * From Applicants WHERE Project='$data_key'"; // where does $dat_key come from?
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_array($result);
        if ($userdata['user_name'] != $row['Applicant_Name']) { // where does $userdata come from?
          echo "<tr><td><input type=\"submit\" name=\"apply\" value=\"Apply\"></td></tr>\n";
        } else {
          echo "<tr><td><input type=\"submit\" name=\"unapply\" value=\"Unapply\"></td></tr>\n";
        }
      }
    }
  }

?>

Link to comment
Share on other sites

I tried that code and no buttons are showing up.  Do you think there is something wrong with my condition statement?  The table I'm working with has 3 columns, when the member applies, it should add the project number, the name of the member, and their user id.  The user id is not needed on this page.

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.