Jump to content


Check out our Community Blogs

Zizzy

Member Since 08 Nov 2011
Offline Last Active May 07 2012 07:12 AM
-----

#622803 Delete MySQL Field - HTML Table

Posted by Zizzy on 26 February 2012 - 02:58 PM

Hello all of CodeCall reading this;

Introduction
First of all, I'd like to apologize if this is a tutorial that may have already been posted in the past, for I have not read back through the pages. If so, in my defense, it may be good to have another recent post for this, for new users not having to search/go through many, many pages. This is also my first tutorial, so I hope it's easy to understand and it helps if you were looking for something like this, or it would be of use to you. :) NOW, on to the tutorial:

Notepad and Notepad++ - Your choice
First off, I'd recommend downloading the latest version of Notepad++ so you can follow along with the tutorial, and not just copy/paste it into your page. Doing it this way, you will learn to write PHP - not learn how to copy and paste other code.

If you'd rather not download and install, you can do this in Notepad ( XP: Start > Run > Notepad, Vista+: Start -> Search bar -> Notepad). If you'd like to use Notepad++, you can download it from the official website here, by clicking this link.

On To The Code
As we go through the code, I will explain what I am doing, use comments in the code, explaining and breaking down what every piece does :)

Connect to the database (database.php)

<?php
$connect = mysql_connect("localhost","db_user","dbpassword"); // this is selecting which database you want to use, with a variable (for later in the code), and using the mysql_connect function to let the code know you want to connect

if (!$connect) // using and if statement to see if the connection is incorrect, and display a mysql_error() message, which lets the user know what the issue is, and then exit the code with die()
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database_name", $connect); // this is selecting the database itself from $connect... which is the variable we created at the beginning of the script... make sure you replace "database_name" with your database, as well as the information provided in $connect

// finally, we end the PHP script for database.php
?>


display.php - displaying the results

<?php
include("database.php"); // include the database file with the include() function.... make sure if you make an includes folder (for those who do) to set the path: include("/path/database.php");

$infoGrab = mysql_query("SELECT * FROM your_table"); // this is taking everything (*) from whichever table you want, from the selected database in database.php, which we included.

echo "<p><b>Information to Delete</b></p>"; // displaying a title (optional), above the table
echo "<table border = '1' cellpadding = '10'>"; //starting off the table
echo "<tr><b><i><th><font color='purple'>Field One:</font></b></i></th> <th><b><i><font color='blue'>Field Two:</font></b></i></th> <th><b><i><font color='green'>Field Three:</font></b></i></th> <th><b><i><font color='red'>Delete File</b></font></i></th></tr></b></i>"; // this is setting the table labels (above the data), with style/color (<b>, <i>, <font> are optional tags)

while ($Row = mysql_fetch_array($infoGrab)) { //while variable $Row = getting data from $infoGrab, which was selecting everything from your selected table
$ID = $Row['id']; // use the ID (should be PRIMARY and AI - auto increment) for specific deleting
$secondVariable = $Row['field_two']; // the second field and variable (after the ID variable)
$thirdVariable = $Row['field_three']; //the third field
$fourthVariable = $Row['field_four']; // the fourth variable

echo "<tr>
<td><b><i><font color='purple'>{$secondVariable}</font></b></i></td> // showing the field after ID
<td><b><i><font color='blue'>{$thirdVariable}</font></b></i></td> //showing the next field
<td><b><i><font color='green'>{$fourthVariable}</font></b></i></td>"; //showing the next field
echo "<td><a href='deleted.php?id='{$ID}'><img src='images/delete.png' width='25' height='25' /></a></td>"; //showing the delete button (feel free to use the delete icon attached, or use plain text instead of <img src>

}

//end the script
?>


deleted.php - deleting the item

if (isset($_GET['id']) && is_numeric($_GET['id'])) { // if the ID is set (isset) and is a number (is_numeric)
$ID = $_GET['id']; // variable ID is getting the ID from the form

$Delete = mysql_query("DELETE * FROM table WHERE id='{$ID}'"); // deleting the specific ID ($ID)

// next you can pretty much echo (display) any successful delete message you want...

echo "<p>Deleted successfully.</p>"; // any message
echo "<p><a href='../index.php'>Return Home</a></p>"; // link or text optional


} else { //something goes wrong(?), display error
echo "<p>Error code #7</p>"; // you can use any error message or 'code' you want

// ending the if and else so we don't get a buggy unexpected $end
}
}

// ending the script
?>

Thanks, readers!
I hope you enjoyed the tutorial, and I hope it comes of use to what you may be looking for. :)

-Zizz


  • 1


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download