Delete specific SQL row on jquery .click. (I am trying to delete an image from the database when it is...












0















i have a row in the table 'files' where 'id' is a unique identifier column, and equals $imgId. imgId=$row['id'];



<script type="text/javascript">
$( "#<?php echo $imgId ?>" ).click(function() {
<?php
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}
$conn->close();
?>
});
</script>


I am displaying multiple embeds where the html id is equal to the unique identifier sql column id for that image.



<embed id="<?php echo  $imgId; ?>"class="delete-row" src="<?php echo $filePath; ?>" type="<?php echo $fileMime; ?>"/>


I just want it so when you click on a spcific image it deletes it from the database and removed the image file from the /uploads folder. Any help would be awesome.










share|improve this question























  • code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

    – user585148
    Jan 2 at 6:08













  • Use jquery click event and ajax post request

    – Zain Farooq
    Jan 2 at 6:08











  • Possible duplicate of jQuery Ajax POST example with PHP

    – Zain Farooq
    Jan 2 at 6:09











  • Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

    – Alex Howansky
    Jan 2 at 6:10
















0















i have a row in the table 'files' where 'id' is a unique identifier column, and equals $imgId. imgId=$row['id'];



<script type="text/javascript">
$( "#<?php echo $imgId ?>" ).click(function() {
<?php
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}
$conn->close();
?>
});
</script>


I am displaying multiple embeds where the html id is equal to the unique identifier sql column id for that image.



<embed id="<?php echo  $imgId; ?>"class="delete-row" src="<?php echo $filePath; ?>" type="<?php echo $fileMime; ?>"/>


I just want it so when you click on a spcific image it deletes it from the database and removed the image file from the /uploads folder. Any help would be awesome.










share|improve this question























  • code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

    – user585148
    Jan 2 at 6:08













  • Use jquery click event and ajax post request

    – Zain Farooq
    Jan 2 at 6:08











  • Possible duplicate of jQuery Ajax POST example with PHP

    – Zain Farooq
    Jan 2 at 6:09











  • Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

    – Alex Howansky
    Jan 2 at 6:10














0












0








0








i have a row in the table 'files' where 'id' is a unique identifier column, and equals $imgId. imgId=$row['id'];



<script type="text/javascript">
$( "#<?php echo $imgId ?>" ).click(function() {
<?php
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}
$conn->close();
?>
});
</script>


I am displaying multiple embeds where the html id is equal to the unique identifier sql column id for that image.



<embed id="<?php echo  $imgId; ?>"class="delete-row" src="<?php echo $filePath; ?>" type="<?php echo $fileMime; ?>"/>


I just want it so when you click on a spcific image it deletes it from the database and removed the image file from the /uploads folder. Any help would be awesome.










share|improve this question














i have a row in the table 'files' where 'id' is a unique identifier column, and equals $imgId. imgId=$row['id'];



<script type="text/javascript">
$( "#<?php echo $imgId ?>" ).click(function() {
<?php
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}
$conn->close();
?>
});
</script>


I am displaying multiple embeds where the html id is equal to the unique identifier sql column id for that image.



<embed id="<?php echo  $imgId; ?>"class="delete-row" src="<?php echo $filePath; ?>" type="<?php echo $fileMime; ?>"/>


I just want it so when you click on a spcific image it deletes it from the database and removed the image file from the /uploads folder. Any help would be awesome.







php jquery mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 6:04









user585148user585148

6318




6318













  • code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

    – user585148
    Jan 2 at 6:08













  • Use jquery click event and ajax post request

    – Zain Farooq
    Jan 2 at 6:08











  • Possible duplicate of jQuery Ajax POST example with PHP

    – Zain Farooq
    Jan 2 at 6:09











  • Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

    – Alex Howansky
    Jan 2 at 6:10



















  • code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

    – user585148
    Jan 2 at 6:08













  • Use jquery click event and ajax post request

    – Zain Farooq
    Jan 2 at 6:08











  • Possible duplicate of jQuery Ajax POST example with PHP

    – Zain Farooq
    Jan 2 at 6:09











  • Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

    – Alex Howansky
    Jan 2 at 6:10

















