php mysql 多行_PHP多重选择表单可在MySQL中插入多行

bd96500e110b49cbb3cd949968f18be7.png

I have the following code used to insert a record using PHP into a MySQL database. The form element is a multiple select that works fine when only one option is selected. When I choose more than 1 option, only the last option is inserted. How do I have the form create a new row for each option selected in the multiple select?

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

$insertSQL = sprintf("INSERT INTO emplikons (EmpNo, IconId) VALUES (%s, %s)",

GetSQLValueString($_POST['insertRecordID'], "text"),

GetSQLValueString($_POST['icons'], "int"));

mysql_select_db($database_techsterus, $techsterus);

$Result1 = mysql_query($insertSQL, $techsterus) or die(mysql_error());

This is the code of the form element. It uses a recordset to dynamically pull values from another table:

do {

?>

} while ($row_icons = mysql_fetch_assoc($icons));

$rows = mysql_num_rows($icons);

if($rows > 0) {

mysql_data_seek($icons, 0);

$row_icons = mysql_fetch_assoc($icons);

}

?>

解决方案

add [] to the name of the select, to get an array of all selected values.

Then, read from $_POST / $_GET... (assuming form method="post")

$sql = '';

foreach ($_POST['icons'] as $icon) {

// no idea where to get EmpNo from, let's assume it is in $empno.

$sql .= "('$empno','$icon'),";

}

$sql = "INSERT INTO tablename (EmpNo, IconId) VALUES " . trim($sql,',');

// Now, please use mysqli_* or PDO instead of mysql_*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值