您好我有從DB的ID從數據庫中刪除行
刪除數據的問題,這是我的代碼:確認
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;
}
}
它會刪除包含這些代碼,調用該函數頁刪除:
$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;
}
}
顯示來自DB的元素的LOOP。
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;
}
}
您是否檢查數據庫中的錯誤消息? – Machavity
您使用的數據庫帳戶對此表有刪除權限?你能提供你的代碼來添加例程進行比較嗎? –
在後期添加查找 –