PHP从下拉菜单中保存用户选择并运行查询


php saving user selection from a dropdown menu and running a query

我有一个php脚本连接到我的数据库,并有两个下拉菜单。现在我要做的是,基于用户选择,在用户选择上运行查询。下拉菜单中的选项是不同的国家。

我想保存这个输入并使用该值来运行查询。例如,如果用户选择美国和巴西,我将运行一些查询,如select * from my database where country == selection 1和selection 2(巴西和美国)。

我如何从下拉菜单中保存用户选择并使用它来运行这样的查询?我更关心实际设置查询,而不是编写查询。

任何帮助将不胜感激!

我的代码:

<html>
<head>
<title> Welcome! </title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<Form Name ="form1" Method ="POST" ACTION = "page1.php">
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link)
{
 $output = 'Unable to connect to the database server.';
 include 'output.html.php'; 
 exit();
}
mysql_select_db('top recipes');
if (!mysql_select_db('top recipes'))
{
 $output = 'Unable to locate the joke database.';
 include 'output.html.php';
 exit();
}
function dropdown( $name, array $options, $selected=null )
{
    /*** begin the select ***/
    $dropdown = '<select name="'.$name.'" id="'.$name.'">'."'n";
    $selected = $selected;
    /*** loop over the options ***/
    foreach( $options as $key=>$option )
    {
        /*** assign a selected value ***/
        $select = $selected==$key ? ' selected' : null;
        /*** add each option to the dropdown ***/
        $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."'n";
    }
    /*** close the select ***/
    $dropdown .= '</select>'."'n";
    /*** and return the completed dropdown ***/
    return $dropdown;
}
function dropdowntwo( $nametwo, array $optionstwo, $selectedtwo=null )
{
    /*** begin the select ***/
    $dropdowntwo = '<select name="'.$nametwo.'" id="'.$nametwo.'">'."'n";
    $selectedtwo = $selectedtwo;
    /*** loop over the options ***/
    foreach( $optionstwo as $key=>$option )
    {
        /*** assign a selected value ***/
        $select = $selectedtwo==$key ? ' selectedtwo' : null;
        /*** add each option to the dropdown ***/
        $dropdowntwo .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."'n";
    }
    /*** close the select ***/
    $dropdowntwo .= '</select>'."'n";
    /*** and return the completed dropdown ***/
    return $dropdowntwo;
}
?>
<form>
<?php
$name = 'my_dropdown';
$options = array( 'USA', 'Brazil', 'Random' );
$selected = 1;
echo dropdown( $name, $options, $selected );
$nametwo = 'my_dropdowntwo';
$optionstwo = array( 'USA', 'Brazil', 'Random' );
$selectedtwo = 1;
echo dropdowntwo( $nametwo, $optionstwo, $selectedtwo );
?>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Select">
</form>

这取决于您想在何时运行查询。如果查询将在normal发送后运行,则只处理发送的请求。

<?php
if (!empty($_POST['my_dropdown'])) {
  $country1 = $_POST['my_dropdown'];
  // validate if $country1 is in allowed values or use at least
  $country1 = mysql_real_escape_string($country1);
}
// similar for my_dropdowntwo => $country2
// process only with both values?
if (!empty($country1) && !empty($country2)) {
    // you can strore it into $_SESSION if you want - you need to run session_start() before headers!
    $_SESSION['country1'] = $country1;
    // or store only to cookies if it should be perzistent
    setcookie("country1", $country1, time()+3600,, '/');
    // or you can use use variables directly if you want to run query now -> use $country1 or $country2 variabes
}
// if you want to use later stored variables
// first test if you have both variables ...
// then
$query = sprintf("SELECT something FROM someTable where country = '%s' AND country2 = '%s', $_SESSION['country1'], $_SESSION['country2']);
// or use $_COOKIE['country1'] if you use cookies instead
// ...
?>

如果你的应用程序比这个单页更大,你可能应该阅读一些今天流行的MVC或使用一些php框架,而不是像这样混合代码:)