Jump to content

Recommended Posts

Sorry guys, another quick question, but hopefully an easier one this time. I'm trying to use the script below to delete rows based on a link of the unique ID. For some reason, the last row is always deleted rather than the one I click on - I guess for some reason the ID is not being passed correctly to the query. Anyone have any suggestions?

 

<?php include 'connect.php'; ?>	
	<? 

//connect to mysql
//then connect as user
     mysql_connect($host,$user,$pass); 

   //select which database you want to edit
   mysql_select_db($db_name); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the members
$result = mysql_query("select id, Forename, Surname, CurrentGrade, LicenseExpiry, Phone, Mobile, Email from $table order by id");
   
   //print table headers
    $fields_num = mysql_num_fields($result);

echo "<h5>Table: {$table}</h5>";
echo "<div id=table><table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
    
}

echo "</tr>";
   
   //run the while loop that grabs all the Member Details
   while($r=mysql_fetch_array($result)) 
   { 
   $id=$r["id"];//take out the id
   $Forename=$r["Forename"];//take out the forename
   $Surname=$r["Surname"];//take out the surname
   $CurrentGrade=$r['CurrentGrade'];
   $LicenseExpiry=$r['LicenseExpiry'];
   $Phone=$r['Phone'];
   $Mobile=$r['Mobile'];
   $Email=$r['Email'];
  
     
    
    
 //make the ID & email a link
      echo "<tr><td><a href='delete.php?cmd=delete&id=$id'>$id</a></td>";
      echo "<td>$Forename</td>";
      echo "<td>$Surname</td>";
      echo "<td>$CurrentGrade</td>";
     echo "<td>$LicenseExpiry</td>";
      echo "<td>$Phone</td>";
      echo "<td>$Mobile</td>";
      echo "<td><a href='mailto:$Email'>$Email</a></td>";
      echo "</tr>";
      
    }
    echo "</table></div>";
}
?>


<?
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM $table WHERE id='$id'";
    $result = mysql_query($sql);
    header("Location: delete.php"); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/
Share on other sites

Please never use short tags to start PHP.

 

Your problem lies here.  You never retrieve the ID from the URL so $id is always that last one extracted from the database.  You need to change this portion:

 

 

if($_GET["cmd"]=="delete")
{
    $id = $_GET['id'];
    $sql = "DELETE FROM $table WHERE id='$id'";
    $result = mysql_query($sql);
    header("Location: delete.php");
}
?>

Brilliant, thanks Maq - that worked. I've not done any PHP before, so I guess by short tags you mean using <? instead of <?php?

 

If so, taken on board - and thanks again for the help  :)

 

PS - is there a "Solved" button here anywhere I can click?

 

 

Also related to your use (overuse) of php tags. Any white-space on a line before an opening <?php tag or any new-lines between a closing ?> tag and the next opening <?php tag will be output to the browser and will alter your page layout (spaces) and interfere with things like headers (new-lines).

 

The spaces in the following before the <?php and <? will be output to the browser -

   <?php include 'connect.php'; ?>   
      <?

and the one or two new-lines in the following will be output to the browser -

?>


<?

 

You should use a minimum number of opening and closing php tags (to avoid problems with content outside of the tags and to make your code look more professional.) Since the code you posted has no in-line/static HTML, one <?php tag at the start of the file and one ?> tag at the end of the file would be the best usage.

Cool, thanks for the tip - there is static html in the page itself, but I just posted the PHP above - however, there is definitely white space that I can remove.

 

 

 

Also related to your use (overuse) of php tags. Any white-space on a line before an opening <?php tag or any new-lines between a closing ?> tag and the next opening <?php tag will be output to the browser and will alter your page layout (spaces) and interfere with things like headers (new-lines).

 

The spaces in the following before the <?php and <? will be output to the browser -

   <?php include 'connect.php'; ?>   
      <?

and the one or two new-lines in the following will be output to the browser -

?>


<?

 

You should use a minimum number of opening and closing php tags (to avoid problems with content outside of the tags and to make your code look more professional.) Since the code you posted has no in-line/static HTML, one <?php tag at the start of the file and one ?> tag at the end of the file would be the best usage.

Ah OK, gotcha - bit of cleaning up to do then...  ;)

 

He's saying instead of this (which is not consistent anyway because you use <?php and <? tags):

 

   <?php include 'connect.php'; ?>   
      <?

 

Use:

 

   <?php include 'connect.php';

 

Now you don't have to  close PHP after the include and re-open which is pointless...

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.