简体   繁体   中英

deleting a row from table

I'm trying to make a delete link, when i click on it it will delete a row from sql table with given ID, it should go like this, but it's not working, can you please take a look at my code and tell me what am I doing wrong, thanks !

This is what the link, when i click on archive it should delete the row.

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

and this is 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");
    }
?>  

Thank you in advanced!

Add this before ' header("Location: oglasi.php"); ' -

mysql_query($query);

You need to perform the query against the database and mysql_query() does that.

So, your code becomes-

<?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");
}
?> 

Just to check that the query has been executed right there, you can also do this-

    <?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");
}
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM