我想使用ajax刪除圖像。它顯示警報消息,表明數據已被刪除,但表中的記錄在那裏。我怎樣才能從表中刪除圖像記錄....使用ajax和php從數據庫中刪除圖像
<span><input type="submit" id="del_btn" value="Delete Image" /></span>
<script type="text/javascript">
$(document).ready(function() {
$("input#del_btn").click(function(){
$.ajax({
type: "POST",
url: "del.php", //
data: {id: <?php echo $delid; ?>},
success: function(msg){
alert("Image Deleted from database");
},
error: function(){
alert("failure");
}
});
});
});
</script>
,這是del.php
<?php
if (isset($_POST['id'])) {
$imgid = $_POST['id'];
$con=new PDO("mysql:host=localhost;dbname=newimg","root","");
$sql = "DELETE FROM attempt010 WHERE id='$imgid' ";
$con->execute($sql);
}
?>
你刪除查詢爲您開放,注射,請使用綁定(即:準備它正確)。看到; http://php.net/manual/en/pdo.prepare.php –
也請接受回答的習慣,https://meta.stackexchange.com/questions/5234/how-does-accepting-an-回答工作。這個問題仍在等待接受,https://stackoverflow.com/questions/45654837/how-do-i-redirect-a-form-after-submission-to-the-same-page。 – chris85
對不起,我是新來的,我沒有意識到這一點......我會記住 –