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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.