Delete specific SQL row on jquery .click. (I am trying to delete an image from the database when it is...
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
add a comment |
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
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
add a comment |
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
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
php jquery mysql
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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