Page 1 of 3

How to edit and delete the selected row

Posted: Wed Sep 03, 2008 5:59 am
by zplits
Hi everyone. Can anyone help me? I know how to insert data into the database, and i know how to make it appear in the tables. But my problem is, i don't how to edit a specific record or delete a specific record when the user click the button.

here is what i mean. When i click the save button, the newly saved record in the database will be displayed just below Code 2001-1. I know how to make it appear in the table. But my prob is, how am i be able to edit the entire row if i click its corresponding edit button. And how am i be able to delete the entire row when i click its corresponding delete button?
Image

Anyone who knows it? Please help me.
Thanks in advance.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 6:51 am
by Sindarin
You have an auto_increment id column I assume, right?

What I do in my news system is do a while loop. Each time it also gets the id value. It returns it in the edit/delete link along with the corresponding action,

Code: Select all

$db_fetch=mysql_query($db_select,$db_connection);
 
//start retrieving entries from the database
while($row = mysql_fetch_array($db_fetch))
  {
  $newsid=$row['news_id'];
  $newstitle=$row['news_title'];
  $newscontent=$row['news_content'];
  $newstags=$row['news_tags'];
  $newsimage=$row['news_thumbnail'];
 
  $maxchars=300;
  if (strlen (($newscontent)) > $maxchars)
  {
  $newscontent=substr($newscontent, 0, $maxchars);
  $newscontent=$newscontent." ...<a href='edit.php?id=$newsid'><br/>More...</a>";
  }
 
  //return news in HTML form
  echo "
  
<table width='846' height='324' border='0' cellpadding='0' cellspacing='0'>
  <tr>
    <td height='49' align='left' valign='top'><img src='files/images/layout/cms-table-top.png' width='981' height='49' /></td>
  </tr>
  <tr>
    <td height='223' align='left' valign='top' background='files/images/layout/cms-table-middle.png'><table width='971' border='0'>
      <tr>
        <td width='14' align='left' valign='top'>&nbsp;</td>
        <td width='75' align='left' valign='top'><strong>ID: $newsid</strong></td>
        <td width='663' align='left' valign='top'><strong>$newstitle</strong></td>
        <td width='191' align='left' valign='top'><a href='edit.php?id=$newsid'><img src='files/images/layout/cms-edit.png' border='0' alt='Edit' /> EDIT</a> <a href='show.php?action=confirmdelete&id=$newsid&title=$newstitle'><img src='files/images/layout/cms-delete.png' border='0' alt='Delete' /> DELETE</a></td>
      </tr>
 
 
 
Then on top of my page in a switch I detect e.g. that the action=confirmdelete, I get (using $id=$_GET[id];) the selected article's id so I show a warning and provide a delete link to that article,

Code: Select all

 
case 'confirmdelete':
$id=$_GET[id];
$title=$_GET[title];
echo "<br/><left><img src='files/images/layout/cms-alert.png' border=0 alt='Success' /><font color='red'>  Do you really want to delete article with ID <b>$id and Title $title</b>?</font> <a href='show.php?action=delete&id=$id&title=$title'>Yes</a> | <a href='show.php'>No</a></left><br /><br />";
 
break;
 
case 'delete':
//DELETE ENTRY
 
$id=$_GET[id];
$title=$_GET[title];
 
//find and delete article
$db_delete="DELETE FROM news WHERE news_id=$id";
 
if (!mysql_query($db_delete,$db_connection))
  {
  die("<font color='red'><img src='files/images/layout/cms-error.png' />Error</font>: " . mysql_error());
  }
echo "<left><img src='files/images/layout/cms-ok.png' border=0 alt='Success' /><font color='green'>  News article with ID <b>$id</b> and Title <b>$title</b> was successfully deleted!</font></left><br /><br />";
 
break;
 
Hope it helps.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 6:54 am
by zplits
Thanks sindarin. here is my code below

Code: Select all

<?php 
$query = 'Select supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost from tbl_supplies';
$result = mysql_query($query) or die('Error in Query');
 
echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" >";
 
    
if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
        echo "<tr onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";  
        echo "<td width = \"60\" >".$row['code']."</td>";
        echo "<td width = \"140\">".$row['name']."</br></td>";
        echo "<td width = \"78\" align = \"right\">".$row['quantity']."&nbsp;</br></td>";
        echo "<td width = \"112\">&nbsp;".$row['measurement']."</br></td>";
        echo "<td width = \"116\" align = \"right\">".$row['perUnitCost']."</br></td>";
        echo "<td width = \"89\" align = \"right\">".$row['totalCost']."</br></td>";
        echo "<td width = \"90\" align = \"center\"><input name=\"Reset\" type=\"submit\" class=\"gridButtons\" id=\"button\" value=\"Edit\" onmouseover=\"this.style.color = '#000000'\" onmouseout=\"this.style.color = '#666666'\"/><input name=\"Reset\" type=\"submit\" class=\"gridButtons\" id=\"button\" value=\"Delete\" onmouseover=\"this.style.color = '#000000'\" onmouseout=\"this.style.color = '#666666'\"/></td>";
        echo "</tr>";       
    }
    echo "</table>";        
}
else{
    echo 'No rows found';
}?>

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:13 am
by zplits
I have made a few changes in the page. Instead of a button edit and delete i have changed it into a normal link. Here is what it looks like
Image