code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

– user585148
Jan 2 at 6:08







code I'm currently using. Not sure where to add sql... <script type="text/javascript"> $( "#<?php echo $imgId ?>" ).click(function() { $( "#<?php echo $imgId ?>" ).remove(); }); </script>

– user585148
Jan 2 at 6:08















Use jquery click event and ajax post request

– Zain Farooq
Jan 2 at 6:08





Use jquery click event and ajax post request

– Zain Farooq
Jan 2 at 6:08













Possible duplicate of jQuery Ajax POST example with PHP

– Zain Farooq
Jan 2 at 6:09





Possible duplicate of jQuery Ajax POST example with PHP

– Zain Farooq
Jan 2 at 6:09













Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

– Alex Howansky
Jan 2 at 6:10





Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.

– Alex Howansky
Jan 2 at 6:10












1 Answer
1






active

oldest

votes


















0














The way that you are performing this is not advisable. You cannot use mysql pdo objects inside jquery click function. I suggest that you use the below approach. Please see that this is just a baseline. Please feel free to change the code as per your requirement.



In the html file or where you are using the script tag, use the below syntax:



<script type="text/javascript">
jQuery(".delete-row").click(function(){
var imageId = jQuery(this).attr('id');
var filePath = jQuery(this).attr('src');

jQuery.ajax({
type: "POST",
url: "[DOMAIN]/deleteImage.php",
data: {imageId: imageId, path: filePath},
success: function(res) {
if (res === "deleted") {
jQuery(this).remove();
}
}
});
});
</script>


In the deleteImage.php file:



<?php
require 'conn.php';

if (!empty($_POST['imageId'])) {
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE && unlink($_POST['path'])) {
return "deleted";
} else {
return false;
}
}

$conn->close();

return false;
?>


Hope this helps.






share|improve this answer
























  • Thanks so much, it still doesn't seem to be working but I will keep working at it.

    – user585148
    Jan 2 at 6:43











  • Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

    – Kishen Nagaraju
    Jan 2 at 6:53













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001926%2fdelete-specific-sql-row-on-jquery-click-i-am-trying-to-delete-an-image-from-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The way that you are performing this is not advisable. You cannot use mysql pdo objects inside jquery click function. I suggest that you use the below approach. Please see that this is just a baseline. Please feel free to change the code as per your requirement.



In the html file or where you are using the script tag, use the below syntax:



<script type="text/javascript">
jQuery(".delete-row").click(function(){
var imageId = jQuery(this).attr('id');
var filePath = jQuery(this).attr('src');

jQuery.ajax({
type: "POST",
url: "[DOMAIN]/deleteImage.php",
data: {imageId: imageId, path: filePath},
success: function(res) {
if (res === "deleted") {
jQuery(this).remove();
}
}
});
});
</script>


In the deleteImage.php file:



<?php
require 'conn.php';

if (!empty($_POST['imageId'])) {
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE && unlink($_POST['path'])) {
return "deleted";
} else {
return false;
}
}

$conn->close();

return false;
?>


Hope this helps.






share|improve this answer
























  • Thanks so much, it still doesn't seem to be working but I will keep working at it.

    – user585148
    Jan 2 at 6:43











  • Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

    – Kishen Nagaraju
    Jan 2 at 6:53


















0














The way that you are performing this is not advisable. You cannot use mysql pdo objects inside jquery click function. I suggest that you use the below approach. Please see that this is just a baseline. Please feel free to change the code as per your requirement.



In the html file or where you are using the script tag, use the below syntax:



