php - 如何将文本框中的数据更新到数据库中?

标签 php database sql-update

我在表中显示数据库中的列表(项目、类别和工作),用户可以单击表上的列表,数据将显示在文本框中。用户可以在文本框中添加、更新和删除列表。之后单击要运行的按钮。在这个 php 中,我决定执行 2 个函数来添加和更新列表(项目、类别和工作)。

终于更新成功

这是更新后的代码:

首先,我在表格上显示了 3 个文本框。用户可以在文本框中添加新列表(项目、类别、工作)。之后,用户还可以单击另一个列表,我在顶部位置显示的 3 个文本框中列出了我数据库中的所有数据,并更改要更新的文本框上的列表(类别和工作)。

<form id="form1" name="Checklist" method="post" action="Checklist2.php">
    <table width="305" height="116" align="center" border="0">
        <tr>
            <td width="37%">Item : <br></td>
            <td width="63%"><input type="text" name="Item" id="Item"></td>
        </tr>
        <tr>
            <td>Category : </td>
            <td>
                <input type="text" name="Category" id="Category">
            </td>
        </tr>
        <tr>
            <td>Job : </td>
            <td> 
                <input type="text" name="Job" id="Job">
            </td>
         </tr>
     </table>
      <div align="center">
        <input type="image" value="submit" src="AddD.png" alt="submit Button" onmouseover="this.src='AddO.png'" onmouseout="this.src='AddD.png'" name="Add_btn" id="Add_btn">
        <input type="image" value="submit" src="UpdateD.png" alt="submit Button" onmouseover="this.src='UpdateO.png'" onmouseout="this.src='UpdateD.png'" name="Update_btn" id="Update_btn">
&nbsp;
        <a href="DeleteChecklist2.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','Delete1O.png',1)">
        <img src="Delete1D.png" name="Image3" width="145px" height="50px" border="0" name="Delete_btn" id="Delete_btn">
    </a>
     </div>
 </form>

//This is the PHP code of add and delete button
<?php
    try {
        $con = new PDO("mysql:host=localhost;dbname=gcmes", "root", "");
        if(isset($_POST['Add_btn'])) {
            $Item = $_POST["Item"];
            $Category = $_POST["Category"];
            $Job = $_POST["Job"];

            if(empty($Item)||empty($Category)||empty($Job)) {
                echo "<script type='text/javascript'>alert('Please fill in the required fields to add!')</script>";
            }
            else {
                $insert=$con->prepare("INSERT INTO details(Item,Category,Job) VALUES(:Item,:Category,:Job)");
                $insert->bindParam(':Item',$Item);
                $insert->bindParam(':Category',$Category);
                $insert->bindParam(':Job',$Job);
                $insert->execute();
                echo "<script type='text/javascript'>alert('Successful Added ! '); window.location.href = 'Checklist2.php';</script>";
            }//else
        }//if addbutton

        if(isset($_GET['Update_btn'])) {
            $Item = $_GET['Item'];
            $Category = $_GET['Category'];
            $Job = $_GET['Job'];

            if(empty($Category)||empty($Job)) {
                echo "<script type='text/javascript'>alert('Please fill in the required fields to update!')</script>";
            }
            else {
                $st=$con->prepare("UPDATE details SET Category = :Category, Job = :Job WHERE Item = :Item");
                $st->bindParam(":Category",$Category);
                $st->bindParam(":Job",$Job);
                $st->bindParam(":Item",$Item);
                $st->execute();
            }//else
        }//if updatebutton
    }//try
    catch(PDOException $e) {
        echo "error".$e->getMessage();
    }
?>


//This is the table list all the data from database
<table id="table" border="0" align="center">
    <tr>
        <th>No</th>
        <th>Category</th>
        <th>Job</th>
    </tr>

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

        foreach($sql as $row) {
            $Item = $row["Item"];
            $Category = $row["Category"];
            $Job = $row["Job"];

            echo'
                <tr>
                    <td>' . $Item . '</td>
                    <td>' . $Category . '</td>
                    <td>' . $Job . '</td>
                </tr>
            ';
        }
        echo"</table>";
    }
    catch(PDOException $e) {
        echo "error".$e->getMessage();
    }
