简体   繁体   English

从下拉菜单的单个选择标签向MySql插入2个值

[英]Inserting 2 values to MySql from a single selection tag of drop down menu

Is there a way to insert 2 values to Mysql from a single selection of drop down menu? 有没有一种方法可以从一个下拉菜单中向Mysql插入2个值?

The example of mysql syntax for inserting the form values is as following: 用于插入表单值的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));
}

I'm trying to insert values of 2 columns (food & image_extension) from each selected tag of the following dropdown list to MySql but it can't insert data to image_extension column. 我正在尝试从下面的下拉列表的每个选定标签中将2列(food和image_extension)的值插入MySql,但无法将数据插入到image_extension列。 It only update food column. 它仅更新食物列。

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

I'm confused about how to put values in the following three attributes of the above dropdown list properly in this case? 在这种情况下,我对如何将值正确放入上述下拉列表的以下三个属性感到困惑?

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

Any guideline shall be gighly appreciated. 任何指导原则都应得到赞赏。

Instead of using two different names for the select just grab the value from the select menu and turn it into an array 无需使用两个不同的名称进行选择,只需从选择菜单中获取值并将其转换为数组即可

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

The problem has been solved anyway with the precise guideline of an expert fellow in www.phpbuilder.com I'm presenting the whole solved scenario below: 无论如何,这个问题已经通过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)); 
} 

The html part should be changed to the following: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM