hi i am having a problem deleting data from DB
this is my code: Confirmation
function confirm_delete_mat(materials_id, materials_header)
{
if(confirm('Are You Sure you want to delete page:
' + materials_header + ' ?'))
{
location.href = '../pages/materials/materials_delete.php?pid=' + materials_id;
}
}
it goes to delete page which has this code and calls the function for delete:
$pid = delete_material($materials_id);
if($pid == false)
{
die('Problem Deleting Page');
}
else
{
//header('location: pages_list.php');
die();
}
and of course there is a function itself:
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;
}
}
The LOOP which shows elements from the 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>';
}
i managed to add new element to the table but cant delete it any help please.
Thanks
inserting data
oh ok thats the button
echo '<li><a style="color:#f00" href="../pages/materials/materials_insert.php" onClick="window.location.href=window.location.href ">Add New Page</a></li>';
and thats a function
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;
}
}
You have to use the GET parameter from your javascript link on your delete page ($materials_id is not set there):
$pid = delete_material($_GET["pid"]);