我是这方面的新手,我最近刚刚挖掘代码...我很难理解为什么这不起作用:
我在搜索结果中添加了一个按钮,因此它会删除某些数据字段,但它不会删除。 我在这里错过了什么?
<?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
}
?>
答案 0 :(得分: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'>";
答案 1 :(得分:0)
echo "<form method='post' action='edit.php?id=<?php echo ".$row["id"]."; ?>
上面一行是在PHP字符串中打开PHP标记。
答案 2 :(得分: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'>";