<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Learn how to  remove a row in mySQL using the power of PHP.</description>
<keywords>tutorial, PHP, tutorials, mysql, database, delete, erase, remove, row, display</keywords>
<title>Delete a Row From mySQL</title>

<slug>
Learn how to  remove a row in mySQL using the power of PHP.</slug>

<text>
Here is an easy PHP script to delete any row from your Database. This tutorial goes along with several others that use the same news database such as Add a Row to mySQL, Displaying a Database, and Edit a Row In mySQL. We have a database called spoono_news, with a table called news. In the mySQL news table, we have 6 fields: id, title, message, who, date, and time. You can see the mySQL code here: <link><url>mysql.txt</url>mysql.txt</link>.This tutorial can be broken down into two sections. Both of the sections can be put on the same page. Open up a new HTML page, save it as delete.php and write all this inside the Body tag. You can see my code that I worked on and made sure worked by right clicking and saving <link><url>delete.txt</url>delete.txt</link>. The first shows you all the rows inside the database with a radio button next to them which you can select to delete a row. The second processes it and deletes it.
<br></br><br></br>
Here is what we have to write in English to pick a row to delete:
<ol>
	<li>Connect to the mySQL </li>
	<li>If a command to delete has not been initialized, then display all the news posts</li>
	<li>When displaying, make the title a link that would go to delete that particular post</li>
</ol>
Here it is in PHP:
<![CDATA[
<pre>
&lt;? 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","password"); 
	
//select which database you want to edit
mysql_select_db("spoono_news"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from news order by id"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
     
	 //make the title a link
      echo "&lt;a href='delete.php?cmd=delete&amp;id=$id'&gt;$title - Delete&lt;/a&gt;";
      echo "&lt;br&gt;";
    }
}
?&gt;
</pre>
]]>

And that is basically all the code we have to write to find the ID for which one you want to delete. Now here is the processing in English:
<ol>
	<li>If the cmd is set to delete, then delete from the database</li>
	<li>Send a confirmation.</li>
</ol>
Here it is in PHP:
<![CDATA[
<pre>
&lt;?
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM news WHERE id=$id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?&gt;
</pre>
]]>
You can see my code that I worked on and made sure worked by right clicking and saving <link><url>delete.txt</url>delete.txt</link>. Not too tough was it? Well thats it folks. I hope it works out for you and if it doesn't, email for help at <link><url>mailto:webmaster@spoono.com</url>webmaster@spoono.com</link> and we'll try to help you out.
</text>

</tutorial>

