如何将文本框中的数据更新到db?

时间:2017-12-13 14:47:06

标签: php database sql-update

我在表格中显示数据库中的列表(项目,类别和作业),用户可以单击表格中的列表,数据将显示在文本框中。用户可以在文本框中添加,更新和删除列表。之后,单击要运行的按钮。在这个PHP中,我决定做2个添加dan更新列表的功能(Item,Category和Job)。

最后我成功更新。

这是更新的代码:

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

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

谢谢大家!

2 个答案:

答案 0 :(得分:0)

这里要介绍几件事。

问题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();

您将变量注入查询本身,否定完全使用prepare。然后尝试bindParam到不存在的占位符。最后,您正在使用相同的信息更新ENTIRE表,因为您忘记了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>代码。

答案 1 :(得分:-1)

问题在于,您的更新查询会更新整个表,因为您没有指定要更新的行

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

试试这个

$st=$con->prepare("UPDATE details SET Category='$Category', Job='$Job' where id='$id'");

您应该将隐藏输入中的行ID传递给表单,以便在提交时将其传递回脚本

<input type="hidden" name="db_id" value="1"><!-- the value should be the primary key id from your database-->

你可以像往常一样选择值

if(isset($_GET["db_id"])){$id = $_GET["db_id"];}//Kindly make sure you sanitize this input to prevent SQL injection attacks
相关问题