将select的值插入数据库

时间:2017-03-23 16:09:41

标签: php html mysql sql pdo

因此,我正在为错误报告系统编写Web应用程序。我有产品,每个产品都有一个或多个模块。添加新产品时,您需要添加模块。所以我在选择中有模块。我想在表格中添加产品的ID和模块的ID。 这是php代码

if(isset($_POST['addproduct'])) {
    $product = new product(0,
                            $_POST['productName'],
                            $_POST['productDescription'],
                            $_POST['productVersion']); 

    addProduct($product,$con,$modules);
    addproductmodule($modules,$con,$productId);
    $modules=$_POST['modules'];
}

function addproduct(Product $product, Connection $con,$modules){
    $sql = ("INSERT INTO products 
                          (productName,productDescription,productVersion) 
                    values(?,?,?)");  
    $req=$con->getPdo()->prepare($sql);
    $req->execute(array($product->getProductName(),
                        $product->getProductDescription(),
                        $product->getProductVersion()
                        )
                ); 


function addproductmodule($modules,  Connection $con, $productId){        
    $productId=$con->getPdo()->lastInsertId();   
    foreach ($modules as $value) 
    {
        $rq = "INSERT INTO product_module 
                        (productId, moduleId) 
                 VALUES (.'$productId'., .'$value'.)"; 
        $rep=$con->getPdo()->prepare($rq);
        $rep->execute(array());  
    }

这是表单的代码

<form class="form-horizontal " method="POST">
<div class="form-group">
<label for="productName" class="col-sm-3 control-label">Product name : <span class="required" style="color:red;">*</span></label>
<div class="col-sm-9">
<input type="text" id="productName" class="form-control" name="productName" value="" required="required" />
</div>
</div>
<div class="form-group">
<label for="productDescription" class="col-sm-3 control-label">Product description : <span class="required" style="color:red;">*</span></label>
<div class="col-sm-9">
<textarea id="productDescription" class="form-control" name="productDescription"  required="required" > </textarea>
</div>
</div>
<div class="form-group">
<label for="productVersion" class="col-sm-3 control-label">Product version     <span class="required" style="color:red;">*</span></label>
<div class="col-sm-9">
<input type="text" id="productVersion" class="form-control" name="productVersion" value="" required="required" />
</div>
</div>

<?php
$modules=ViewModules($con);
$count=count($modules);
$data='<div class="form-group"><label for="productModules" class="col-sm-3 control-label">Product modules : <span class="required" style="color:red;">*</span></label>
<div class="col-sm-3">
<select id="select1"class="form-control"  multiple size="'.$count.'"style="height:100px;">';

if ($count>0){
    $number=1;
    foreach ($modules as $module)
    {
        $data .='<option value="'.$module['moduleId'].'">'.$module['moduleName'].'</option>';

        $number++;
    }
} else {
    $data .='<option value="Please add modules first">Please add modules first</option>
</br>';
}

$data .= '</select><a href="#" id="add">add &gt;&gt;</a></div><div class="col-sm-3">
<select multiple="multiple" name="modules" id="select2" class="form-control" style="height:100px;"></select>
<a href="#" id="remove">&lt;&lt; remove</a>
</div></div><br>'; 
echo $data;

?>
<br>     

<center><input type="submit" class="btn btn-primary" style="width:10%;" name="addproduct" value="Save"/></center>
</form>

0 个答案:

没有答案