Jump to content

Simple question regarding "if" statements


xwishmasterx

Recommended Posts

I have a table where a filed called "recruitment" can either be "0" or "1"  Why does my below code not work?

 

$sql_rec = ("SELECT * FROM teams WHERE team_id=".$_GET['t']." ");
$rsrec = mysql_fetch_array($sql_rec);
if($rsrec['recruitment'] == 1){
echo "Your team is <b>OPEN</b> for all members to join <br>
<a href='changemode0.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";}
elseif($rsrec['recruitment'] == 0){
echo "Your team is <b>CLOSED</b> for all members to join <br>
<a href='changemode1.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";}

 

to ask in another way: how do I make the if statement to check if the value is "1" or "0"?

Link to comment
https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/
Share on other sites

You aren't executing the query, and you have no logic in place to make sure the query succeeds before you attempt to access any result. This should produce the results you're looking for, and I added some very basic error handling/reporting.

 

$query = ("SELECT recruitment FROM teams WHERE team_id={$_GET['t']}");
if( !$result = mysql_query($query) ) {
echo "Query failed";
} else {
if( mysql_num_rows($result) === 1 ) {
	$array = mysql_fetch_assoc($sql_rec);
	$one = "Your team is <b>OPEN</b> for all members to join <br><a href='changemode0.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";
	$zero = "Your team is <b>CLOSED</b> for all members to join <br><a href='changemode1.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";
	echo $array['recruitment'] == 1 ? $one : $zero;
} else {
	echo 'Query returned empty result set';
}
}

Add the line indicated in the comment, and post the output it generates.

 

     echo $array['recruitment'] == 1 ? $one : $zero;
     echo '<br>var_dump: ' . var_dump($array['recruitment']) . '<br>'; // <---- ADD THIS
} else {

Deleting the "leftover" worked! Thanks a bunch.

 

If any of you can tell me why I cannot use this url: (returns unexpected T-STRING)

changemode1.php?t=".$_GET['t']."&r=".$_GET['r']."

 

or perhaps tell me how I can rewrite it , then I'll be happy as a hamster! ;)

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.