PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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?
Anyone who knows it? Please help me.
Thanks in advance.
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,
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,
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;
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.
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.
$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
//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.
Last edited by Sindarin on Wed Sep 03, 2008 8:29 am, edited 1 time in total.
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.
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),
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
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.
Hahaha it's Sindarin, and stop calling me sir, you make me feel like a knight.
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.
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?
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;