0

有没有办法从一个下拉菜单中向 Mysql 插入 2 个值?

用于插入表单值的 mysql 语法示例如下:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO menu (food, image_extension) VALUES (%s, %s)",
GetSQLValueString($_POST['food'], "text"),
GetSQLValueString($_POST['image_extension'], "text"),

mysql_select_db($database_menu, $menu);
$Result1 = mysql_query($insertSQL, $menu) or die(mysql_error());

$insertGoTo = "menu.php?status=choosen";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

我正在尝试将以下下拉列表的每个选定标记中的 2 列(食物和 image_extension)的值插入到 MySql,但它无法将数据插入到 image_extension 列。它只更新食物栏。

<select name="food, image extension" class="dropdownmenu" input id="food" value="<?php echo $_POST['food'].$_POST['image_extension']; ?>"> 
<option value="selected="selected">Select Food</option>
<option value="Pizza, pizza.jpg">Pizza</option>
<option value="French Fry' frenchfry.jpg">French Fry</option>
</select>

在这种情况下,我对如何在上述下拉列表的以下三个属性中正确放置值感到困惑?

1. <select name="food, image extension" 
2. <select value="<?php echo $_POST['food'].$_POST['image_extension']; ?>" 
3. <option value="Pizza, pizza.jpg">Pizza</option>

任何指南都将不胜感激。

4

2 回答 2

0

无论如何,这个问题已经通过 www.phpbuilder.com 专家研究员的精确指导解决了,我将在下面展示整个解决方案:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 

//("," Comma within the double quote shall be used as delimiter here) 
$food=explode(",",$_POST['food']); 
$insertSQL = sprintf("INSERT INTO menu (food, image_extension) VALUES (%s, %s)", 

// The column names in the following string should be replaced by the newly created 
// array elements along with trim function to remove any unexpected white space.       

GetSQLValueString (trim($food[0]), "text"), 
GetSQLValueString (trim($food[1]), "text");

mysql_select_db($database_menu, $menu); 
$Result1 = mysql_query($insertSQL, $menu) or die(mysql_error()); 

$insertGoTo = "menu.php?status=choosen"; 
if (isset($_SERVER['QUERY_STRING'])) { 
$insertGoTo .= (strpos($insertGoTo, '?')) ? "" : "?"; 
$insertGoTo .= $_SERVER['QUERY_STRING']; 
} 
header(sprintf("Location: %s", $insertGoTo)); 
} 

html部分应更改为以下内容:

 <select name="food" class="dropdownmenu" input id="food" 
 value="<?php echo  $_POST['food']; ?>">
 <option value="selected="selected">Select Food</option>
 <option value="Pizza, pizza.jpg">Pizza</option>
 <option value="French Fry' frenchfry.jpg">French Fry</option>
 </select>
于 2012-04-23T08:39:14.087 回答
0

而不是为选择使用两个不同的名称,只需从选择菜单中获取值并将其转换为数组

<?php

$food = explode (",", $_POST['food']);

//$food[0] will equal Pizza, $food[1] will equal pizza.jpg
$insertSQL = "INSERT INTO menu (food, image_extension) VALUES ({$food[0]}, {$food[1]})";

?>

<select name="food" class="dropdownmenu" id="food">
<option value="Pizza, pizza.jpg">Pizza</option>
<option value="French Fry, frenchfry.jpg">Pizza</option>
</select>
于 2012-04-21T15:34:22.897 回答