<script type="text/javascript">
jQuery(".delete-row").click(function(){
var imageId = jQuery(this).attr('id');
var filePath = jQuery(this).attr('src');

jQuery.ajax({
type: "POST",
url: "[DOMAIN]/deleteImage.php",
data: {imageId: imageId, path: filePath},
success: function(res) {
if (res === "deleted") {
jQuery(this).remove();
}
}
});
});
</script>


In the deleteImage.php file:



<?php
require 'conn.php';

if (!empty($_POST['imageId'])) {
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE && unlink($_POST['path'])) {
return "deleted";
} else {
return false;
}
}

$conn->close();

return false;
?>


Hope this helps.






share|improve this answer
























  • Thanks so much, it still doesn't seem to be working but I will keep working at it.

    – user585148
    Jan 2 at 6:43











  • Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

    – Kishen Nagaraju
    Jan 2 at 6:53
















0












0








0







The way that you are performing this is not advisable. You cannot use mysql pdo objects inside jquery click function. I suggest that you use the below approach. Please see that this is just a baseline. Please feel free to change the code as per your requirement.



In the html file or where you are using the script tag, use the below syntax:



<script type="text/javascript">
jQuery(".delete-row").click(function(){
var imageId = jQuery(this).attr('id');
var filePath = jQuery(this).attr('src');

jQuery.ajax({
type: "POST",
url: "[DOMAIN]/deleteImage.php",
data: {imageId: imageId, path: filePath},
success: function(res) {
if (res === "deleted") {
jQuery(this).remove();
}
}
});
});
</script>


In the deleteImage.php file:



<?php
require 'conn.php';

if (!empty($_POST['imageId'])) {
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE && unlink($_POST['path'])) {
return "deleted";
} else {
return false;
}
}

$conn->close();

return false;
?>


Hope this helps.






share|improve this answer













The way that you are performing this is not advisable. You cannot use mysql pdo objects inside jquery click function. I suggest that you use the below approach. Please see that this is just a baseline. Please feel free to change the code as per your requirement.



In the html file or where you are using the script tag, use the below syntax:



<script type="text/javascript">
jQuery(".delete-row").click(function(){
var imageId = jQuery(this).attr('id');
var filePath = jQuery(this).attr('src');

jQuery.ajax({
type: "POST",
url: "[DOMAIN]/deleteImage.php",
data: {imageId: imageId, path: filePath},
success: function(res) {
if (res === "deleted") {
jQuery(this).remove();
}
}
});
});
</script>


In the deleteImage.php file:



<?php
require 'conn.php';

if (!empty($_POST['imageId'])) {
$sql = "DELETE FROM files WHERE id=$imgId";

if ($con->query($sql) === TRUE && unlink($_POST['path'])) {
return "deleted";
} else {
return false;
}
}

$conn->close();

return false;
?>


Hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 6:29









Kishen NagarajuKishen Nagaraju

1,140213




1,140213













  • Thanks so much, it still doesn't seem to be working but I will keep working at it.

    – user585148
    Jan 2 at 6:43











  • Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

    – Kishen Nagaraju
    Jan 2 at 6:53





















  • Thanks so much, it still doesn't seem to be working but I will keep working at it.

    – user585148
    Jan 2 at 6:43











  • Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

    – Kishen Nagaraju
    Jan 2 at 6:53



















Thanks so much, it still doesn't seem to be working but I will keep working at it.

– user585148
Jan 2 at 6:43





Thanks so much, it still doesn't seem to be working but I will keep working at it.

– user585148
Jan 2 at 6:43













Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

– Kishen Nagaraju
Jan 2 at 6:53







Sure. You're Welcome. I have given the solution in Core PHP. If you are using any MVC, then please accomodate the above logic in your code accordingly and afterwards please upvote the answer if it helped you in any way.

– Kishen Nagaraju
Jan 2 at 6:53






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001926%2fdelete-specific-sql-row-on-jquery-click-i-am-trying-to-delete-an-image-from-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]