Php / mysql bir damla listeden tüm seçenekleri nasıl

1 Cevap php

Thanks to stackoverflow.com's frienly experts I've managed to create my first php + mysql application. The code searches a mysql database for last names and cities. The choices are made through two drop lists like these:

Choose city:
All cities
Liverpool
Manchester

Choose last name:
All last names
Lennon
Gallagher

Kod, örneğin dönecekti. Liverpool yaşayan tüm Lennons.

Ancak, kod örneğin dönecekti böylece seçenekleri "Tüm şehirler" ve iş "Tüm soyadları" yapmak mümkün olmamıştır. Tüm Lennons herhangi bir şehirde yaşayan ya da tüm insanların Liverpool yaşıyor. Peki, bu nasıl yapılabilir?

Şimdiye kadar kodu:

index.php

<?php
$conn = mysql_connect('localhost', 'user', 'password') or die("Connection failed");
mysql_select_db("database", $conn) or die("Switch database failed");

//this gets the cities from the database to the drop list
$query = "SELECT DISTINCT city FROM user".mysql_real_escape_string($city);
$result = mysql_query($query, $conn);
$options="";
while ($row=mysql_fetch_array($result)) {
$city=$row["city"];
$options.="<OPTION VALUE=\"$city\">".$city;
}

//this gets the last names from the database to the drop list
$query2 = "SELECT DISTINCT lastname FROM user".mysql_real_escape_string($lastname);
$result2 = mysql_query($query2, $conn);
$options2="";
while ($row2=mysql_fetch_array($result2)) {
$lastname=$row2["lastname"];
$options2.="<OPTION VALUE=\"$lastname\">".$lastname;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>test</title>
</head>
<body>

<form action="get.php" method="post">
<p>
<select name="city">
<option value=0>Choose
<option value=1>All cities
<?=$options?>
</select>
</p>
<p>
<select name="lastname">
<option value=0>Choose
<option value=1>All last names
<?=$options2?>
</select>
</p>
<p>
<input value="Search" type="submit">
</p>
</form>

<br>
</body>
</html>

get.php

<?php

$conn = mysql_connect('localhost', 'user', 'password') or die("Connection failed");
mysql_select_db("database", $conn) or die("Switch database failed");

$query = "SELECT * FROM user WHERE city = '".mysql_real_escape_string($_POST['city'])."' AND lastname = '".mysql_real_escape_string($_POST['lastname'])."'";
$result = mysql_query($query, $conn);

echo $rowcount;
$zerorows=true;
while ($row = mysql_fetch_assoc($result)) 
{
$zerorows=false;
echo '<b>City: </b>'.htmlspecialchars($row[city]).'<br />';
echo '<b>Last name: </b>'.htmlspecialchars($row[lastname]).'<br />';
echo '<b>Information: </b>'.htmlspecialchars($row[information]).'<br />'.'<br />';
}
if($zerorows) echo "No results";

mysql_close($conn);
?>

1 Cevap

Sadece (örneğin ALL gibi) tüm özel bir değere sahip ve koymak:

if ($_POST['city'] == 'ALL')
{
    $sql_city = '1';
}
else
{
    $sql_city = "city = '".mysql_real_escape_string($_POST['city'])."'";
}

if ($_POST['lastname'] == 'ALL')
{
    $sql_lastname = '1';
}
else
{
    $sql_lastname = "lastname = '".mysql_real_escape_string($_POST['lastname'])."'";
}

$query = "SELECT * FROM user WHERE ".$sql_city." AND '".$sql_lastname."";

Sonra eğer en dayalı SQL ifadesi oluşturmak. 1'i kullanmak nedeni her zaman doğru değerlendirir olmasıdır.