And here is my code:

Code: Select all

$query = 'Select supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost from tbl_supplies';
$result = mysql_query($query) or die('Error in Query');
 
echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" >";
 
    
if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
        echo "<tr onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";  
        echo "<td width = \"60\" >".$row['code']."</td>";
        echo "<td width = \"140\">".$row['name']."</br></td>";
        echo "<td width = \"78\" align = \"right\">".$row['quantity']."&nbsp;</br></td>";
        echo "<td width = \"112\">&nbsp;".$row['measurement']."</br></td>";
        echo "<td width = \"116\" align = \"right\">".$row['perUnitCost']."</br></td>";
        echo "<td width = \"89\" align = \"right\">".$row['totalCost']."</br></td>";
        echo "<td width = \"90\" align = \"center\"><a href ='supplies.php?mode=edit&id='".$row['supplies_PK']."'>&nbsp;Edit&nbsp;</a>&nbsp;<a href ='supplies.php?mode=delete&id='".$row['supplies_PK']."'>&nbsp;Delete&nbsp;</a>";
        echo "</tr>";       
    }
    echo "</table>";        
}
else{
    echo 'No rows found';
}
I don't know what to do next. I mean when i clicked the edit or delete button. Should i create a new page where it enables the user to edit, and another page to confirm deletion. I'm quite confuse on how this line of code speaks of 'supplies.php?mode=edit&id='".$row['supplies_PK'] . If i'll create a page for editing and deleting what will be the filename of the file?

Help me pls. I'm stuck on this. Hope some enlightenment from you. Thanks in advance.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:20 am
by Sindarin
I see you don't have a column in your table that it's auto incremented and serves as an id, you should add one and make it as the primary key of your table.

Then in your while loop,

Code: Select all

$id=$row['id']; //gets the current row id value from database
echo '<a href="delete.php?id=$id">Delete</a>';//echo the delete link with the current row id and go delete.php, and in there GET the id value at the start of the script

Code: Select all

//alternative //(edited)
echo '<a href="supplies.php?action=delete&id=$id">Delete</a>';//make a switch and check for when action is 'delete' GET the id value at the start of the script and delete entry with id: Sid
on another note, too much echoes. :o

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:24 am
by zplits
Sir sindarin, I have an primary key in my table, it's supplies_PK. Can you please guide me? I'm really new to this, as to what you can see. My scripts are really basic. Please???

Mind if we chat? If you don't just click my sig, it will load the chatroom. Thanks in advance, hoping for a positive response.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:36 am
by Sindarin
The chat is having issues for me.

Here try something,

this switch before you start the while loop,

Code: Select all

switch($_GET['action']){
//detect and delete entry goes here
case 'delete':
//get the supply's id
$deleteid=$_GET[id];
echo "I would delete supply with ID:$deleteid but I'll leave it up to you!<br/>";
//end of delete entry
break;
default:
//nothing to be done here
}
and this code in your while loop (this will display the delete link),

Code: Select all

$id=$row['supplies_PK'];
echo '<a href="supplies.php?action=delete&id=$id">Delete</a>';
Tell me the results

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:42 am
by zplits
ohh, i'm sorry sir :) Ahmm...Where should i put you code? the switch one, the name of the file that's displayed as an image is named as supplies.php....Where should i put the switch code? in a new page? or in the supplies.php?

And if create a delete page, what name should i give? I'm sorry sir, I'm confuse in the line of code

Code: Select all

echo '<a href="supplies.php?action=delete&id=$id">Delete</a>';

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:44 am
by Sindarin
Both go in your supplies.php page. :D

