Korne127 Korne127 - 5 months ago 16
SQL Question

Mysql Database doesn't work with hyphen (-)

The user says in which federal state (Germany) he lives.
The important code is really easy:

echo "<a href=\"FirstLogin.php?Name=Baden-Würtemberg\">Baden-Würtemberg</a><br>";
echo "<a href=\"FirstLogin.php?Name=Bayern\">Bayern</a><br>";
echo "<a href=\"FirstLogin.php?Name=Berlin\">Berlin</a><br>";
echo "<a href=\"FirstLogin.php?Name=Brandenburg\">Brandenburg</a><br>";
echo "<a href=\"FirstLogin.php?Name=Bremen\">Bremen</a><br>";
...
exit (0);


The user clics on the link which shows the name of the federal state in which he lives and he'll be redirected to FirstLogin.php with the federal state as $_GET.

Now, the Problem:

On this site the user should choose the town in which he live.

In a mysql database all towns are saved.

All the towns, which are in the federal state the user have chosen, will be shown:

$FederalState = $_GET["Name"];
$sql = "SELECT Towns FROM Towns WHERE Federal State = '$FederalState'";
foreach ($pdo->query($sql) as $row) {
$Towns[] = $row["Towns"];
}
for ($i=0;$i<count($Towns);$i++) {
$Town = $Towns[$i];
echo "<a href=\"NextPage.php?Name=$Town\">$Town</a><br>";
}


That all is working well if the user chooses "Bayern", "Berlin" or an other normal federal state. But, if the user chooses a federal state with an - (Hyphen or Minus) it doesn't work! No links come!

And if I make

foreach ($pdo->query($sql) as $row) {
echo $row["Staedte"]."<br>";
}


The towns of the federal state are only shown if the federal state doesn't have a hypen respectively Minus (-).

I don't know what to do to make the script work.

Please help me and give me a solution.

EDIT 1:
The matter is not the hyphen,
the matter is the ü in Baden-Würtemberg,
I have tried it with "Nordrhein-Westfahlen" and it worked
But at the beginning I have used
<meta charset="utf-8"/>

Answer

Instead of this:

$FederalState = $_GET["Name"];
$sql = "SELECT Towns FROM Towns WHERE Federal State = '$FederalState'";
    foreach ($pdo->query($sql) as $row) {
        $Towns[] = $row["Towns"];
    }
for ($i=0;$i<count($Towns);$i++) {
    $Town = $Towns[$i];
    echo "<a href=\"NextPage.php?Name=$Town\">$Town</a><br>";
}

Try This Code:

$pdo->exec("set names utf8");
$FederalState = urldecode ($_GET["Name"]);
$sql = "SELECT Towns FROM Towns WHERE Federal State = '$FederalState'";
    foreach ($pdo->query($sql) as $row) {
        $Towns[] = $row["Towns"];
    }
for ($i=0;$i<count($Towns);$i++) {
    $Town = $Towns[$i];
    echo "<a href=\"NextPage.php?Name=$Town\">$Town</a><br>";
}

Hope this works for you...