Jump to content

PHP Help!


Ratee

Recommended Posts

Okay so i'm making a map where users can go search for Pokemon and capture them... for my new Pokemon game. but when i click to capture them

it does not work... no error messages or nothing shows up it just refreshes the page and that's it.. please could someone help me to work this out as i am a Beginner, iv posted my code below, thanks:)

 

 

The PHP

<?php
$map = rand(1, 10);
$result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1");
while($row = mysql_fetch_array($result))

  if ($map==1) {
  echo "</br>";
  echo $row['image'];
  echo "<br>";
  echo "A wild ";
  echo  $row['name'] . " Appeared. </br> Level: " . $row['level'];
  echo "<br />";
  echo "<input type='hidden' name='$row[name]'>";
  echo "<form action='map1.php' method=post>";
  echo "<input type='hidden' name='$row[name]'>";
  echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' /></form>";
  }else{
  
echo "<div class=\"pokemontext\">";
echo "<b>No wild Pokémon appeared.</b>";
echo "</br>";
echo "<i>Keep moving around to find one.</i>";
echo "<div>";
}

if(isset($_POST['$row[name]'])) 
{
echo "<div class=\"pokemonalign\">";
echo $row['image'];
echo "</br>";
echo 'You have captured Wild';
echo $row['name'] . "";
echo "</div>";
$add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')");
}
else
{
}
?>

 

 

SQL

Table structure for table `firemap`
  `id` bigint(255) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `level` int(11) NOT NULL,
  `image` varbinary(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;

 

Thank you for your time.

Link to comment
Share on other sites

That will do nothing. That would check if a form field with the literal name of $row[name] had been sent.

What he's doing is giving the fields the name of the pokemon. He needs to instead make the name something static like "pokemon_id" and use the id of the value, then check if $_POST['pokemon_id'] is set.

Link to comment
Share on other sites

When you submit the form, the entire page gets processed again, which means that $row['name'] will have a different value, since you're doing it randomly.

 

Make the name of the form field (of which you have two, you only need it once), something like id, whatever your primary key is. Then for the value of the hidden input you'd do $row['id'].

 

I have been under the impression that it's the other way around

Yes, you were right, I apologize. The original way he has it is checking for the literal string - but changing it to your way won't work, because the value of $row['name'] will likely be different, due to the random factor.

Link to comment
Share on other sites

Following jesirose's suggestion, here's what you want (I'm bored to hell)

 

<?php

if (isset($_POST["pokemon_id"]))
{
    $result = mysql_query("SELECT * FROM firemap WHERE id = " . intval($_POST["pokemon_id"]));

    $row = mysql_fetch_assoc($result); // or whatever you do with plain mysql functions

    if (isset($row['id']))
    {
        echo "<div class=\"pokemonalign\">";
        echo $row['image'];
        echo "</br>";
        echo 'You have captured Wild';
        echo $row['name'] . "";
        echo "</div>";
        $add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')");
    }
}


$map = rand(1, 10);
$result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1");
while ($row = mysql_fetch_array($result))
{
    if ($map == 1)
    {
        echo "<br />";
        echo '<img src="' . $row['image'] . '" alt="" />';
        echo "<br />";
        echo "A wild ";
        echo $row['name'] . " Appeared. <br /> Level: " . $row['level'];
        echo "<br />";
        echo "<form action='map1.php' method='post'>";
        echo "<input type='hidden' name='pokemon_id' value='$row[id]' />";
        echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' />";
        echo "</form>";
    }
    else
    {
        echo "<div class=\"pokemontext\">";
        echo "<b>No more wild Pokémon appeared.</b>";
        echo "<br />";
        echo "<i>Keep moving around to find one.</i>";
        echo "</div>";
    }
}
?>

Link to comment
Share on other sites

okay thank you so much for your help everyone! there is just one small problem still...

 

when the Pokemon is captured is still displays "No more wild Pokémon appeared.

Keep moving around to find one."

 

i was wondering if there is anyway to get rid of this when a Pokemon is caught?

Link to comment
Share on other sites

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (isset($_POST["pokemon_id"]))
    {
        $result = mysql_query("SELECT * FROM firemap WHERE id = " . intval($_POST["pokemon_id"]));

        $row = mysql_fetch_assoc($result); // or whatever you do with plain mysql functions

        if (isset($row['id']))
        {
            echo "<div class=\"pokemonalign\">";
            echo $row['image'];
            echo "</br>";
            echo 'You have captured Wild';
            echo $row['name'] . "";
            echo "</div>";
            $add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')");
        }
    }
}
else
{
    $map = rand(1, 10);
    $result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1");
    while ($row = mysql_fetch_array($result))
    {
        if ($map == 1)
        {
            echo "<br />";
            echo '<img src="' . $row['image'] . '" alt="" />';
            echo "<br />";
            echo "A wild ";
            echo $row['name'] . " Appeared. <br /> Level: " . $row['level'];
            echo "<br />";
            echo "<form action='map1.php' method='post'>";
            echo "<input type='hidden' name='pokemon_id' value='$row[id]' />";
            echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' />";
            echo "</form>";
        }
        else
        {
            echo "<div class=\"pokemontext\">";
            echo "<b>No more wild Pokémon appeared.</b>";
            echo "<br />";
            echo "<i>Keep moving around to find one.</i>";
            echo "</div>";
        }
    }
}
?>

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.