Saturday, 15 February 2014

php - Insert Multiple combobox into one Field in database -



php - Insert Multiple combobox into one Field in database -

i has multiple combobox in page, here's code:

<select name="usulan[]" size="3" multiple> <option name="satu" value="satu"> satu </option> <option name="dua" value="dua"> dua </option> <option name="tiga" value="tiga"> tiga </option> <option name="empat" value="empat"> empat </option> </select>

then check more 1 alternative this:

i want every alternative check inserted 1 field (field_usulan) in database.

so if has table this:

id | alat | field_usulan ----------------------------- 1 | |

after inserted, want become this:

id | alat | field_usulan ----------------------------- 1 | rj45 | satu, dua

if utilize code:

<?php if(isset($_request['submit'])) { include "../conf/koneksi.php"; $count = count($_post['alat']); for($i = 0; $i < $count; $i++){ if(!empty($_post['alat'][$i])) { $alat = mysql_real_escape_string($_post['alat'][$i]); $usulan = mysql_real_escape_string($_post['usulan'][$i]); $sql= "alter table tb_inventaris auto_increment = 1"; mysql_query($sql); $sql2=mysql_query("insert tb_inventaris (alat,usulan) values ('$alat', '$usulan')") or die(mysql_error()); echo "<meta http-equiv=\"refresh\" content=\"0; url='../koordinator.php?url=index'\">"; }else{ echo "<meta http-equiv=\"refresh\" content=\"0; url='../koordinator.php?url=index'\">"; } } } ?>

it insert first option, this:

id | alat | field_usulan ----------------------------- 1 | rj45 | satu

sorry bad language.

while inserting, utilize php's implode() function.

$usulan = isset($_post['usulan']) ? implode(',', $_post['usulan']) : ''; $sql2=mysql_query("insert tb_inventaris (alat,usulan) values ('$alat', '$usulan')") or die(mysql_error());

hope works you.

php mysql

No comments:

Post a Comment