PHP native development news station deleting news

Our first two articles have completed the addition and modification of news. So in this lesson, we will introduce you to deleting news. This one is simpler than the previous two!

First create a new_delete.php, then we need to find the delete button on the news list page and add a connection to this button. We also need to transmit data through the id and delete the news based on the id, but here we Still use JS

<a class='button border-red' href='javascript:;' onclick='return del(<?php echo $val['id']?>)'>
<span class='icon-trash-o'></span> 删除</a>

Then add a JS code at the bottom of the news list page:

//单个删除
function del(id){
    if(confirm("您确定要删除吗?")){
        document.location.href = "new_delete.php?id=" + id ;
    }
}

Then we introduce the passed data through the id on the new_delete.php page, and then the most data deal with!

Needless to say, the first step is to connect to the database:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/2
 * Time: 15:44
 */
// 连接mysql数据库
$link = mysqli_connect('localhost', 'root', 'root');
if (!$link) {
    echo "connect mysql error!";
    exit();
}

// 选中数据库 my_db为数据库的名字
$db_selected = mysqli_select_db($link, 'news');
if (!$db_selected) {
    echo "<br>selected db error!";
    exit();
}

The next step is to accept the ID data, then query the data based on the ID, execute the SQL statement,

$id = $_GET['id'];
if( !is_numeric($id) ){
    echo "ERROR!";
    exit;
}
$sql = "delete from new where id = $id";
$result = mysqli_query($link, $sql);

The last step is to delete the data :

if($result){
    echo "删除成功!";
    // 直接跳转进入简历列表
    header("Location: new_list.php");

} else {
    echo "删除失败!";
}

Isn’t it very simple? The deletion function is now complete!


Continuing Learning
||
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/2 * Time: 15:44 */ // 连接mysql数据库 $link = mysqli_connect('localhost', 'root', 'root'); if (!$link) { echo "connect mysql error!"; exit(); } // 选中数据库 my_db为数据库的名字 $db_selected = mysqli_select_db($link, 'news'); if (!$db_selected) { echo "<br>selected db error!"; exit(); } // 根据id 删除 $id = $_GET['id']; if( !is_numeric($id) ){ echo "ERROR!"; exit; } $sql = "delete from new where id = $id"; // 执行sql语句 $result = mysqli_query($link, $sql); if($result){ echo "删除成功!"; // 直接跳转进入简历列表 header("Location: new_list.php"); } else { echo "删除失败!"; }
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!