11 Replies - 575 Views - Last Post: 25 July 2011 - 01:24 AM Rate Topic: -----

#1 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

data not showing...

Posted 24 July 2011 - 09:01 PM

Here's my page where clans can register their clan name:

<?php
  include('base.php');
  if(isset($_SESSION['username']))
  {
   echo "Welcome! <br /><br />";
  }
  else
  {
   echo "Please <a href=\"index.php\">login</a>";
  }
?>

<?php
  $query = mysql_query("SELECT * FROM clans");
  while($rows = mysql_fetch_array($query))
  {
  echo "<strong>Clan Name</strong>:<a href=\"clan_main.php?={$rows['clan']}\">" .$rows['clan'] ."</a>";
  echo "<br />";
  }
?>

<h1>Create Clan</h1>
<form action="clan_validate.php" method="post">
leader:<br />
        <input type="text" readonly="readonly" name="leader" value="<?php echo $_SESSION['username'];?>"><br /><br />
clan:<br />
      <input type="text" name="clan"><br /><br />
<input type="submit" value="submit">
</form>



Here's the page for each clan, but the problem is the data of each clan isn't viewing like i called it to view. Here is the error I am getting:

Quote

Notice: Undefined index: clan in C:\xampp\htdocs\clan_main.php on line 4


clan_main.php
<?php
  include('base.php');
  
  $clan = $_POST['clan'];
  $query = mysql_query("SELECT * FROM clans WHERE clan = '".$clan."'");
  while($row = mysql_fetch_array($query))
  {
   echo $row['clan'];
  }
  ?>


I don't understand how my data is not set.. I have them in phpmyadmin... I'm a bit confused.

This post has been edited by dangmnx: 24 July 2011 - 09:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: data not showing...

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 24 July 2011 - 10:50 PM

Hi there,
the problem is you're fetching the result as an numeric array the way you did.

you can either specify the result type using the optional argument of mysql_fetch_array() like this:
 while($row = mysql_fetch_array($query, MYSQL_ASSOC))



or you can use another function which is mysql_fetch_assoc() to get the result as an associative array.

This post has been edited by ahmad_511: 24 July 2011 - 10:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: data not showing...

Posted 24 July 2011 - 11:36 PM

I tired both

while($row = mysql_fetch_array($query, MYSQL_ASSOC))


and

while($row = mysql_fetch_assoc($query))


the error still remains the same :/
Was This Post Helpful? 0
  • +
  • -

#4 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 24 July 2011 - 11:53 PM

maybe this is a silly question but can you just var_dump the $row inside the while loop
var_dump($row);


and then check if the result contains expected data
Was This Post Helpful? 1
  • +
  • -

#5 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: data not showing...

Posted 25 July 2011 - 12:00 AM

<?php
  include('base.php');
  
  $clan = $_POST['clan'];
  $query = mysql_query("SELECT * FROM clans WHERE clan = '".$clan."'");
  while(var_dump($row));
  {
   echo $row['clan'];
  }
  ?>


get's me

Quote

Notice: Undefined index: clan in C:\xampp\htdocs\clan_main.php on line 4

Notice: Undefined variable: row in C:\xampp\htdocs\clan_main.php on line 6
NULL
Notice: Undefined variable: row in C:\xampp\htdocs\clan_main.php on line 8

Was This Post Helpful? 0
  • +
  • -

#6 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 25 July 2011 - 12:04 AM

I meant:
 while($row = mysql_fetch_assoc($query));
  {
   var_dump($row);
  }


Was This Post Helpful? 1
  • +
  • -

#7 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: data not showing...

Posted 25 July 2011 - 12:06 AM

Sorry, I got this.

Quote

Notice: Undefined index: clan in C:\xampp\htdocs\clan_main.php on line 4
bool(false)

Was This Post Helpful? -1
  • +
  • -

#8 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 25 July 2011 - 12:30 AM

that means your query didn't extract any data from the table, but wait a minute....
you have another problem here :)
take a look to your links:
echo "<strong>Clan Name</strong>:<a href=\"clan_main.php?={$rows['clan']}\">" .$rows['clan'] ."</a>";


you're not passing the clan variable at all

try
echo "<strong>Clan Name</strong>:<a href=\"clan_main.php?clan={$rows['clan']}\">" .$rows['clan'] ."</a>";


and another thing you're passing it through URL which means you have to use $_GET[] to read it

the question now is: are you seeing this error when using the form to submit the clan name or when using the generated links?
Was This Post Helpful? 2
  • +
  • -

#9 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: data not showing...

Posted 25 July 2011 - 12:46 AM

Ahhh! I'm only seeing this error after clicking on the link when i'm trying to view each clan link.

For ex: When i click on http://localhost/clan_main.php?=gt%29 that's when I get the error

Quote

Notice: Undefined index: clan in C:\xampp\htdocs\clan_main.php on line 4


The code for that page is:
<?php
  include('base.php');
  
  $clan = $_GET['clan'];
  $query = mysql_query("SELECT * FROM clans WHERE clan = '".$clan."'");
  while($row = mysql_fetch_assoc($query));
 {
  echo $row['leader'];
 }
  ?>


I changed
$_POST['clan'];
to
$_GET['clan'];
yet still the error remains :(


If this help, this the page where my clan register.
<?php
  include('base.php');
  if(isset($_SESSION['username']))
  {
   echo "Welcome! <br /><br />";
  }
  else
  {
   echo "Please <a href=\"index.php\">login</a>";
  }
?>

<?php
  $query = mysql_query("SELECT * FROM clans");
  while($rows = mysql_fetch_array($query))
  {
  echo "<strong>Clan Name</strong>:<a href=\"clan_main.php?clan={$rows['clan']}\">" .$rows['clan'] ."</a>";
  echo "<br />";
  }
?>

<h1>Create Clan</h1>
<form action="clan_validate.php" method="post">
leader:<br />
        <input type="text" readonly="readonly" name="leader" value="<?php echo $_SESSION['username'];?>"><br /><br />
clan:<br />
      <input type="text" name="clan"><br /><br />
<input type="submit" value="submit">
</form>

I changed the URL: as you instructed me to do also.

Here's the sql dump:

CREATE TABLE IF NOT EXISTS `clans` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `leader` varchar(15) NOT NULL,
  `clan` varchar(50) NOT NULL,
  `members` varchar(15) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `clans`
--

INSERT INTO `clans` (`id`, `leader`, `clan`, `members`) VALUES
(2, 'dang', 'gt)', ''),
(3, 'dang', 'gt)-', '');

Was This Post Helpful? 0
  • +
  • -

#10 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 25 July 2011 - 01:14 AM

Well, did you by accident left a semicolon somewhere :) that may break the while loop
in the clan_main.php remove the semicolon at the end of this line:
while($row = mysql_fetch_assoc($query));


Was This Post Helpful? 1
  • +
  • -

#11 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: data not showing...

Posted 25 July 2011 - 01:17 AM

ah HA! thank you very so much! it worked!

So it was the URL that was the problem right?
Was This Post Helpful? 0
  • +
  • -

#12 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: data not showing...

Posted 25 July 2011 - 01:24 AM

the URL, the $_GET and the semicolon :)
and just to correct myself about result_type argument of the mysql_fetch_array() function,
the default is MYSQL_BOTH as stated in the php manual, so you can either access fetched columns by index or by name.
so sorry for any inconvenience :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1