0
我有一個連接到我的數據庫的php腳本,並有兩個下拉菜單。現在我想要做的是,根據用戶選擇的內容,對用戶選擇運行查詢。下拉菜單中的選項是不同的國家/地區。php從下拉菜單中保存用戶選擇並運行查詢
我想保存此輸入並使用該值運行查詢。即如果用戶選擇美國和巴西,我會運行一些查詢,如select *從我的數據庫中選擇國家==選擇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>