执行SQL语句后自动从数据库表中删除数据

C.杰克逊

我有一个reviewfile.php,它通过将数据插入数据库来读取文件并按照以下条件对其进行处理:

  1. 结果重复
  2. 没有评论的结果
  3. 源或目标为“任何”的数据结果。

我有每个条件的SQL语句。

在打印SQL语句的结果之后,我想删除数据库表的所有数据,以便在上载新文件时可以重新插入数据。

PS:我不想丢桌子。

我可以寻求帮助吗:)

下面是我的代码

if(isset($_POST['reviewFile'])){
    if (isset($_POST['fileSelected'])){

        $SelFile = $_POST['fileSelected'];


        $query=$con->prepare("SELECT * from file where file_name = '$SelFile'");
        $query->bind_result($file_id, $uid, $file_name, $file_size);
        $query->execute();
        $query->fetch();
        $query->close();

        $path = 'C:/xampp/htdocs/fwrule/fw/fileuploads/';
        $file_to_get = $path.$SelFile;

        $_SESSION['tmpfname'] = $file_to_get;
        $_SESSION['excelReader'] = PHPExcel_IOFactory::createReaderForFile($_SESSION['tmpfname']);
        $_SESSION['excelObj'] = $_SESSION['excelReader']->load($_SESSION['tmpfname']);

        $_SESSION['worksheet'] = $_SESSION['excelObj']->getSheet(0);
        $_SESSION['lastRow'] =  $_SESSION['worksheet']->getHighestRow();
        $_SESSION['highestColumn'] = $_SESSION['worksheet']->getHighestColumn();

        //loop through each row of the worksheet in turn
        for ($_SESSION['row'] = 3; $_SESSION['row'] <= $_SESSION['lastRow']; ++$_SESSION['row']) {

            //read a row of data into an array
            $rowData = $_SESSION['worksheet']->rangeToArray('A'.$_SESSION['row']. ':'.$_SESSION['highestColumn'] . $_SESSION['lastRow'], NULL, TRUE, FALSE);

            //insert rows into database
            $InsertData = $con->prepare("INSERT INTO file_data(id, file_id, no, name, source, destination, hit_counts, service, action, track, install_on, time, comment)
            VALUES('', '$file_id', '".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."')");


                $InsertData->execute();
                $InsertData->store_result();


            /*if (!$InsertData = $con->prepare("INSERT INTO file_data(id, file_id, no, name, source, destination, hit_counts, service, action, track, install_on, time, comment)
                    VALUES('', '$file_id', '".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."')"))
            {
                ?>
                <html>
                    <script>
                        window.alert("Error message:: %s\n", $con->error");
                    </script>
                </html>
                <?php
            }*/
        }


        if ($DuplicatedRows = $con->prepare("SELECT id
                      from file_data a
                      join ( SELECT source, destination, hit_counts, service
                               from file_data
                              group by source, destination, hit_counts, service
                             having count(*) > 1 ) b
                        on a.source = b.source
                       and a.destination = b.destination
                        and a.hit_counts = b.hit_counts
                        and a.service = b.service")){

                            $DuplicatedRows->bind_result($id);
                            $DuplicatedRows->execute();
                            $DuplicatedRows->store_result();

                            while($DuplicatedRows->fetch()){

                                $rowNumforDuplicates = ($id+2);
                                $info1 = "Duplicated Rules";

                                echo "<tr>";
                                echo "<td>".$rowNumforDuplicates."</td>";
                                echo "<td>".$info1."</td>";
                                //echo "<td>".$source."".$destination."".$hit_counts."".$service."</td>";
                                echo "</tr>";

                            }

                            $DuplicatedRows->free_result();
                            $DuplicatedRows->close();

                }
                else{

                    ?>
                    <html>
                        <script>
                            window.alert("Error message:: %s\n", $con->error");
                        </script>
                    </html>
                    <?php
                }

                if ($checkforAny = $con->prepare("SELECT id FROM file_data WHERE source collate latin1_swedish_ci = 'any' or destination collate latin1_swedish_ci ='any' "))
                {

                    $checkforAny->bind_result($id);
                    $checkforAny->execute();
                    $checkforAny->store_result();

                    while($checkforAny->fetch()){

                        //row number in excel as the file is reviewed from line 3 onwards, hence 2 is added to the ID value
                        $rowNumforAny = ($id+2);

                        echo "<br>";
                        echo "The one with Any: $rowNumforAny";
                    }

                    $checkforAny->free_result();
                    $checkforAny->close();

                }
                else{

                    ?>
                    <html>
                        <script>
                            window.alert("Error message:: %s\n", $con->error");
                        </script>
                    </html>
                    <?php
                }

                if ($checkforComments = $con->prepare("SELECT id FROM file_data WHERE comment = '' "))
                {
                    $checkforComments->bind_result($id);
                    $checkforComments->execute();
                    $checkforComments->store_result();

                    while($checkforComments->fetch()){

                        $rowNumforComments = ($id+2);

                        echo "<br>";
                        echo "The one with with no comments:$rowNumforComments";

                    }
                    echo "</table>";

                    $checkforComments->free_result();
                    $checkforComments->close();
                }
                else {

                    ?>
                    <html>
                        <script>
                            window.alert("Error message:: %s\n", $con->error");
                        </script>
                    </html>
                    <?php
                }

                if ($checkforHitcounts = $con->prepare("SELECT id FROM file_data ORDER BY hit_counts ASC|DESC")){

                                $checkforHitcounts->bind_result($id, $hit_counts);
                                $checkforHitcounts->execute();
                                $checkforHitcounts->store_result();

                                while($checkforHitcounts->fetch()){

                                    echo "<br>";
                                    echo "hitcounts:$id";

                                }
                                echo "</table>";

                                $checkforHitcounts->free_result();
                                $checkforHitcounts->close();

                }
                else{

                    ?>
                    <html>
                        <script>
                            window.alert("Error message:: %s\n", $con->error");
                        </script>
                    </html>
                    <?php
                }


                if ($DuplicatedRows == true && $checkforAny == true && $checkforComments == true)
                {
                    $delAllData = $con->prepare("DELETE * from file_data");

                    $delAllData->bind_result();
                    if ($delAllData->execute())
                    {
                        $delAllData->fetch();
                        echo "hi"; 
                    }
                    $delAllData->close();
                }
    }
}

除我要从数据库表中删除数据的部分外,这些代码都工作正常。

这是不起作用的部分:

if ($DuplicatedRows == true && $checkforAny == true && $checkforComments == true)
                {
                    $delAllData = $con->prepare("DELETE * from file_data");

                    $delAllData->bind_result();
                    if ($delAllData->execute())
                    {
                        $delAllData->fetch();
                        echo "hi"; 
                    }
                    $delAllData->close();
                }
擎天柱犯罪

您运行的用于删除内容的查询是错误的。

查询应该是 DELETE FROM file_data

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如果数据库中没有日期列,则在特定日期后自动删除数据库

来自分类Dev

在Appharbor中删除数据库SQL Server

来自分类Dev

试图从 sql 文件中删除数据库名称

来自分类Dev

SQL Powershell删除数据库

来自分类常见问题

在phpMyAdmin中删除数据库

来自分类Dev

删除数据库中的行

来自分类Dev

删除数据库中的图像

来自分类Dev

删除数据库中的行

来自分类Dev

自动从DynamoDb表中删除数据

来自分类Dev

删除数据库条目后重新加载html表数据

来自分类Dev

在插入语句后从SQL数据库中检索自动增量ID

来自分类Dev

删除数据库后,SQL Server不会删除所有数据库文件吗?

来自分类Dev

删除数据库

来自分类Dev

Slick可以从模型中自动在数据库中创建表(生成SQL并执行)吗?

来自分类Dev

SQL LocalDB-删除数据库文件后无法删除

来自分类Dev

jQuery单击div后从数据库中删除数据

来自分类Dev

是否可以同时从Sqlite数据库中的所有表中删除数据

来自分类Dev

Docker-compose stop从数据库中的表中删除数据

来自分类Dev

如何删除数据库中MySQL表中的相关数据?

来自分类Dev

是否可以同时从Sqlite数据库中的所有表中删除数据

来自分类Dev

删除数据库后,表“ django_migrations”已存在

来自分类Dev

如何从条件中删除数据库中的数据?

来自分类Dev

从通过两个表绑定的数据网格中删除数据库条目

来自分类Dev

某些不活动后EF删除数据库

来自分类Dev

删除数据库条目后刷新列表视图

来自分类Dev

显示和删除数据库中的数据

来自分类Dev

客观地从Oracle数据库中删除数据

来自分类Dev

从Postgres数据库中删除数据

来自分类Dev

使用href从数据库中删除数据行

Related 相关文章

热门标签

归档