在php中传递多个选项值

时间:2017-08-16 09:35:11

标签: php

我正在尝试为php中的选项分配多个值,以便我可以在以后单独提取它们并插入到mysql中

我在试图分配的方式。

    // Transfer Li from Current to completed.
$("ul").on("click", ".move", function(){  
  $(this).parent().remove().appendTo( "#container1 > #delComp");
  $(this).parent().removeClass("move comp").addClass("appendToDo comp2 comp1");
  $(this, ':nth-child(2)').remove();
});

这就是我试图提取它们的方式

<select name="val[parent]">
<?php
if($cnt > 0){
while($parent = mysqli_fetch_assoc($sql))
{
?>
<option value="<?php echo $parent['category_id']. "-" . $parent['trade_id'];?> "> <?php echo $parent['trade_name']; ?> </option>
<?php }} ?>
</select>

我不知道如何提取它们分配它们。请帮忙。

这是完整表格

if(isset($_POST['val']))
{
    list($category_id, $trade_id) = explode("-", $_POST['val'], 2);
    $aVals = $_POST['val'];
    $category_id = 0;
    $category_name = $aVals['category_name'];
    $parent = $aVals['parent'];
    $trade_id =$aVals['trade_id'];


    if(isset($aVals['category_id']))
    {
        $category_id = $aVals['category_id'];
    }

    $sql = "INSERT INTO category SET category_name = '$category_name', parent = $parent, vcat_id = '$trade_id', status = 1";
    $sql = mysqli_query($databaseLink,$sql);
    header('location:category.php');
}

1 个答案:

答案 0 :(得分:1)

从表单中,您可以像这样分配PHP变量:

if(isset($_POST['val']['parent'])){
    $parent = $_POST['val']['parent'];
    # split the value by "-", and assign to category_id & trade_id
    list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);
}
if(isset($_POST['val']['category_name'])){
    # set the category_name variable
    $category_name = $_POST['val']['category_name'];
}
if(isset($_POST['val']['category_id'])){
    # override category_id variable
    $category_id = $_POST['val']['category_id'];
}

你可能想要收紧你的MySQL查询,但它似乎对安全漏洞持开放态度。