Home >Backend Development >PHP Tutorial >PHP implementation code to delete records and delete image files at the same time_PHP tutorial

PHP implementation code to delete records and delete image files at the same time_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:23868browse

Copy code The code is as follows:

$cn = mysql_connect('127.0.0.1','root','root') or die ('database connect fail');
mysql_select_db('test',$cn);
mysql_query("set names 'gbk'");
/*
Create database tutorial
CREATE DATABASE `test` ;
Create data table test1
CREATE TABLE `test`.`test1` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 40 ) NULL DEFAULT '0',
`litpic` VARCHAR( 100 ) NULL DEFAULT '0',
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
*/
$sql = " Select * from test1 ";
$query = mysql_query( $sql ) or die($sql);
if( mysql_num_rows( $query ) )
{
while( $rs = mysql_fetch_array( $ query ) )
{
$sql = "Delete from test1 where id=".$rs['id'];
if( mysql_query( $sql ) )
{
if( defile( $rs['litpic'] ))
{
echo 'Failed to delete picture file';
}
else
{
echo 'The record was deleted successfully and deleted Record the corresponding pictures or files';
}
}
else
{
echo 'Record failed to save deleted data and pictures';
}
}
}
else
{
echo 'No record';
}
/*
@pare return
#$url string
Delete file
*/
function delfile($url)
{
if( file_exists($url) && is_file( $url ) )
{
if( unlink( $url ) )
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321780.htmlTechArticleCopy the code as follows: $cn = mysql_connect('127.0.0.1','root','root') or die('database connect fail'); mysql_select_db('test',$cn); mysql_query("set names 'gbk'"); /* Create data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn