如何使用 PHP(和 HTML)从 SQLite 中选择、插入、更新和删除数据

Posted

技术标签:

【中文标题】如何使用 PHP(和 HTML)从 SQLite 中选择、插入、更新和删除数据【英文标题】:How to SELECT, INSERT, UPDATE and DELETE data from SQLite with PHP (and HTML) 【发布时间】:2016-06-23 08:46:23 【问题描述】:

我确实有一个SQLite Web Interface,它应该从 SQLite 表中选择、插入、更新和删除数据。不幸的是,似乎只有 SELECT 查询有效。 我无法插入、更新或删除。

sshTunnel.sqlite --> mydata

CREATE TABLE "mydata" ("ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "SSH_USER" VARCHAR, "SSH_IP" VARCHAR, "SSH_PORT" VARCHAR, "SSH_PW" VARCHAR, "SSH_KEYFILE" VARCHAR, "TUNNEL_LOCAL_INTERFACE" VARCHAR, "TUNNEL_LOCAL_PORT" VARCHAR, "TUNNEL_REMOTE_IP" VARCHAR, "TUNNEL_REMOTE_PORT" VARCHAR, "DESCRIPTION" VARCHAR)

这是我的代码 sn-ps...

index.php

<script>
$(document).ready(function()

    function fetch_data()
    
        $.ajax(
            url:"select.php",
            method:"POST",
            success:function(data)
            
                $('#live_data').html(data);
            
        );
    
    fetch_data();

    $(document).on('click', '#btn_add', function()
    
        var SSH_USER = $('#SSH_USER').text();
        var SSH_IP = $('#SSH_IP').text();
        var SSH_PORT = $('#SSH_PORT').text();
        var SSH_PW = $('#SSH_PW').text();
        var SSH_KEYFILE = $('#SSH_KEYFILE').text();
        var TUNNEL_LOCAL_INTERFACE = $('#TUNNEL_LOCAL_INTERFACE').text();
        var TUNNEL_LOCAL_PORT = $('#TUNNEL_LOCAL_PORT').text();
        var TUNNEL_REMOTE_IP = $('#TUNNEL_REMOTE_IP').text();
        var TUNNEL_REMOTE_PORT = $('#TUNNEL_REMOTE_PORT').text();
        var DESCRIPTION = $('#DESCRIPTION').text();

        $.ajax(
            url:"insert.php",
            method:"POST",
            data:SSH_USER:SSH_USER, SSH_IP:SSH_IP, SSH_PORT:SSH_PORT, SSH_PW:SSH_PW, SSH_KEYFILE:SSH_KEYFILE, TUNNEL_LOCAL_INTERFACE:TUNNEL_LOCAL_INTERFACE, TUNNEL_LOCAL_PORT:TUNNEL_LOCAL_PORT, TUNNEL_REMOTE_IP:TUNNEL_REMOTE_IP, TUNNEL_REMOTE_PORT:TUNNEL_REMOTE_PORT, DESCRIPTION:DESCRIPTION,
            dataType:"text",
            success:function(date)
            
                alert(data);
                fetch_data();
            
        );
    );

    $(document).on('click', '.btn_edit', function()
    
        var SSH_USER = $(this).data("id1");
        var SSH_IP = $(this).data("id2");
        var SSH_PORT = $(this).data("id3");
        var SSH_PW = $(this).data("id4");
        var SSH_KEYFILE = $(this).data("id5");
        var TUNNEL_LOCAL_INTERFACE = $(this).data("id6");
        var TUNNEL_LOCAL_PORT = $(this).data("id7");
        var TUNNEL_REMOTE_IP = $(this).data("id8");
        var TUNNEL_REMOTE_PORT = $(this).data("id9");
        var DESCRIPTION = $(this).data("id10");

        if(confirm("Are you sure you want to edit this?"))
        
            $.ajax(
                url:"edit.php",
                method:"POST",
                data:SSH_USER:SSH_USER, SSH_IP:SSH_IP, SSH_PORT:SSH_PORT, SSH_PW:SSH_PW, SSH_KEYFILE:SSH_KEYFILE, TUNNEL_LOCAL_INTERFACE:TUNNEL_LOCAL_INTERFACE, TUNNEL_LOCAL_PORT:TUNNEL_LOCAL_PORT, TUNNEL_REMOTE_IP:TUNNEL_REMOTE_IP, TUNNEL_REMOTE_PORT:TUNNEL_REMOTE_PORT, DESCRIPTION:DESCRIPTION,
                dataType:"text",
                success:function(date)
                
                    alert(data);
                    fetch_data();
                
            );
        
    );

    $(document).on('click', '.btn_delete', function()
    
        var id = $(this).data("id12");
        if(confirm("Are you sure you want to delete this?"))
        
            $.ajax(
                url:"delete.php",
                method:"POST",
                data:id:id,
                dataType:"text",
                success:function(date)
                
                    alert(data);
                    fetch_data();
                
            );
        
    );
);
</script>

...

<div id="live_data"></div>

select.php

<?php
$db = new SQLite3('sshTunnel.sqlite');
$results = $db->query('SELECT * FROM mydata');
$output = '';
$output .= '<div class="table-responsive">
                <table class="table table-bordered">
                    <tr>
                        <th>ID</th>
                        <th>SSH_USER</th>
                        <th>SSH_IP</th>
                        <th>SSH_PORT</th>
                        <th>SSH_PW</th>
                        <th>SSH_KEYFILE</th>
                        <th>TUNNEL_LOCAL_INTERFACE</th>
                        <th>TUNNEL_LOCAL_PORT</th>
                        <th>TUNNEL_REMOTE_IP</th>
                        <th>TUNNEL_REMOTE_PORT</th>
                        <th>DESCRIPTION</th>
                        <th>Editieren</th>
                        <th>Löschen</th>
                    </tr>';
while ($row = $results->fetchArray())

    $output .= '<tr>
                    <td>'.$row["ID"].'</td>
                    <td class="SSH_USER" data-id1="'.$row["ID"].'" contenteditable="true">'.$row["SSH_USER"].'</td>
                    <td class="SSH_IP" data-id2="'.$row["ID"].'" contenteditable="true">'.$row["SSH_IP"].'</td>
                    <td class="SSH_PORT" data-id3="'.$row["ID"].'" contenteditable="true">'.$row["SSH_PORT"].'</td>
                    <td class="SSH_PW" data-id4="'.$row["ID"].'" contenteditable="true">'.$row["SSH_PW"].'</td>
                    <td class="SSH_KEYFILE" data-id5="'.$row["ID"].'" contenteditable="true">'.$row["SSH_KEYFILE"].'</td>
                    <td class="TUNNEL_LOCAL_INTERFACE" data-id6="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_LOCAL_INTERFACE"].'</td>
                    <td class="TUNNEL_LOCAL_PORT" data-id7="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_LOCAL_PORT"].'</td>
                    <td class="TUNNEL_REMOTE_IP" data-id8="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_REMOTE_IP"].'</td>
                    <td class="TUNNEL_REMOTE_PORT" data-id9="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_REMOTE_PORT"].'</td>
                    <td class="DESCRIPTION" data-id10="'.$row["ID"].'" contenteditable="true">'.$row["DESCRIPTION"].'</td>
                    <td><button type="button" name="edit_btn" data-id11="'.$row["ID"].'" class="btn btn-xs btn-warning btn-block btn_edit">Editieren</button></td>
                    <td><button type="button" name="delete_btn" data-id12="'.$row["ID"].'" class="btn btn-xs btn-danger btn-block btn_delete">Löschen</button></td>
                </tr>';

