Jump to content

Delete just the associated row from mysqli_fetch_array


chrono23

Recommended Posts

Hello,

  So I have only been using php for a few months now and am a noob. This question is probably an easy fix.

 

  My problem is that I have an array of info which is user stories they post on my site. I have a delete button for each story for the user to delete the associated story if they wish. The problem is that the delete button code deletes the last $row or story that was fetched! It seems that the variable updates through every loop and the code just deletes that id. Here is my code.

 

//page with delete button

echo "<center><b>Stories Shared</b></center>";
$result = mysqli_query($cxn, "SELECT * FROM Stories WHERE User = '$name' ORDER BY ID DESC");

while($row = mysqli_fetch_array($result))
{
if (empty($row))
{
echo "<center>N/A</center>";
}
$artpixname = $row['Artpixname'];
echo "<hr />";

echo "<div id = 'info'>";;
echo "ID: " . $row['ID'];
echo "<b>Category:   </b>" .$row['Type']. "   ";
$id = $row['ID'];
echo "<FORM ACTION='delete.php' METHOD='POST'>

<input type='hidden' name='ID' value='$id'>
<INPUT TYPE = 'Submit' name='Delete' id='Delete' VALUE = 'Delete'>";

echo "<center><b><h3>" . $row['Name'] . "</h3></b></center>   ";

echo "</div>";

   if (!empty($artpixname))
   {
   echo "<center><img class = 'artimg' src = 'articles/images/".$row['Artpixname']."'></center>";
   }
echo "<br />";
echo "<br />";
echo $row['Article'];
echo "<br />";
echo "<br />";

//now the delete button opens up the delete program here...

<?php
session_start();
include 'header.php';
include 'connection.php';

$id = $_POST['ID'];
$name = $_SESSION['logname'];


$result = mysqli_query($cxn, "SELECT ID FROM Stories WHERE ID = '$id'");

while($row = mysqli_fetch_array($result))
{
$sql = mysqli_query($cxn, "DELETE FROM Stories WHERE ID = '$id'");
}

echo "Your story was deleted successfully!";
echo "<br />";
echo "Click <a href = 'userpage.php?'.$name'><b>Here</b></a> to return to your profile page.";
?>

What am I doing wrong? I tried everything including different variables and even putting the delete query in the while loop. Any help would be appreciated thanks!

 

Lance

 

MOD EDIT:

 . . . 

tags added.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.