通过ID

时间:2015-08-20 16:03:09

标签: php mysql

我在从数据库中删除数据时遇到问题

这是我的代码:确认

    function confirm_delete_mat(materials_id, materials_header)
{
    if(confirm('Are You Sure you want to delete page:\n' + materials_header + ' ?'))
    {
        location.href = '../pages/materials/materials_delete.php?pid=' + materials_id;
    }
} 

它会删除包含此代码的页面并调用delete函数:

$pid = delete_material($materials_id); 

if($pid == false)
{
    die('Problem Deleting Page');
}
else
{
    //header('location: pages_list.php');
    die();
}

当然还有一个功能本身:

function delete_material($materials_id)
{
    global $db;

    try
    {
        $sql = "DELETE FROM materials WHERE materials_id = :materials_id LIMIT 1";      
        $stmt = $db->prepare($sql);

        $stmt->bindParam(':materials_id', $materials_id, PDO::PARAM_INT);

        return $stmt->execute();
    }
    catch(Exception $e) 
    {
       return false;
    }   
}

LOOP,显示来自DB的元素。

foreach($materials as $mat_key => $mat_val)
        {   

            echo '<section class="wrapper_matrls">';
            echo '<ul>';
            echo '<li>';
            echo '<div class="mat_header"> <h3> '.$mat_val['materials_header'].' </h3> </div>'; 
            echo '<p>';
            echo '<img src="../../images/'.$mat_val['materials_src'].'" alt="" title="" />';
            echo $mat_val['materials_text'];                
            echo '</p>';
            echo '</li>';
            echo '</ul>';
            echo '</section>';
            echo $materials_id = $mat_val['materials_id'];
            echo $materials_header = $mat_val['materials_header'];
            echo '<a href="javascript:confirm_delete_mat('.$materials_id.', \''.$materials_header.'\')" class="">Delete This page</a>';

        }

我设法将新元素添加到表中但不能删除它 请帮助。

由于

插入数据

好的,好的 那就是按钮

echo '<li><a style="color:#f00" href="../pages/materials/materials_insert.php" onClick="window.location.href=window.location.href ">Add New Page</a></li>';

那是一个功能

function insert_materials()
{
    global $db;

    try
    {
        $sql = "INSERT INTO materials 
        (   materials_header, materials_order, materials_src , materials_text) 
         VALUES 
        ('New Header', 1000 , 'gallery/small/1435238385.jpg', 'enter text'  )";     

        $stmt = $db->prepare($sql);

        if( $stmt->execute() == false )
        return false;
        else
        return 
        $db->lastInsertId();

    }
    catch(Exception $e) 
    {
       //ERROR - function returns FALSE
       return false;
    }   
}

1 个答案:

答案 0 :(得分:0)

您必须使用删除页面上的javascript链接中的GET参数($ materials_id未在此处设置):

$pid = delete_material($_GET["pid"]);