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
https://forums.phpfreaks.com/topic/74387-constructing-an-if-statement/
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";
}?>

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

?>

?>

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

?>

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.

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.