AJAX/Jquery not deleting row from MySQL table

DataGuy

I am trying to use AJAX and jQuery to delete a record from a table. The row is being removed from the page view as required, but not from the table.

Im using the following files and related sections:

Query

$result = mysql_query("SELECT id,name FROM myTable");

Query result:

while($row = mysql_fetch_array($result))
{
$id1=$row['id'];
$dataname=$row['name'];
?>
<div class="show">
<span class="name"><?php echo $dataname;  ?></span><br><span class="name"><?php echo $id1;  ?></span>
<span class="action"><a href="#" id="<?php echo $id1; ?>" class="delete" title="Delete">Delete</a></span>
</div>
<?php
}
?>

AJAX Script

<script type="text/javascript">
$(function() {
$(".delete").click(function(){
var element = $(this);
var del_id = element.attr("id");
var info = 'id=' + del_id;
if(confirm("Are you sure you want to delete this?"))
{
 $.ajax({
   type: "POST",
   url: "delete.php",
   data: info,
   success: function(){
 }
});
  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");
 }
return false;
});
});
</script>

delete.php

<?php
include('db.php');
if($_POST['id'])
{
$id=mysql_real_escape_string($_POST['id']);
$delete = "DELETE FROM `testdb` WHERE id = '$id1' ";
mysql_query($delete);
}
?>
Abhik Chakraborty
$id=mysql_real_escape_string($_POST['id']);
$delete = "DELETE FROM `testdb` WHERE id = '$id1' ";

You are using $id1 in the delete statement, it should be $id

$delete = "DELETE FROM `testdb` WHERE id = '$id'";

NOTE : Your code is vulnerable to sql injection, start using preparedStatement with mysqli_ or PDO

UPDATE: Using PDO to do the delete operation

error_reporting(E_ALL);
ini_set('display_errors', '1');
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', '****', '****');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
$id = $_POST['id'] ;
$sql = "DELETE FROM testdb WHERE id = :id "; 
$stmt = $pdo->prepare($sql); 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 
$stmt->execute(); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting Row from users table PHP MYSQL

From Dev

deleting a row from table

From Dev

Deleting specific row from a table in MySQL using PHP

From Dev

"On Delete Cascade" if deleting a row from the child table

From Dev

Deleting a row from an HTML table in javascript

From Dev

Deleting a ROW from MySQL database using MySQLi

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Deleting row in Sqlite table

From Dev

deleting a row in a table

From Dev

In Angularjs, how to update table data after deleting a row from it

From Dev

How to get the value of a cell from an html table after deleting the row?

From Dev

Deleting a row with Php & MySQL

From Dev

MySql - Deleting a row

From Dev

MySql - Deleting a row

From Dev

Deleting mysql database row

From Dev

Deleting a row with Php & MySQL

From Dev

Deleting a row from MySQL in ListView in Visual Studio 2013

From Dev

Deleting table row by id with jQuery

From Dev

Deleting Row in table using jQuery

From Dev

Deleting csv file vales from mysql table using single query

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

Deleting many rows from a big table MySql 5.5.46

From Dev

MySQL: deleting rows based on a condition with data from another table and NO JOIN

From Dev

UITableView deleting row reduced table row height

From Dev

Deleting row using PHP Mysql

From Dev

Delete selected row from table in mysql

From Dev

mySQL - Selecting the latest row from a table

From Dev

Delete an entire row from a table, using MySQL

From Dev

Delete selected row from table in mysql

Related Related

HotTag

Archive