-1

我是这方面的新手,我最近才研究代码......我很难理解为什么这不起作用:

我在搜索结果中添加了一个按钮,因此它会删除某些数据字段,但它不会这样做。我在这里错过了什么?

<?php //deleting

isset($_GET['action']) ? $action=$_GET['action'] : $action="";

if($action=='delete'){
$id=$_REQUEST['id'];

$query=mysql_query("set names 'utf8'");
$query = mysql_query("DELETE FROM norse5_proov WHERE id='$id'") or die(mysql_error());

if($query){

echo "<div id='dain'><br><br><br><br><br><br><br><br><br><br>Andmed kustutatud.</div>";

}

}
?>

        <?php //searching
        mysql_query ("set character_set_results='utf8'"); 
        if($_SERVER['REQUEST_METHOD'] == "POST"){
            $query = "SELECT * FROM norse5_proov WHERE 1=1 ";

            if(!empty($_POST["osakond"])){
                $query .= " AND osakond LIKE '%".mysql_real_escape_string($_POST["osakond"])."%'";
            }
            if(!empty($_POST["soetusaasta"])){
                $query .= " AND soetusaasta LIKE '%".mysql_real_escape_string($_POST["soetusaasta"])."%'";
            }
            if(!empty($_POST["it_number"])){
                $query .= " AND it_number LIKE '%".mysql_real_escape_string($_POST["it_number"])."%'";
            }
            if(!empty($_POST["tooteruhm"])){
                $query .= " AND tooteruhm LIKE '%".mysql_real_escape_string($_POST["tooteruhm"])."%'";
            }
            if(!empty($_POST["mudeli_nimetus"])){
                $query .= " AND mudeli_nimetus LIKE '%".mysql_real_escape_string($_POST["mudeli_nimetus"])."%'";
            }
            if(!empty($_POST["sn"])){
                $query .= " AND sn LIKE '%".mysql_real_escape_string($_POST["sn"])."%'";
            }
            if(!empty($_POST["riigivara_nr"])){
                $query .= " AND riigivara_nr LIKE '%".mysql_real_escape_string($_POST["riigivara_nr"])."%'";
            }
            if(!empty($_POST["inventaari_nr"])){
                $query .= " AND inventaari_nr LIKE '%".mysql_real_escape_string($_POST["inventaari_nr"])."%'";
            }
            if(!empty($_POST["maja"])){
                $query .= " AND maja LIKE '%".mysql_real_escape_string($_POST["maja"])."%'";
            }
            if(!empty($_POST["ruum"])){
                $query .= " AND ruum LIKE '%".mysql_real_escape_string($_POST["ruum"])."%'";
            }
            if(!empty($_POST["vastutaja"])){
                $query .= " AND vastutaja LIKE '%".mysql_real_escape_string($_POST["vastutaja"])."%'";
            }
            if(!empty($_POST["markus"])){
                $query .= " AND markus LIKE '%".mysql_real_escape_string($_POST["markus"])."%'";
            }
            if(!empty($_POST["id"])){
                $query .= " AND id LIKE '%".mysql_real_escape_string($_POST["id"])."%'";
            }

            ?>
            <table id="tablk">
                <tr>
                    <th width="80">Osakond</th>
                    <th width="80">Soetusaasta</th>
                    <th width="80">IT Number</th>
                    <th width="80">Tooterühm</th>
                    <th width="80">Mudeli nimetus</th>
                    <th width="80">SN</th>
                    <th width="80">Riigivara nr</th>
                    <th width="80">Inventaari nr</th>
                    <th width="80">Maja</th>
                    <th width="80">Ruum</th>
                    <th width="80">Vastutaja</th>
                    <th width="80">Märkus</th>
                    <th width="80">ID</th>
                    <th width="80">Tegevus</th>
                </tr>
                <?php


                $result = mysql_query($query);
                while($row = mysql_fetch_assoc($result)){
                    echo "<tr>";
                    echo    "<td>".$row["osakond"]."</td>";
                    echo    "<td>".$row["soetusaasta"]."</td>";
                    echo    "<td>".$row["it_number"]."</td>";
                    echo    "<td>".$row["tooteruhm"]."</td>";
                    echo    "<td>".$row["mudeli_nimetus"]."</td>";
                    echo    "<td>".$row["sn"]."</td>";
                    echo    "<td>".$row["riigivara_nr"]."</td>";
                    echo    "<td>".$row["inventaari_nr"]."</td>";
                    echo    "<td>".$row["maja"]."</td>";
                    echo    "<td>".$row["ruum"]."</td>";
                    echo    "<td>".$row["vastutaja"]."</td>";
                    echo    "<td>".$row["markus"]."</td>";
                    echo    "<td>".$row["id"]."</td>";
                    echo "<td>";
                    echo "<button onclick='delete_user( {id} )'>Kustuta</button>"; 
                    echo "<form method='post' action='edit.php?id=<?php echo ".$row["id"]."; ?>'><input type='submit' value='Muuda'>";
                    echo "</td>";

                    echo "</tr>";
                }
                mysql_free_result($result);
                ?>
            </table>
            <?php
        }
        ?>
4

3 回答 3

2

这一行:

echo "<button onclick='delete_user( {id} )'>Kustuta</button>"; 

创建一个按钮并调用delete_user函数,但它不是 PHP 函数而是 Javascript。你可能没有定义这个函数。其次问题可能出在{id},也许你应该写delete_user(".$row["id"].")

我想您必须创建一个按钮来提交要删除的表单。您可能需要两种形式,一种用于编辑(在下一行中有),另一种用于删除。

而不是我引用的行:

echo "<form method='post' action='edit.php?action=delete&id=".$row["id"]."'><input type='submit' value='Muuda'>";
于 2013-03-04T08:16:36.037 回答
0

尝试

echo "<button onclick='delete_user(".$row[id].")'>Kustuta</button>"; 
echo "<form method='post' action='edit.php?action=delete&id=<?php echo ".$row["id"]."; ?>'><input type='submit' value='Muuda'>";
于 2013-03-04T08:18:12.293 回答
0
echo "<form method='post' action='edit.php?id=<?php echo ".$row["id"]."; ?> 

上面的行是在 PHP 字符串中打开一个 PHP 标记。

于 2013-03-04T08:13:18.820 回答