更新文本框中表中的值但未更新 db 中的值? [英] Update the value from table in textbox BUT not updated the value in db?

查看:19
本文介绍了更新文本框中表中的值但未更新 db 中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经列出了所有数据(项目、类别、工作、孔(孔正在评估标记))并在文本框中显示孔(标记).

I have listed all the data(Item, Category, Job, Hole(Hole is evaluating marks)) and I display the Hole(mark) in textbox filed.

我想在用户更改后更新 Hole(marks).

I want to update the Hole(marks) after user change.

我使用php列出所有数据

I list all the data using 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 代码

The html code i just use to display button

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

问题是将警报消息成功更新但值未在我的数据库中更新.为什么?有什么问题?

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

this is my interface i want update the marks in textbox filed

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

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

i just add the code after <td>{$rowData['Frequency']}</td> (dn Fer answer)

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

推荐答案

请记住以下几点:

  • 我和你的环境不一样,所以它可能不起作用一个.
  • 不鼓励在数据库 fieldNames 和 arrayKeys 等中使用空格.一世更喜欢使用lowerCamelCase,检查dB fieldNames是否与代码匹配下面!
  • 阅读我在代码中添加的注释.
  • 我没有考虑 psr 编码、值验证或安全性(sql 注入)等.代码是指导你的,你应该自己考虑这些事情.

希望能让你更接近你的目标......

Hopefully getting you closer to your goals...

更新:每行的 Evaluate 字段的值现在被验证为 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>

这篇关于更新文本框中表中的值但未更新 db 中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