更新文本框中表的值但未更新db中的值?

时间:2017-12-15 08:56:56

标签: php textbox updates

我列出了所有数据(项目,类别,作业,孔(评估标记孔)),并在文本框中显示孔(标记)。

我想在用户更换后更新孔(标记)。

我使用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>

问题是警报消息是否成功更新但是我的数据库中的值没有更新。为什么?有什么问题?

enter image description here 这是我的界面 我想更新文本框中的标记

我需要更改孔作为选择,让用户选择只需要更新的孔,我设置的孔有一个下拉菜单列表。如何判断哪个洞?

enter image description here

我只是在<td>{$rowData['Frequency']}</td>之后添加代码(dn Fer答案)

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

1 个答案:

答案 0 :(得分:0)

请记住以下几点:

  • 我没有和你一样的环境,所以它可能不起作用 在一个。
  • 不鼓励数据库fieldNames和arrayKeys等中的空格。一世 更喜欢使用lowerCamelCase,检查dB fieldNames是否与代码匹配 下面<!/ LI>
  • 阅读我在代码中的评论。
  • 我没有考虑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>