2016-08-26 155 views
0

我试图做一个删除链接,当我点击它时,它会从给定ID的sql表中删除一行,它应该像这样,但它不工作,请你拿看看我的代码并告诉我我做错了什么,谢谢!从表中删除一行

这是什么链接,当我点击归档它应该删除该行。

echo '<td><a href="archivenews.php?newsid='. $row['ID'] .'">Arhiviraj</a>/<a href="archivenews.php?newsid='. $row['ID'] .'">Obrisi</a></td>'; 

,这是archivenews.php

<?php 
    session_start(); 
    require('konektor.php'); 

    if(!(isset($_SESSION["login"]) && $_SESSION["login"] == "OK")) 
    { 
     header("Location: index.php"); 
     exit; 
    } 
    else 
    { 
     $id = $_GET['newsid']; 
     $query = "DELETE FROM `Oglasi` WHERE `ID` = '$id'"; 
     header("Location: oglasi.php"); 
    } 
?> 

谢谢先进!

+1

1。您只要声明一个变量用SQL查询...但你永远不会执行它......并使用准备好的查询..阅读关于sql注入 –

+1

你只是在'$ query'变量中定义一个查询,你没有执行它,所以什么也没有发生。并且'请看一下SQL注入,你有使用这个代码的危险'。 – Paul

+1

谢谢,我现在正在学习准备好的语句:) – Nathanus

回答

0

'header(“Location:oglasi.php”); ' -

mysql_query($query); 

您需要对数据库和mysql_query()执行查询。

所以,你的代码成为─

<?php 
session_start(); 
require('konektor.php'); 

if(!(isset($_SESSION["login"]) && $_SESSION["login"] == "OK")) 
{ 
    header("Location: index.php"); 
    exit; 
} 
else 
{ 
    $id = $_GET['newsid']; 
    $query = "DELETE FROM `Oglasi` WHERE `ID` = '$id'"; 
    mysql_query($query); 
    header("Location: oglasi.php"); 
} 
?> 

只是为了检查该查询已被执行就在那里,你也可以做这个 -

<?php 
session_start(); 
require('konektor.php'); 

if(!(isset($_SESSION["login"]) && $_SESSION["login"] == "OK")) 
{ 
    header("Location: index.php"); 
    exit; 
} 
else 
{ 
    $id = $_GET['newsid']; 
    $query = "DELETE FROM `Oglasi` WHERE `ID` = '$id'"; 
    $result = mysql_query($query); 
    if(!$result) 
    { 
     die("Could not archive" . mysql_error()); 
     //This will display any error in the execution of query. 
    } 
    header("Location: oglasi.php"); 
} 
?>