2012-04-21 45 views
0

有沒有一種方法可以從單個下拉菜單選擇中將2個值插入Mysql?從下拉菜單的單個選擇標記中插入2個值到MySql

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> 

任何指導方針都應該被高度讚賞。

回答

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

在這種情況下應該是什麼',那麼每當你通過post傳遞這個值時,這個值將等於「pizza,pizza.jpg」,那麼你會發現上面的爆炸將它轉換成你可以插入的數組進入數據庫 – chadpeppers 2012-04-21 23:09:17

+0

查看上面的新修訂 – chadpeppers 2012-04-21 23:17:02

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> 
相關問題