【问题标题】:Having an issue deleting rows from server从服务器删除行时遇到问题
【发布时间】:2019-03-28 19:38:40
【问题描述】:

所以简而言之,当我运行代码时,我无法让我的代码从我的数据库表“tblsurvey”中删除行,但没有显示错误,并且它似乎正确执行了语句但是在检查表时我找到了行没有被删除。

<?php //set a question from the database V1.0
require '../configure.php'; //required to connect to the DB
//initialising variables
    $qID = ''; //question ID
    $dropDown = ''; //drop down box
    $startSelect = '<select name=drop1>'; //initial value of select
    $endSelect = '</select>'; //end of select
    $fullHTML = ''; //display the dropdown menus options
    $getDropdownID = ''; //on button submit grabs the UID for the questionairre
    $hiddenTag = '';

    $DB = "questonaire"; //must match Database
    $db_isFound = new mysqli(DB_SERVER, DB_USER, DB_PASS, $DB); //connecting to the database see ../configure.php for details 

    //checking if button as been pressed -- needs to redirect to the appropriate questionaire on pressed
    if (isset($_GET['submit'])){
     //initialising the selected questionaire ID  
        $getDropdownID = $_GET['drop1'];
        //display the selected questionaire
        if ($db_isFound){
            $SQL = "DELETE FROM tblsurvey WHERE ID = ?";
            $SQL_stmt = $db_isFound->prepare($SQL);
            if($SQL_stmt){
                $SQL_stmt->bind_param("s", $qID);
                $SQL_stmt->execute();
                print("question has been successfully removed from the database.");

            }else{
                print("There was a problem running your query: row not deleted");
            }

        }else{
           print("error connecting to DB: Question not deleted");
        }
    } 

正确输出并显示

print("question has been successfully removed from the database.");

但是该行并未从表中删除。

任何帮助将不胜感激。

【问题讨论】:

  • 执行后,echo $SQL_stmt-&gt;affected_rows;是什么?您确定$qID 匹配数据库中的记录吗?
  • $qID 在此代码中仅分配给空字符串
  • 在输出该消息之前,您没有检查 execute() 调用的结果(或捕获异常)。所以你实际上对查询是否成功没有任何感觉。
  • 错误检查 但如果您不介意,请将 ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 添加到脚本顶部。这将强制任何 mysqli_ 错误生成您可以在浏览器上看到的异常以及正常的 PHP 错误。
  • @user3407086 什么SQL 这个sql $SQL = "DELETE FROM tblsurvey WHERE ID = ?";

标签: php sql mysqli rowdeleting


【解决方案1】:

在这一行

$SQL_stmt->bind_param("s", $qID);

$qID 替换为$getDropdownID,因为我看不到$qID 的价值所在。

【讨论】:

  • 是的,谢谢你,我在你回复前 5 分钟意识到了这一点。谢谢你的帮助。
【解决方案2】:

感谢您查看我有一点脑子放屁,并在准备好的声明中调用了错误的 var 修复是

$SQL_stmt->bind_param("i", $getDropdownID);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    相关资源
    最近更新 更多