doudi8829 2014-04-20 18:50
浏览 20

如何使用<select>选项将多个项目发布到mysql

I am working with smarty and would like to know if there is a way to post multiple select items to MySQL. currently on submit of form it stores just one items although the < select > is set to multiple.

here is my code.

<select  name="name" class="box-20" multiple="multiple">
<optgroup label="Clients">
{foreach from=$client item=client}
<option value="{$client.FNAME}">{$client.FNAME}&nbsp;{$client.LNAME}</option>
{/foreach}
</optgroup>
<optgroup label="Type">
{foreach from=$type item=type}
<option value="{$type.CTYPE}">{$type.CTYPE}<option>
{/foreach}
</optgroup>
</select>

this outputs

Clients

<option value="Sam">Sam Adams</option>
<option value="nan">Jan John</option>
<option value="Lincoln">Lincoln Smith</option>
<option value="Julie">Julie Bliss</option>

Type

<option value="1">1</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

MySQL code

if(isset($VAR['submit'])) {
 $sql = "UPDATE CLIENTS  SET 

NAME  =".$db->qstr($VAR["name"])."

WHERE CUST  = ". $db->qstr( $VAR['cust_id']);


    if(!$result = $db->Execute($sql)) {
    force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
    exit;
}
}

Even if I select them all, it stores just the last option I select in MySQL. Thank you all so much!!

  • 写回答

2条回答 默认 最新

  • drrqwokuz71031449 2014-04-20 18:52
    关注

    Change this -

    <select  name="name[]" class="box-20" multiple="multiple">
    

    You can see that I have replaced name="name" to name="name[]". What this does is, since you are selecting multiple option, it creates an array of what you've selected.

    e.g

    echo '<pre>'; print_r($_POST['name']);  // will generate following array
    
    Array (
     [0] => client1
     [1] => client2  
     [2] => client3
    )
    
    评论

报告相同问题?