Removing 1 row from table with button issue
up vote
-1
down vote
favorite
Im working on an older project. that needs a small fix with deleting a specific row from the database. There is a loop that retrieves images from the database and adds a delete button to it as well. but when i click the remove button it removes all the images instead of the image i click on. so if it retrieves 5 images. it removes 5 instead of 1 that i click on. ps.(don't hate about the older version of sql that's used pls :)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$row['ID']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
}
php html sql
add a comment |
up vote
-1
down vote
favorite
Im working on an older project. that needs a small fix with deleting a specific row from the database. There is a loop that retrieves images from the database and adds a delete button to it as well. but when i click the remove button it removes all the images instead of the image i click on. so if it retrieves 5 images. it removes 5 instead of 1 that i click on. ps.(don't hate about the older version of sql that's used pls :)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$row['ID']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
}
php html sql
Post your js code
– Saad Suri
Nov 12 at 10:44
you have write the delete query inside the while loop and also using$row['ID']. that's mean all images will be deleted . you should write delete query outside the loop and get image id fromform submit
– Bilal Ahmed
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
The above dupe is for mixingmysql_query+mysqli_queryinside theif, and why are you not deleting$_POST['removeSel']outside the loop instead of deleting all the records that you fetch
– Alon Eitan
Nov 12 at 10:47
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Im working on an older project. that needs a small fix with deleting a specific row from the database. There is a loop that retrieves images from the database and adds a delete button to it as well. but when i click the remove button it removes all the images instead of the image i click on. so if it retrieves 5 images. it removes 5 instead of 1 that i click on. ps.(don't hate about the older version of sql that's used pls :)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$row['ID']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
}
php html sql
Im working on an older project. that needs a small fix with deleting a specific row from the database. There is a loop that retrieves images from the database and adds a delete button to it as well. but when i click the remove button it removes all the images instead of the image i click on. so if it retrieves 5 images. it removes 5 instead of 1 that i click on. ps.(don't hate about the older version of sql that's used pls :)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$row['ID']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
}
php html sql
php html sql
edited Nov 12 at 10:46
asked Nov 12 at 10:40
Helpmeplsty
61
61
Post your js code
– Saad Suri
Nov 12 at 10:44
you have write the delete query inside the while loop and also using$row['ID']. that's mean all images will be deleted . you should write delete query outside the loop and get image id fromform submit
– Bilal Ahmed
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
The above dupe is for mixingmysql_query+mysqli_queryinside theif, and why are you not deleting$_POST['removeSel']outside the loop instead of deleting all the records that you fetch
– Alon Eitan
Nov 12 at 10:47
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04
add a comment |
Post your js code
– Saad Suri
Nov 12 at 10:44
you have write the delete query inside the while loop and also using$row['ID']. that's mean all images will be deleted . you should write delete query outside the loop and get image id fromform submit
– Bilal Ahmed
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
The above dupe is for mixingmysql_query+mysqli_queryinside theif, and why are you not deleting$_POST['removeSel']outside the loop instead of deleting all the records that you fetch
– Alon Eitan
Nov 12 at 10:47
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04
Post your js code
– Saad Suri
Nov 12 at 10:44
Post your js code
– Saad Suri
Nov 12 at 10:44
you have write the delete query inside the while loop and also using
$row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit– Bilal Ahmed
Nov 12 at 10:46
you have write the delete query inside the while loop and also using
$row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit– Bilal Ahmed
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
The above dupe is for mixing
mysql_query + mysqli_query inside the if, and why are you not deleting $_POST['removeSel']outside the loop instead of deleting all the records that you fetch– Alon Eitan
Nov 12 at 10:47
The above dupe is for mixing
mysql_query + mysqli_query inside the if, and why are you not deleting $_POST['removeSel']outside the loop instead of deleting all the records that you fetch– Alon Eitan
Nov 12 at 10:47
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
accepted
As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
}
//here is delete image code outside of loop
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
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%2f53260438%2fremoving-1-row-from-table-with-button-issue%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
up vote
-1
down vote
accepted
As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
}
//here is delete image code outside of loop
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
add a comment |
up vote
-1
down vote
accepted
As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
}
//here is delete image code outside of loop
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
}
//here is delete image code outside of loop
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)
$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
echo "<div class='results'>";
$image = $row['afbeelding'];
$image_src = "images/".$image;
echo "<img class='selfiess' src='$image_src'>";
echo "<br>";
echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
echo "</div>";
}
//here is delete image code outside of loop
if (isset($_POST['removeSel'])){
$query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
$result2 = mysql_query($query);
header("Location: index.php?page=selfies");
}
answered Nov 12 at 10:52
Bilal Ahmed
3,25631335
3,25631335
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53260438%2fremoving-1-row-from-table-with-button-issue%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
Post your js code
– Saad Suri
Nov 12 at 10:44
you have write the delete query inside the while loop and also using
$row['ID']. that's mean all images will be deleted . you should write delete query outside the loop and get image id fromform submit– Bilal Ahmed
Nov 12 at 10:46
Possible duplicate of Can I mix MySQL APIs in PHP?
– Alon Eitan
Nov 12 at 10:46
The above dupe is for mixing
mysql_query+mysqli_queryinside theif, and why are you not deleting$_POST['removeSel']outside the loop instead of deleting all the records that you fetch– Alon Eitan
Nov 12 at 10:47
Show your html code and jquery or js code also.
– Leena Patel
Nov 12 at 11:04