如何从php中的下拉列表中选择名称后获取列名并插入到db中

时间:2018-03-23 08:10:46

标签: php mysql

如何从下拉菜单列表中获取值以在db?中插入值?

我的系统有18个孔,用户可以根据需要更新孔值数据。那么如何检测选择哪个洞并更新我的数据库中的数据?

这是代码

<?php
$result     = '';
$doUpdate   = isset($_POST['updateButton_x']);
if ($doUpdate) { 

$stmtSetParams = $stmtInParams = array();
$validationOptions = array('options' => array('min_range' => 1, 'max_range' => 5));
//Define statement and parameters (Assuming dB field 'item' is the primary key).
$set = '`Hole1` = CASE `Item` ';
foreach ($_POST['Evaluate'] as $Item => $Hole1) {
    //Get input value of each table row
    if (filter_var($Hole1, FILTER_VALIDATE_INT, $validationOptions) !== false) {
        //Field is not blank
        $set .= 'WHEN ? THEN ? ';
        $stmtSetParams[] = $stmtInParams[] = $Item;
        $stmtSetParams[] = $Hole1;
    } else {
        //Field is not an integer from 1 to 5
        $result .= "Field \'Evaluate\' of item \'$Item\' with a value of \'$Hole1\' is not from 1 to 5 and skipped while saving!\\n";
    }
}
$set .= 'END';
//Define query placeholders
$placeHolders = implode(', ', array_fill(0, count($stmtInParams), '?'));
$query = <<<SQL
UPDATE `details` SET $set WHERE `Item` IN ($placeHolders)
SQL;
}
//Query the dB.
try {
$dbh = new PDO('mysql:host=localhost;dbname=gcmes', 'root');
if ($doUpdate) {
    //Update requested. Prepare and execute update query
    $stmt = $dbh->prepare($query);
    $stmt->execute(array_merge($stmtSetParams, $stmtInParams));
    $result .= 'Update Completed!';
}
//Query for en fetch (updated) table data
$stmt = $dbh->query("SELECT * FROM `details`");
$tableData = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
//A PDO exception is raised
$result = 'Error: ' . addslashes($e->getMessage());
}
//Alert results of database operations
if ($result != '') {
echo "<script>alert('$result')</script>";
}
?>
<form id="form1" name="chooseFormNameIfNeeded" method="post" action="Job.php">
<!--  attribute align is deprecated, define and use a class instead -->
<table class="info" align="center">
    <tr>
        <!--  use th instead of td for table header -->
        <!--  using <b> tag is discouraged -->
        <th width="10"><b>Item</b></th>
        <th width="100"><b>Category</b></th>
        <th width="100"><b>Job</b></th>
        <th width="30"><b>Frequency</b></th>
        <th width="10"><b>Hole</b></th>
        <th width="30"><b>Evaluate</b></th>
    </tr>
     <?php
        foreach ($tableData as $rowData) {
            //Print a table row for each of the fetched records
            echo <<<HTML
                echo <<<HTML
        <tr>
            <td>{$rowData['Item']}</td>
            <td>{$rowData['Category']}</td>
            <td>{$rowData['Job']}</td>
            <td>{$rowData['Frequency']}</td>
            <td><select name="hole">
            <option value="Hole1">1</option>
            <option value="Hole2">2</option>
            <option value="Hole3">3</option>
            <option value="Hole4">4</option>
            <option value="Hole5">5</option>
            <option value="Hole6">6</option>
            <option value="Hole7">7</option>
            <option value="Hole8">8</option>
            <option value="Hole9">9</option>
            <option value="Hole10">10</option>
            <option value="Hole11">11</option>
            <option value="Hole12">12</option>
            <option value="Hole13">13</option>
            <option value="Hole14">14</option>
            <option value="Hole15">15</option>
            <option value="Hole16">16</option>
            <option value="Hole17">17</option>
            <option value="Hole18">18</option>
            </select>
        <td>
    <!-- Assuming dB field 'item' is the primary key. -->
    <input type="number" name="Evaluate[{$rowData['Item']}]" id="Evaluate" value="{$rowData['Hole1']}"
           min=1 max=5>
</td>
</tr>
HTML;
}
?>
    </table>

用户可以选择任何孔来插入标记 enter image description here Hole是我的数据库中的一列 enter image description here  有人请帮忙,谢谢!

0 个答案:

没有答案