-2

Okay, so what I've got here is a PHP generated table in which the values are pulled from a database. Now, if the administrator is logged in, I'd like to add an option to delete a row from the database.

I've got the structure all set up, but I have no idea what to put in the path of the link that is supposed to delete the row. How can I do this?

<?php
    $rb = 1;
    include 'konekcija.php';
    $query = "SELECT * FROM oglasi";
    $rezultat = mysql_query($query);
    while($niz = mysql_fetch_array($rezultat)){

        echo "<tr><td>" . $rb . "</td><td>" . $niz['model'] . "</td><td>" . $niz['cena'] . "</td><td><img src='" . $niz['slika_path'] . "'</img></td>";
        if($_SESSION['uloga'] == "admin"){
            echo "<td><a href='#'>Obrisi</a></td>";
        }
        echo "</tr>";
        $rb++;
    }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • So when yous say delete row... Do you mean delete row from html table? Or actually delete row from database when html row is deleted? – bluegman991 Dec 13 '13 at 17:32
  • For one thing, you will need `session_start();` since you're using sessions (if it's not inside your include already). `if($_SESSION['uloga'] == "admin")` (*Just saying*) ;-) – Funk Forty Niner Dec 13 '13 at 17:33
  • i think you have to create a button for every row and send ajax request on its click to delete row by id. – Manwal Dec 13 '13 at 17:34
  • [**See this Q&A on SO**](http://stackoverflow.com/q/14475096/1415724) which helped me achieve something similar. – Funk Forty Niner Dec 13 '13 at 17:37
  • Yeah, I meant to rid it from the database. I've got the session_start() up there, this is just a piece of code. – user3071639 Dec 13 '13 at 17:38

3 Answers3

0

You could try something like

echo "<td><a href='delete.php?row=" .$niz['id']. "'>Obrisi</a></td>";

Where delete.php is a php page that checks if the user is an administrator, parses the id value via GET (always remember to do proper escaping to avoid injections, or use other query systems, like prepared statements or stored procedures), and deletes the row with the specified id. Of course every record must have a unique id. Eventually the page redirects to the table view page.

Important sidenote: mysql_* functions are deprecated, use mysqli_* ones instead!

Vereos
  • 503
  • 4
  • 15
0

Well first of the link that you should use to delete the row is going to be whatever link you create.

Therefore you will have to create a new php page, or modify an existing one. To handle the requests to delete the rows.

Here is a quick example:

handler.php

<?php
//execute database connection here
switch($_GET['action'])
{
    case 'delete':
        mysql_query('DELETE FROM `TABLE_NAME` WHERE '.$_GET['where']);
        break;

    case 'add':
        //parse info and submit request to db
        break;

    case 'edit':
        //parse info and submit request to db
        break;
}
//close db connection
?>

Disclaimer! This example is by no means secure, do not use it in production.

Then the absolute link you would use would be something like: (unescaped version) http://host.com/pathtofile/handler.php?action=delete&where=(row_name='rowid')

bluegman991
  • 707
  • 4
  • 9
0

guo

$rb = 1;
include 'konekcija.php';
$query = "SELECT * FROM oglasi";
$rezultat = mysql_query($query);
while($niz = mysql_fetch_array($rezultat)){

    echo "<tr><td>" . $rb . "</td><td>" . $niz['model'] . "</td><td>" . $niz['cena'] . "</td><td><img src='" . $niz['slika_path'] . "'</img></td>";
    if($_SESSION['uloga'] == "admin"){
        echo "<td><a href='#'>Obrisi</a></td>";
    }
    echo "</tr>";
    $rb++;
}

?>

fedorqui
  • 275,237
  • 103
  • 548
  • 598
hamdy
  • 1