Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Pages: [1]
Print
Topic: Deleting data From mysql  (Read 620 times)
« on: August 16, 2008, 09:56:09 AM »
sai Offline
Newbie

View Profile
*
Posts: 10



Syntax
 
"DELETE FROM table_name WHERE column_name=' value' ";
 
 
Overview
 
In this tutorial create 2 files
1. delete.php
2. delete_ac.php

Step
1. Create table "test_mysql" in database "test".
2. Create file delete.php.
3. Create file delete_ac.php.
 
1.Set up database 
 
CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

--
-- Dumping data for table `test_mysql`
--

INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');
 
2.Create file - delete.php
############### Code

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><table width="400%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td>
</tr>
<?

// close while loop
}

// close connection;
mysql_close();

?>
</table></td>
</tr>
</table>

3.Create file delete_ac.php
############### Code

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();

?>
Logged


 
« Reply #1 on: September 23, 2008, 05:49:12 PM »
kurtc Offline
Newbie

View Profile
*
Posts: 10



Nice tutorial, but most people would know simple php-mysql integration scripts such as this one.

Oh, and I fixed it for you.

Code:

// Connect to server and select databse.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");


You don't put speech marks around variables, or else it trys connecting to server '$host', instead of finding the variable host and finding what it's value is

 cool
Logged
« Reply #2 on: September 24, 2008, 09:07:06 AM »
Robert Plank Offline
Newbie

View Profile
*
Posts: 1



Kurt, the speech quotes don't matter.  As long as they are double quotes (not single quotes), "$host" will return the value of the variable $host.
Logged
« Reply #3 on: September 24, 2008, 02:08:06 PM »
harmin Offline
Newbie

View Profile
*
Posts: 13



Btw I think care should be taken to properly escape variables whose value is going to be supplied by users and which are going to be used in a database. Eg. $_GET['id'] here. This way you can avoid the problems with sql injection attacks.
Logged
 
Pages: [1]
Print
Jump to:  

Powered by SMF | SMF © 2006-2008, Simple Machines LLC