here this should work:

Code: Select all

 
 
switch($_GET['action']){
//detect and delete entry goes here
case 'delete':
//get the supply's id
$deleteid=$_GET[id];
echo "I would delete supply with ID:$deleteid but I'll leave it up to you!<br/>";
//end of delete entry
break;
//detect and edit entry goes here
case 'edit':
//get the supply's id
$editid=$_GET[id];
echo "I would edit supply with ID:$editid but I'll leave it up to you!<br/>";
//end of edit entry
break;
default:
//nothing to be done here
}
 
$query = 'Select supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost from tbl_supplies';
$result = mysql_query($query) or die('Error in Query');
 
echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" >";
 
   
if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
        $id=$row['supplies_PK'];
        echo "<tr onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">"; 
        echo "<td width = \"60\" >".$row['code']."</td>";
        echo "<td width = \"140\">".$row['name']."</br></td>";
        echo "<td width = \"78\" align = \"right\">".$row['quantity']."&nbsp;</br></td>";
        echo "<td width = \"112\">&nbsp;".$row['measurement']."</br></td>";
        echo "<td width = \"116\" align = \"right\">".$row['perUnitCost']."</br></td>";
        echo "<td width = \"89\" align = \"right\">".$row['totalCost']."</br></td>";
        //edit and delete added here
        echo '<td width = \'90\' align = \"center\'><a href="supplies.php?action=edit&id=$id"> Edit</a> | <a href="supplies.php?action=delete&id=$id">Delete</a>';
        //end of it
        echo "</tr>";      
    }
    echo "</table>";       
}
else{
    echo 'No rows found';
}
 

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 8:58 am
by zplits
Thanks sir. but i get an error. here is what caused the error.

Code: Select all

echo "<td width = \"90\" align = \"center\"><a href =\"supplies.php?action=edit&id=$id\">&nbsp;Edit&nbsp;</a>";
What have i done wrong?

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 9:05 am
by zplits
i mean this one sir

Code: Select all

echo '<td width = '90' align = \"center'><a href="supplies.php?action=edit&id=$id"> Edit</a> | <a href="supplies.php?action=delete&id=$id">Delete</a>';
that causes the error.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 9:13 am
by zplits
WOW sir sandarin. It worked out fine. I have fix the previous question. instead of

Code: Select all

echo '<td width = '90' align = \"center'><a href="supplies.php?action=edit&id=$id"> Edit</a> | <a href="supplies.php?action=delete&id=$id">Delete</a>';
I've changed it into

Code: Select all

echo "<td width = \"90\" align = \"center\"><a href=\"supplies.php?action=edit&id=$id\"> Edit</a> | <a href=\"supplies.php?action=delete&id=$id\">Delete</a>";
Now it really works. When i click the edit it displays a text saying "I would edit supply with ID:1 but I'll leave it up to you!" and when i click delete "I would delete supply with ID:1 but I'll leave it up to you!". Now that is working. I want to create an edit page where there he will be able to edit and update the record, and a delete page where he will ask for the confirmation to delete it or not. Can you provide me a code in the switch statement? Thank you so much for you help sir sandarin. I really appreciate it.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 9:19 am
by Sindarin
Hahaha it's Sindarin, and stop calling me sir, you make me feel like a knight. :mrgreen:

Well I include an edit.php file myself and get the existing values from the database to fill in the fields. It won't be hard to do so.
Yet I am still in the process of making my cms so I research all ways to find the best one.

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 9:25 am
by zplits
opsss. typo...Sorry Sindarin. If i create a edit.php, how will i put inside the switch statement? That when i click the edit button, the edit.php page will load.

Oh, CMS? wow...It's difficult. I wish i can help you, but as to what the status now. :) you know what i mean. I'm just new in php, i focus more on flash. :) want a sample of what i have created in flash?

Re: How to edit and delete the selected row

Posted: Wed Sep 03, 2008 9:40 am
by Sindarin
include or require will do the job,

require_once('yourpage.php'); //includes a php, html document into your current page.

so it would be like,

Code: Select all

switch($_GET['action']){
//detect and delete entry goes here
case 'delete':
//get the supply's id
$deleteid=$_GET[id];
require_once('delete.php');
exit;
//end of delete entry
break;
Oh, CMS? wow...It's difficult.
Not so hard when you actually start doing it.
want a sample of what i have created in flash?
shoot.