Jump to content

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! ;)

Whoops, I missed that one. But when you say you deleted it, I hope you mean you changed it to $result . . .

 

As far as the error above, post the complete line that is generating it.

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.