从多个选择框向MySQL发送多条记录

时间:2009-06-30 12:29:02

标签: php mysql select

我正在尝试将多行插入MySQL表,具体取决于从多个选择框中选择的选项数。目前它正在插入一行(无论选择了多少个选项),但每次“strategyname”列都为空。

关于如何插入多行以及为什么没有将选项的值发送到表中的任何想法?

以下是表格:

<form method="POST" action="update4.php">

<input type="hidden" name="id" value="1">

<p class="subheadsmall">Strategies</p>

<p class="sidebargrey">

<?php

            $result = mysql_query("SELECT strategyname FROM sslink WHERE study_id = '{$_GET['id']}'");
                if (!$result) {
                    die("Database query failed: " . mysql_error());
                }

while($row = mysql_fetch_array($result)) {
    $strategyname = $row['strategyname'];


    echo $strategyname.'<br />';
}

?>
        <p class="subheadsmall">Add a strategy... (hold down command key to select more than one)</p>

<select name="strategylist" multiple="multiple">
     <?php

            $result = mysql_query("SELECT * FROM strategies");
                if (!$result) {
                    die("Database query failed: " . mysql_error());
                }

while($row = mysql_fetch_array($result)) {
    $strategylist = $row['name'];
    $strategyname = htmlspecialchars($row['name']);
    echo '<option value="' . $strategylist . '" >' . $strategyname . '</option>' . '\n';
}

?>
</select>
    </p>



<input type="submit" class="box" id="editbutton" value="Update Article">

</form>

这就是将其发送到数据库的原因:

<?php
$id=$_POST['id'];
$test=$_POST['strategylist'];
$db="database";

$link = mysql_connect("localhost", "root", "root");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());
//for($i=0;$i<sizeof($_POST["test"]);$i++)
//{
//$sql = "insert into tbl_name values ($_POST["test"][$i])"; }
//sql = "INSERT INTO table_name VALUES ('" . join(",",$_POST["test"]) . "')";
$result=mysql_query("INSERT INTO sslink (study_id, strategyname) VALUES ('$id','" . join(",",$_POST["strategylist"]) . "')")or die("Insert Error: ".mysql_error());
mysql_close($link);
print "Record added\n";
?>

3 个答案:

答案 0 :(得分:3)

几点:

  • 您的select需要命名为strategylist[]才能告诉PHP它将包含数组而不是单个值
  • 然后,您的插入代码需要遍历该数组,为其包含的每个元素创建一个新插入,除非(看起来如此)您希望将所有这些选项连接到一行的字段中。

目前,您的表单只返回一个选项(从PHP的角度来看),所以它只会插入一行。

要迭代数组,请使用以下内容:

foreach($_POST["strategylist[]"] as $s) {
    # do the insert here, but use $s instead of $_POST["strategylist[]"]
    $result=mysql_query("INSERT INTO sslink (study_id, strategyname) " .
       "VALUES ('$id','" . join(",",$s) . "')")
        or die("Insert Error: ".mysql_error());
}

答案 1 :(得分:2)

两件事:

  1. 如果您查看其中包含多个选择的网页来源,您能看到<option value="something">行吗?值是空的吗?我觉得奇怪的是,在您的文件顶部,您使用的是$row['strategyname'],之后您正在使用$row['name']。我怀疑这可能是空StrategyName列的原因。

  2. 要处理多项选择,您应将select标记指定为

    <select name="strategylist[]" multiple="multiple">
    

    额外的[]告诉PHP形成一个包含所有选择的数组。然后,您可以循环遍历数组:

    $strategylist = $_POST['strategylist'];
    for ($i = 0; $i < count($strategylist); $i++) {
        $strategyname = $strategylist[$i];
        // Insert a record...
    }
    

答案 2 :(得分:0)

// first you need to define your output as one variable if you don't like the loop 
   if($_POST){
   $sum = implode(", ", $_POST[select2]);
   echo $sum.".";
}
// the variable sum is the one you are seeking for you can insert it to the database
// if you want to enter every peiece of the array in a new field you should use
// different select names