php - 从数据库中删除数据时出错

标签 php database post sql-delete

我正在尝试从数据库中删除一些信息,但是当我要求用户确认操作时,出现了问题。你能帮帮我吗?

如果没有解决,脚本会打印要选择的类别。然后,它要求用户删除或不删除所选类别。最后,它会删除所选择的内容。

但是最后一部分(脚本的第一部分)有一些错误,我无法理解哪里出了问题:

    <?php
//If the user confirm to delete...
    if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
//if conferma_eliminazione=1, don't delete
        if(isset($_POST['conferma_eliminazione']) and $_POST['conferma_eliminazione']=='1'){
                echo 'Eliminazione annullata';
            }else{//Delete if conferma_eliminazione=0
                while($row=$categoria){
                    $delete= "DELETE FROM categorie WHERE $row = category_id";
                    $query=mysql_query($delete);
                    echo "$row eliminato\n";
                }
            }
    }else{//ELSE, print the form to confirm action
    if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){
//Array with the chosen "categories"
        $categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
//Print the chosen categories to ask confirmation
        echo'Sicuro di voler eliminare le seguenti categoire?<br />';
        foreach($categoria as $row){
            echo "$row<br />\n";
        }
//Yes = 0, No = 1
        echo '<form method="post" action="',$_SERVER['REQUEST_URI'],'">
        <input type="radio" name="conferma_elimiazione" value="0" />Si<br />
        <input type="radio" name="conferma_eliminzione" value="1" />No<br />
        <input type="hidden" name="eliminazione_conferma" value="conferma" />';

        foreach($categoria as $row){
            echo'<input type="hidden" name="categoria[]" value="',$row,'" />',"\n",'' ;
        }   
        echo'
        <input type="submit" value="Conferma" />
        </form>';
        }else{//In the end, if nothing is settled, print the form to check the category to delete
//Select the categories from the database
        echo'<form method="post" action="',$_SERVER['REQUEST_URI'],'" />',"\n",'';
    $select = "SELECT nome,category_id FROM categorie ORDER BY category_id" ;
    $select_result=mysql_query($select) or die("Mysql Error: ".mysql_error());
    while($row = mysql_fetch_assoc($select_result)){
        echo '<input type="checkbox" name="categoria[]" value="',$row['category_id'],'">',$row['nome'],'<br />';

    }
    echo'<input type="hidden" name="eliminazione" value="delete" />
    <input type="submit" name="submit" value="Elimina" />
    </form>';
    }
    }

最佳答案

查询的问题

$delete= "DELETE FROM categorie WHERE $row = category_id";

这里

WHERE $row = category_id";

什么是category_id

你认为它有值(value)吗?

可能你想要WHERE category_id=/*这里有像$row['column_name']*/这样的东西

关于php - 从数据库中删除数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32981623/

相关文章:

php - 使用 MYSQL,从子查询中选择每第 n 行 SELECT

JavaScript:使用 Post 参数自动重定向页面

asp.net-mvc - 从下拉列表中选择的值不会从 jquery 对话框中发布

php - 确定 $_POST 的来源

java - 在延迟加载列表上调用 size() 时无法延迟初始化集合

php - PDO ... SET NAMES utf8 危险吗?

php - 大表最快的MySQL行排名

javascript - 以编程方式设置类型为 ="file"的输入 HTML 元素的值?

php - PHP中的ADOdb和PDO有什么区别?

php - 使用 PHP 的 MySQLi 登录表单 - fatal error : Uncaught Error: Call to a member function close() on boolean