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

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.