-4

I need to get a random user name and go to view.php?name=USERNAME where am I wrong?

<form action="view.php?name=<?php $getname; ?>" method="get">
    <input class="button" type="submit" name="submit" value="RANDOM SKIN" />
</form>
<?php
    include('config.php');
    if ($_POST['submit']) {
        $sql1 = mysql_query("SELECT * FROM `skins` ORDER BY rand() limit 1") or die(mysql_error());
        while ($row2 = mysql_fetch_assoc($sql1)) {
            $getname = $row2['username'];
            echo $getname;
        }
    }
?>

2 Answers2

0

The form's method is set to GET but you're checking $_POST. Either use method="post" in the form or $_GET in the PHP code.

JJJ
  • 32,902
  • 20
  • 89
  • 102
0

Aside from the issue of alternating between $_GET and $_POST, if you want to keep your key value pairs from your GET request, this isn't a big issue. You have multiple options:

Recommended

Check out parse_url(). This is available from PHP 4 onward. The documentation has plenty of examples:

$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

Alternative

Try a var_dump() on your $_SERVER variables. You should be able to see a QUERY_STRING attribute which you can access with $_SERVER['QUERY_STRING']. Here, you can manually split your key / values using. Guess what, there's a function for that too!

Check out: parse_str(). You give it a query string, like that from a GET, and it'll sort out all your key values for you. Check out the documentation, it gives you the following example:

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

Pretty simple really! Try both these functions - they're there to solve your exact problem. To get the above working for you, I would first try:

var_dump(parse_str($_SERVER['QUERY_STRING']));

In closing, I'll be the one to talk about MySQL deprecation. Here's what I copy / paste to the newbies:

Please, don't use mysql_* functions in new code. They are no longer maintained, are officially deprecated, and can be dangerous in live code. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jimbo
  • 25,790
  • 15
  • 86
  • 131