$output .= '<tr>
                <td></td>
                <td id="SSH_USER" contenteditable="true"</td>
                <td id="SSH_IP" contenteditable="true"</td>
                <td id="SSH_PORT" contenteditable="true"</td>
                <td id="SSH_PW" contenteditable="true"</td>
                <td id="SSH_KEYFILE" contenteditable="true"</td>
                <td id="TUNNEL_LOCAL_INTERFACE" contenteditable="true"</td>
                <td id="TUNNEL_LOCAL_PORT" contenteditable="true"</td>
                <td id="TUNNEL_REMOTE_IP" contenteditable="true"</td>
                <td id="TUNNEL_REMOTE_PORT" contenteditable="true"</td>
                <td id="DESCRIPTION" contenteditable="true"</td>
                <td colspan="13"><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-block btn-success">Hinzufügen</button></td>
            </tr>
        </table>
    </div>';
echo $output;
$db->close();
?>

插入.php

<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec('INSERT INTO mydata(  SSH_USER, 
                                SSH_IP, 
                                SSH_PORT, 
                                SSH_PW, 
                                SSH_KEYFILE, 
                                TUNNEL_LOCAL_INTERFACE, 
                                TUNNEL_LOCAL_PORT, 
                                TUNNEL_REMOTE_IP, 
                                TUNNEL_REMOTE_PORT, 
                                DESCRIPTION) 
                                VALUES( '".$_POST["SSH_USER"]."', 
                                                '".$_POST["SSH_IP"]."', 
                                                '".$_POST["SSH_PORT"]."', 
                                                '".$_POST["SSH_PW"]."', 
                                                '".$_POST["SSH_KEYFILE"]."', 
                                                '".$_POST["TUNNEL_LOCAL_INTERFACE"]."', 
                                                '".$_POST["TUNNEL_LOCAL_INTERFACE"]."', 
                                                '".$_POST["TUNNEL_LOCAL_PORT"]."', 
                                                '".$_POST["TUNNEL_REMOTE_IP"]."', 
                                                '".$_POST["TUNNEL_REMOTE_PORT"]."', 
                                                '".$_POST["DESCRIPTION"]."')');
