【问题标题】:Something wrong while deleting data from DB从数据库中删除数据时出现问题
【发布时间】:2015-10-06 23:54:35
【问题描述】:

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

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

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

    <?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>';
    }
    }

【问题讨论】:

  • 您能否减少代码量或将其分开以使其更具可读性? “错误”也是一个广义的术语,请定义它。
  • 我只知道 mysql_query 和其他已弃用,但如果我发现错误,我会稍后检查它们。对不起^^"

标签: php database post sql-delete


【解决方案1】:

查询的问题

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

这里

WHERE $row = category_id";

category_id 是什么?

你有这个价值吗?

可能你想要WHERE category_id=/*something here like $row['column_name']*/

【讨论】:

  • category_id 是我的数据库中保存类别 ID 的列。但是,问题是当用户接受删除所选类别时。脚本不执行这一段: //if conferma_eliminazione=1, don't delete if(isset($_POST['conferma_eliminazione']) and $_POST['conferma_eliminazione']=='1'){等等...}
  • 那么查询应该是WHERE category_id=/*some value*/
【解决方案2】:

感谢大家的帮助,但是我已经通过这种方式解决了我的问题(脚本没有转换整数类型中“conferma_eliminazione”的值):

<h2>Modifica o Elimina Categoria </h2>
<?php
if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
        $categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
        $a=(int)$_POST['eliminazione'];
                if($a=='1'){
            echo 'Eliminazione annullata';
            echo "\n";
        }elseif($a=='0'){
            foreach($categoria as $row){
                $delete= "DELETE FROM categorie WHERE $row = category_id";
                $query=mysql_query($delete);
                echo "$row eliminato\n";
            }
        }
}else{
if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){

    $categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
    echo'Sicuro di voler eliminare le seguenti categoire?<br />';
    foreach($categoria as $row){
        echo '',$row,'<br />',"\n";
    }
    echo '<form method="post" name="form_eliminazione_categoria" action="',$_SERVER['REQUEST_URI'],'">',"\n",'
    <input type="radio" name="eliminazione" value="0" />Si<br />' ,"\n",'
    <input type="radio" name="eliminazione" value="1" />No<br />',"\n",'
    <input type="hidden" name="eliminazione_conferma" value="conferma" />',"\n",'';
    foreach($categoria as $row){
        echo '<input type="hidden" name="categoria[]" value="',$row,'" /><br />',"\n",'';
    }
    echo'
    <input type="submit" value="Conferma" />
    </form>';
    }else{
    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 "\n";
}
echo'<input type="hidden" name="eliminazione" value="delete" />',"\n",'
<input type="submit" name="submit" value="Elimina" />',"\n",'
</form>';
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 2019-05-04
    • 1970-01-01
    • 2021-06-02
    • 2012-04-15
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多