更新从表中的值在文本框中但不能更新分贝值?(Update the value from table

2019-10-31 06:13发布

我列出的所有数据(项目,类别,工作,孔(孔正在评估引号))和我提起显示在文本框中的孔(标记)。

我想用户变化后更新孔(引号)。

我列出使用PHP的所有数据

<?php
try{
$con = new PDO("mysql:host=localhost;dbname=gcmes", "root", "");
$sql = $con->query("SELECT * FROM details");

echo"<table class='info' align='center'>";
echo"<tr><td width='10'><b>No</b></td>
<td width='30'><b>Category</b></td>
<td width='50'><b>Job</b></td>
<td width='40'><b>Evaluate</b></td><tr>";
foreach($sql as $row) {
$Item = $row["Item"];
$Category = $row["Category"];
$Job = $row["Job"];
$Evaluate = $row["Hole 1"];

echo'
    <tr>
        <td>' . $Item . '</td>
        <td>' . $Category . '</td>
        <td>' . $Job . '</td>
        <td><input type="input" name="Evaluate" id="Evaluate" value="' . $Evaluate . '">
        </td>
    </tr>
';
}
echo"</table></form>";

if(isset($_POST['Update_btn'])){
$Evaluate = $_POST["Hole 1"];

if(empty(Evaluate)){
echo "<script type='text/javascript'>alert('Please fill in the required fields to update!')</script>";
}

else{
$insert=$con->prepare("UPDATE details SET Evaluate=:Hole 1 WHERE Item=:Item");
$insert->bindParam(':Hole1',$Evaluate);
$insert->bindParam(":Item",$Item);
$insert->execute();

echo "<script type='text/javascript'>alert('Successful Updated ! ');
window.location.href = 'Hole 1.php';
</script>";
}//else
}//if add button
}//try
catch(PDOException $e)
{
echo "error".$e->getMessage();
}
?>

HTML代码,我只是用它来显示按钮

<form id="form1" name="Hole 1" method="post" action="Hole 1.php">
<input name="Update_btn" type="image" id="Update_btn" onmouseover="this.src='UpdateO.png'" onmouseout="this.src='UpdateD.png'" value="submit" src="UpdateD.png" alt="submit Button" align="right"> 
</form>

问题是,将提醒信息成功更新,但该值不是在我的数据库更新。 为什么? 问题是什么?

这是我的界面我想更新提交的文本框标记

我需要改变孔作为选择给用户选择哪些孔仅需要更新,我设置的孔有一个下拉菜单列表。 如何dectect哪个孔?

我只添加代码后<td>{$rowData['Frequency']}</td> (DN费尔回答)

<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>

Answer 1:

请记住以下几点:

  • 我没有相同的环境是你的,所以它可能无法正常工作一对一。
  • 在数据库字段名和arrayKeys等空间都望而却步。 我更喜欢使用lowerCamelCase,检查是否分贝字段名下面的代码匹配!
  • 阅读我放置在代码中的注释。
  • 我没拿PSR编码,值验证或安全(SQL注入)等考虑。 该代码是引导你,你应该自己把这些东西考虑。

希望让你更接近自己的目标......

更新:各行的评估字段的值现在被确认为从1到5范围内的整数。

<?php

//Initialize variables
$result     = '';
$doUpdate   = isset($_POST['updateButton_x']);

//Because button type is 'image', we get parameters buttonName_x and buttonName_y
//from the browsers POST request when the form is sent.
if ($doUpdate) { 
    //Update button pressed.
    //Initialize variables
    $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="test.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>No</b></th>
            <th width="30"><b>Category</b></th>
            <th width="50"><b>Job</b></th>
            <th width="40"><b>Evaluate</b></th>
        </tr>
        <?php
            foreach ($tableData as $rowData) {
                //Print a table row for each of the fetched records
                echo <<<HTML
<tr>
    <td>{$rowData['item']}</td>
    <td>{$rowData['category']}</td>
    <td>{$rowData['job']}</td>
    <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>
    <!--  Attribute align is deprecated, define and use a class instead -->
    <!--  Value attribute should not be specified -->
    <input name="updateButton" type="image" id="Update_btn" src="http://via.placeholder.com/100x50/0000FF?text=Update" 
           alt="submit Button" class="align-right" 
           onmouseover="this.src='http://via.placeholder.com/100x50/00FF00?text=Update'"
           onmouseout="this.src='http://via.placeholder.com/100x50/0000FF?text=Update'"
    >
</form>


文章来源: Update the value from table in textbox BUT not updated the value in db?