Hi,

I have created a database, where it is possible to create posts and see them on a different page.

On the page where it's possible to see all the records I want to create a delete link so it is possible to delete the record where you have pressed the delete link.

I have written some code, but it doesn't delete the record it only refreshes the page witch is called se_kunder.php

Can you please help me with what's missing? aren't I executing the delete sql query, or how do I do that?

I hope you understand what I meen.
Thank you for your help.
Here is the code:

<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxx", $con);

$id = $row['ID'];
$result = mysql_query($sql);

    
   

$result = mysql_query("SELECT * FROM kunder");




$sql = "DELETE FROM kunder WHERE ID='$id'"; 



while($row = mysql_fetch_array($result))
  
 
  
  echo "<b>NY KUNDE</b>" . "<br><br><b /><u>Virksomhedsnavn</u></b><br>" . $row['Virksomhedsnavn'] . "<br><br><b><u>CVR. nummer</u></b> <br> " . $row['cvrnummer']  . 
  "<br><br><b /><u>Kontaktperson</u></b><br>" . $row['kontaktperson'] . "<br><br><b><u>E-mail</u></b> <br> " . $row['email'] . 
  "<br><br><b /><u>kundenummer</u></b><br>" . $row['kundenummer'] . "<br><br><b><u>fakturanummer</u></b> <br> " . $row['fakturanummer'] .
  "<br><br><b /><u>oprettet</u></b><br>" . $row['oprettet'] . "<br><br><b><u>brugernavn til ftp</u></b> <br> " . $row['brugerftp'] .
  "<br><br><b /><u>kodeord til ftp</u></b><br>" . $row['passftp'] . "<br><br><b><u>brugernavn til cms</u></b> <br> " . $row['brugercms'] .
  "<br><br><b /><u>kodeord til cms</u></b><br>" . $row['passcms'] . "<br><br><b><u>udbyder</u></b> <br> " . $row['hosters'] .
  "<br><br><b /><u>betalt</u></b><br>" . $row['betalt'] . "<br><br><b><u>skal betale senest</u></b> <br> " . $row['betalsenest'] .
  "<br><br><b /><u>send neste faktura</u></b><br>" . $row['nestefaktura'] . "<br><br><b><u>pris</u></b> <br> " . $row['pris'] . 
  "<br><br><b /><u>webadresse</u></b><br>" . $row['webadresse'] . "<br><br><b><u>link til faktura</u></b> <br> " . $row['fakturaurl'] .    
  "<br><br><b /><u>link til projektbeskrivelse</u></b><br>" . $row['projektbeskrivelseurl'] . "<br><br><b><u>noter</u></b> <br> " . $row['noter']. "<br><br>" .
  '<a href="se_kunder.php?id=$id\">Slet</a>' .
  "<br><br>" .
  "<hr />";;

  

mysql_close($con);
?>

On line 12 there is no variable $row yet, so $id is undefined.

Ok thank's but how is it I define the $id in the variable $row?

Thank's

What row do you want to delete ?

I want to delete the row that the delete link is on.
So there is going to be a delete link on every row.

so it something like, it's gonna delete this row if I click this link.
So I probably connect the link via the id of the row.

Hope you understand

Your reference to $id needs to be $row

Like this: $row = $row;

it still doesn't work :(

now that part looks like this:

$row = $row['ID'];

$id = $row['ID'];
$result = mysql_query($sql);

    
$result = mysql_query("SELECT * FROM kunder");

$sql = "DELETE FROM kunder WHERE ID='$id'";

And this link looks like this:

'<a href="se_kunder.php?id=$id\">Slet</a>'

If you are just trying to delete a record you just need:

mysql_query("DELETE FROM kunder WHERE ID='".$row['ID']."'";

A for your link, that is just the formatting:

"<a href=\"se_kunder.php?id=".$row['ID']."\">Slet</a>" .

where should I put the:

mysql_query("DELETE FROM kunder WHERE ID='".$row['ID']."'";

And is it instead of something else, should I delete something in my code and put this instead.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.