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

发布于 2024-11-01 13:53:53 字数 2629 浏览 0 评论 0原文

我有一个连接到我的数据库的 php 脚本,并且有两个下拉菜单。现在我想做的是,根据用户选择的内容,对用户选择运行查询。下拉菜单中的选项是不同的国家/地区。

我想保存此输入并使用该值来运行查询。即,如果用户选择美国和巴西,我将运行一些查询,例如从我的数据库中选择 *,其中国家 == 选择 1 和选择 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>

I have a php script that connects to my database and has two drop down menus. Now what I want to do is, based on what the user selection is, run queries on the user selection. The options in the drop down menus are different countries.

I want to save this input and use that value to run a query. I.e. if a user selects USA and Brazil, I will run some query like select * from my database where country == selection 1 and selection 2 (brazil and usa).

How exactly do I save the user selection from a drop down menu and use it to run a query like that? I am more concerned about actually setting up the query than writing the query.

Any help would be appreciated!

My code so far:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-11-08 13:53:53

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

<?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 框架,而不是像这样混合代码:)

It depends when you want to run your query. If query will be run after normal sending that just process sent request.

<?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
// ...
?>

It yours application will be larger then this single page, you should probably read something about todays popular MVC or use some php framework and not mix code like this:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文