?>

这是一个脚本,当用户点击表中的数据时(上面的代码)将显示在文本框中:

<script>
    var table = document.getElementById('table');
        for(var i = 1; i < table.rows.length; i++)
        {
            table.rows[i].onclick = function()
            {
                //rIndex = this.rowIndex;
                document.getElementById("Item").value = this.cells[0].innerHTML;
                document.getElementById("Category").value = this.cells[1].innerHTML;
                document.getElementById("Job").value = this.cells[2].innerHTML;
            };
        }
</script>

谢谢大家!

最佳答案

这里要介绍一些事情。

问题 1:

你的 INSERT设置不正确。你有:

$insert=$con->prepare("INSERT INTO details(Item,Category,Job) VALUES(:Item,:Category,:Job)");
$insert->bindParam('Item',$Item);
$insert->bindParam('Category',$Category);
$insert->bindParam('Job',$Job);
$insert->execute();

应该是这样的(注意bindParam中添加的:):

$insert=$con->prepare("INSERT INTO details(Item,Category,Job) 
                       VALUES(:Item,:Category,:Job)");
$insert->bindParam(':Item',$Item);
$insert->bindParam(':Category',$Category);
$insert->bindParam(':Job',$Job);
$insert->execute();

问题 2:

第二个是你的UPDATE都是不正确的,并且没有正确使用准备。你有这个:

$st=$con->prepare("UPDATE details SET Category='$Category', Job='$Job'");
$st->bindParam(1,$Category);
$st->bindParam(2,$Job);
$st->execute();

您将变量注入(inject)查询本身的位置,完全否定了 prepare 的使用。然后您尝试将 bindParam 绑定(bind)到不存在的占位符。最后,您正在使用相同的信息更新整个表,因为您忘记了 WHERE条款。

所以试试这个:

$st=$con->prepare("UPDATE details SET Category = :Category, Job = :Job 
                   WHERE Item = :Item");
$st->bindParam(":Category",$Category);
$st->bindParam(":Job",$Job);
$st->bindParam(":Item",$Item);
$st->execute();

问题 3:

最后,正如我在评论中提到的...您可以将 Item 作为隐藏的表单元素传递,这样用户就无法轻易更改它:

<tr>
    <td width="37%">Item : <br></td>
    <td width="63%">
        <input type="hidden" name="Item" id="Item">
        <span id="ItemDisplay"><!-- Item will be visible here --></span>
    </td>
</tr>    

// js addition:
    document.getElementById("Item").value = this.cells[0].innerHTML;
    document.getElementById("ItemDisplay").innerHTML = this.cells[0].innerHTML;

问题 4:

在您获得更新按钮的 html 之前,您有一个关闭 form标记,它破坏了您的 html 表单提交参数:

</form><!-- THIS shouldnt be here -->
<?PHP
     // big block of php
?>
<div align="center">
    <input type="image" value="submit" src="AddD.png" alt="submit Button" onmouseover="this.src='AddO.png'" onmouseout="this.src='AddD.png'" name="Add_btn" id="Add_btn">
    <input type="image" value="submit" src="UpdateD.png" alt="submit Button" onmouseover="this.src='UpdateO.png'" onmouseout="this.src='UpdateD.png'" name="Update_btn" id="Update_btn">
</div>
</form><!-- this one is the CORRECT one to keep -->

删除早期</form>标签。

关于php - 如何将文本框中的数据更新到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795832/

相关文章:

sql - 是否可以通过多个字段连接表?

sql - 更新可计算字段

MySql 重新编号行结果 NULL 值

mysql - 改进 MySQL 中的更新

php - 使用 SQL 的投票/排名逻辑

php - 在 PHP 应用程序中存储系统配置的最佳方式(编码方式)是什么?

php - Laravel 在 View 中检测路由组

php - 使用 PHP/MySQL 的可扩展聊天室?

android - 在 android 中查询 sqlite(插入查询)

database - 使用继承来解决设计问题