Jump to content

[SOLVED] Really need help on this one!


adamjones

Recommended Posts

Hi.

I have profile pages on my website for users, and want to incorporate a badge function, so on their profile page, people can see which badges they have, eg. 'Donator' etc..

 

In my database, it's simply the usernames, password, and in a new row, 'badges'. Here I would enter the badge names, all separated by commas, eg. 'donator,admin,vip' etc...

 

And this is the code used on their profile page;

 

<?php
session_start();
require_once('config.php');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

$qry="SELECT * FROM members WHERE username='$_SESSION[user];'";
$result=mysql_query($qry);

$getbadge = mysql_query($query) or die(mysql_error());

if($result) {
	while ($row = mysql_fetch_array($getbadge)) {
							$img = $row['badges'];

 echo "<img src='badges/".$img.".gif' alt='".$img."'>";
} else {
					echo "<p>".$error."</p><br>";
					}
}
					?>

 

So it will display the badge name '$img' with .gif at the end, for each user. But I get this error;

 

"Parse error: syntax error, unexpected T_ELSE in /home/wowdream/public_html/habhub/profile.php on line 25"

Link to comment
Share on other sites

im new to PHP but it looks like you have a } in the wrong place.

 

try this...

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='$_SESSION[user];'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($query) or die(mysql_error());
   
if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } 
} 
else  {
                  echo "<p>".$error."</p><br>";
                  }
?>

Link to comment
Share on other sites

im new to PHP but it looks like you have a } in the wrong place.

 

try this...

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='$_SESSION[user];'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($query) or die(mysql_error());
   
if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } 
} 
else  {
                  echo "<p>".$error."</p><br>";
                  }
?>

 

Thanks, its working, but I get this error on the page now, where the badge should be;

 

"Query was empty"

Link to comment
Share on other sites

You have an extra } at the bottom.

 

Should be:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='".$_SESSION['user']."'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($query) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>";

} else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

Link to comment
Share on other sites

 

Thanks, its working, but I get this error on the page now, where the badge should be;

 

"Query was empty"

 

thats outta my league...haha. picking out the extra } was just something I noticed right away with the if and while statements.  extra set of eyes always helps :D

Link to comment
Share on other sites

try this.

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='".$_SESSION['user']."'";
   $result=mysql_query($qry)or die(mysql_error());
   
if (mysql_num_rows($result)) {

      while ($row = mysql_fetch_array($result)) {
                       
$img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>";

} else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

Link to comment
Share on other sites

Should be:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='$_SESSION[user];'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($query) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>";
} 
}else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

I think.

Link to comment
Share on other sites

Should be:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM members WHERE username='$_SESSION[user];'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($query) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>";
} 
}else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

I think.

 

Thanks, but still get this error;

 

"Query was empty"

:(

Link to comment
Share on other sites

Try:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($qry) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
                        $img = $row['badges'];
                        
    echo "<img src='badges/".$img.".gif' alt='".$img."'>";
} 
}else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

You were calling $Query instead of $qry. Try the above code.

Link to comment
Share on other sites

Try:

<?php
  session_start();
  require_once('config.php');

  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  if(!$link) {
     die('Failed to connect to server: ' . mysql_error());
  }

  $db = mysql_select_db(DB_DATABASE);
  if(!$db) {
     die("Unable to select database");
  }

  $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'";
  $result=mysql_query($qry);

  $getbadge = mysql_query($qry) or die(mysql_error());

  if($result) {
     while ($row = mysql_fetch_array($getbadge)) {
                       $img = $row['badges'];

   echo "<img src='badges/".$img.".gif' alt='".$img."'>";
} 
}else {
                 echo "<p>".$error."</p><br>";
                 }
                 ?>

You were calling $Query instead of $qry. Try the above code.

 

Thank you SO much! You fixed it :)

 

I'm just wondering.. If a user has more than one badge, eg 'donater,vip', etc.. stored in the database, how could I show this in my code, as it would come up with

 

"badges/donate,vip.gif" - If that makes sense?

 

Cheers.

Link to comment
Share on other sites

How is that stored in the database?

As "donater,vip"?

If so you could try:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($qry) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
	  $imgs = explode(",",$row['badges']);
	  foreach ($imgs as $v)
	  	{
	  	echo "<img src='badges/".$v.".gif' alt='".$v."'>";	
	  	}
} 
}else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

 

Link to comment
Share on other sites

How is that stored in the database?

As "donater,vip"?

If so you could try:

<?php
   session_start();
   require_once('config.php');
   
   $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die("Unable to select database");
   }
   
   $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'";
   $result=mysql_query($qry);
   
   $getbadge = mysql_query($qry) or die(mysql_error());
   
   if($result) {
      while ($row = mysql_fetch_array($getbadge)) {
	  $imgs = explode(",",$row['badges']);
	  foreach ($imgs as $v)
	  	{
	  	echo "<img src='badges/".$v.".gif' alt='".$v."'>";	
	  	}
} 
}else {
                  echo "<p>".$error."</p><br>";
                  }
                  ?>

 

 

;o

 

It works! Thank you so much for your help!

:)

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.