0

I have taken a jQuery script which would remove divs on a click, but I want to implement deleting records of a MySQL database. In the delete.php:

<?php 

$photo_id = $_POST['id'];

$sql = "DELETE FROM photos
  WHERE id = '" . $photo_id . "'";

$result = mysql_query($sql) or die(mysql_error());

?>

The jQuery script:

$(document).ready(function() {
   $('#load').hide();
});

$(function() {
$(".delete").click(function() {
$('#load').fadeIn();
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(){
 commentContainer.slideUp('slow', function() {$("#photo-" + id).remove();});
 $('#load').fadeOut();
  }

 });

return false;
 });
});

The div goes away when I click on it, but then after I refresh the page, it appears again...

How do I get it to delete it from the database?

EDIT: Woopsie... forgot to add the db.php to it, so it works now >.<

1
  • Where is the connection to the database and prevention of sql injection Commented Jul 6, 2015 at 21:02

2 Answers 2

1

There's no way the php could even come close to working. Where is the database? Check out http://www.php.net/manual/en/mysql.examples-basic.php from which you can see there's more to the database than just a query.

<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Are you trying to teach me how to use PHP and MySQL with databases? LOL I already know how to do stuff like that...
Obviously not. Sql injection
0

You have your data as a GET string, but you are using a POST request, try changing your string variable to an object. Like :

$(document).ready(function() {
   $('#load').hide();
});

$(function() {
$(".delete").click(function() {
$('#load').fadeIn();
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = { id : id };

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(){
 commentContainer.slideUp('slow', function() {$("#photo-" + id).remove();});
 $('#load').fadeOut();
  }

 });

return false;
 });
});

Plus I am hoping you are preparing your MySQL connection properly in your PHP, you cannot just call mysql_query and hope it will know which database you mean, and how to connect to it by itself :)

Look at @Quotidian answer! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.