views:

106

answers:

3
http://localhost/?area=characters&name=Michal+Stroganof



$result = mysql_query("SELECT * from players WHERE name = '$_GET[name]'");

while ($row = mysql_fetch_assoc($result)) {

    echo "Name: " .$row['name']. "<br>";
    echo "Level: " .$row['level']. "<br>";

}

This is all code of my characters.php

If the get variable "name" is not included in the URL i want to show a search form that searches the table players. How would I do this?

+8  A: 

Do you mean just to change your SQL string like so?

$sql = 'SELECT * from players';
if (isset($_GET['name'])) {
    $safename = mysql_real_escape_string($_GET['name']);
    $sql .= " WHERE name='$safename'";
}
$result = mysql_query($sql);

Be sure to sanitize your SQL!

jheddings
+5  A: 

Use isset():

if (isset($_GET['name'])) {
    // your above code
} else {
    // display form        
}
yjerem
Ah, good catch... I misread the OP's question.
jheddings
+1  A: 

Quick and dirty:

<?php
if (!isset($_GET['name']))
{
    echo  '<form action="'. $_SERVER['PHP_SELF'] .'" method="GET">'
         .'<input type="text" name="name" />'
         .'</form>';
}
else
{
    // your current code that queries your database here
}
?>
JP