我正在尝试创建一个表来管理上传的音乐,我希望当用户按下“删除”时它会删除该条目。这是我正在使用的代码。
<?php
$sql="SELECT * FROM content WHERE `uploader` = '" . $user_data['username'] . "' ORDER BY id DESC";
$records=mysql_query($sql);
while($sound=mysql_fetch_assoc($records)){
echo "<tr class='adder'>";
echo "<td width='40' class='player'> <a href='".$sound['link']."' class='sm2_button'>Play/</a></td>";
echo '<td width="75" class="name">'.$sound['date'].'</td>';
echo '<td width="150" class="name">'.$sound['name'].'</td>';
echo "<td width='58' class='bpm'>".$sound['uploader']."</td>";
echo "<td width='220' class='keywords'>".$sound['keywords']."</td>";
echo "<td width='50' class='keywords'>Edit</td>";
echo "<td width='50' class='keywords'><span onclick='mysql_query('DELETE FROM content WHERE id = ".$sound['id']." ')>Delete</span></td>";
echo "</tr>";
}
?>
答案 0 :(得分:1)
我不是php开发人员,但我相信你应该将数据提交给服务器。看来您的代码只是打印出来的 &#39; mysql_query(&#39; DELETE FROM content WHERE id =&#34;。$ sound [&#39; id&#39;]。&#34;&#39;)
到html页面。
你应该做的是
答案 1 :(得分:1)
这真的很简单
创建一个名为process.php的页面
<?php
// include your connection file
$id = "0";
if (isset($_GET['id'])) {
$id = $_GET['id'];
}
try {
$sql = "DELETE FROM table-name WHERE id=:id";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
} catch (PDOException $e) {
die("Could not delete from the table: " . $e->getMessage());
}
$GoTo = "/the/path/where/you/want/to/send/the/visitor.php";
header(sprintf("Location: %s", $GoTo));
?>
然后改变这一行......
echo "<td width='50' class='keywords'><span onclick='mysql_query('DELETE FROM content WHERE id = ".$sound['id']." ')>Delete</span></td>";
为...
echo "<td width='50' class='keywords'><a href=\"process.php?id=".$sound['id']."\">Delete</a></td>";
希望这有帮助。
快乐的编码!
答案 2 :(得分:0)
我只是重新使用一个更安全的方法是使用$ _POST ...使用$ _GET任何用户都可以通过更改'process.php删除其他用户条目?id = x'
我必须使用表单按钮才能执行此操作....
表格行
echo "<td width='50' class='keywords'><form name='deletesound' class='deletesound' method='post' action='process.php'>
“;
<强> process.php:强>
<?php
ob_start();
include 'core/init.php';
include 'includes/header.php';
protect_page();
$id = $_POST['getid'];
if (isset($_POST['getid'])) {
$id = $_POST['getid'];
mysql_query("DELETE FROM content WHERE id = $id");
header('Location: manage.php');
exit();
}
?>
答案 3 :(得分:-1)
使用ajax尝试以下代码。
viewContent.php
<?php
$sql="SELECT * FROM content WHERE `uploader` = '" . $user_data['username'] . "' ORDER BY id DESC";
$records=mysql_query($sql);
while($sound=mysql_fetch_assoc($records)){
?>
<tr class='adder'>
<td width='40' class='player'> <a href='<?php echo $sound['link']; ?>' class='sm2_button'>Play/</a></td>
<td width="75" class="name"><?php echo $sound['date']; ?></td>
<td width="150" class="name"><?php echo $sound['name']; ?></td>
<td width='58' class='bpm'><?php echo $sound['uploader']; ?></td>
<td width='220' class='keywords'><?php echo $sound['keywords']; ?></td>
<td width='50' class='keywords'>Edit</td>
<td width='50' class='keywords'><span onclick='deleteContent(<?php echo $sound['id']; ?>)'>Delete</span></td>
</tr>
}
?>
<script>
function deleteContent(id)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
window.location="/viewContent.php";
}
}
xmlhttp.open("GET","deleteContent.php?q="+id,true);
xmlhttp.send();
}
</script>
deleteContent.php
<?php
$id=$_GET['id'];
mysql_query('DELETE FROM content WHERE id = ".$id." ')
echo "done";
?>
上面的代码从视图页面发送请求以通过ajax删除页面,并在获取repsonse时重新加载页面。