$db->close();
?>

edit.php

<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec("UPDATE mydataSET     SSH_USER='".$_POST["SSH_USER"]."', 
                                SSH_IP='".$_POST["SSH_IP"]."', 
                                SSH_PORT='".$_POST["SSH_PORT"]."', 
                                SSH_PW='".$_POST["SSH_PW"]."', 
                                SSH_KEYFILE='".$_POST["SSH_KEYFILE"]."', 
                        TUNNEL_LOCAL_INTERFACE='".$_POST["TUNNEL_LOCAL_INTERFACE"]."', 
                            TUNNEL_LOCAL_PORT='".$_POST["TUNNEL_LOCAL_PORT"]."', 
                                TUNNEL_REMOTE_IP='".$_POST["TUNNEL_REMOTE_IP"]."', 
                                TUNNEL_REMOTE_PORT='".$_POST["TUNNEL_REMOTE_PORT"]."', 
                                DESCRIPTION='".$_POST["DESCRIPTION"]."' 
                                WHERE ID='".$_POST["ID"]."'");
$db->close();
?>

删除.php

<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec('DELETE FROM mydata WHERE ID='".$_POST["ID"]."'');
$db->close();
?>

【问题讨论】:

【参考方案1】:

您确定您的脚本正在编译和运行吗? 我在 insert.php 中发现了一个奇怪的引号用法:你用单引号打开字符串,然后它们使用双引号来终止和连接......对于 delete.php 也是如此。

另外,请确保您在查询中放置的所有数据都“添加了斜杠”。如果单引号输入任何内容,您的所有查询都将失败,当然,安全也很重要。

-- 塞尔吉奥

【讨论】:

搜索“如何与 php post 连接”成功了。感谢您的帮助,塞尔吉奥!

以上是关于如何使用 PHP(和 HTML)从 SQLite 中选择、插入、更新和删除数据的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SQLite 和 PHP 将图像保存到数据库?

如何使用 PHP 搜索包含多个单词的值的数据库?

如何使用 sqlite3 支持编译 php

如何使用 php 更新 SQLite 表中的数据?

如何使用本机 sqlite 库而不是 QSql 从 sqlite 插入和检索 QString

如何在 SQLite 中为 DELETE 启用 LIMIT?