How can i update records in database with php

Hello PHP Buddies,

I’m trying, i am really trying, to make an update button work

this is what i got so far, so delete works fine and the view i know how to make it work I dont know how to brings the texbox and update
[php]
if ((isset($_GET[‘cmd’])) && (isset($_GET[‘ID’])))
{
include(’…/…/connect_db.php’);
//$header = $_SESSION[md5(“search”)];

if 	($_GET['cmd'] == "view")
{
echo "view";
}elseif	($_GET['cmd'] == "update")
{
echo "update";
}elseif	($_GET['cmd'] == "delete")
{
mysql_query("DELETE FROM `programming_language_snippet_codes_vaults` WHERE `ID` ='".$_GET['ID']."'") or die("Failed to delete Record with ID: ".$_GET['ID']."<br/>".mysql_error());

}

}
[/php]

how can i make the update button work like wehn i click the lil pencil it brings a textbox for each field and let me change the text and save it back

the page im tryin tto make this work looks like this

NOTE: right now when i click update image i see update at the top of the page that is good i put it intentionally to debug everything working

when deleting data you don’t need any kind of view but you do when you want to edit any data so you really want to go to an edit page when updating a record also don’t pass raw data to your db secure the data first, use mysql_real_escape_string http://php.net/manual/en/function.mysql-real-escape-string.php

I’ll give you an example of creating an edit facility.

First you may have a list of records that you want to be able to update something like:

[php]
$q = mysql_query(“SELECT * FROM table”);
while($r = mysql_fetch_object($q)){
echo “

$r->title Edit


}
[/php]

this will show the title and create an edit link next to the title in the link I have edit.php? then id=$r->id
the ?id will become a variable on edit.php its a way to pass the id to the edit page.

To get the id on edit.php:

[php]$id = mysql_real_escape_string($_GET[‘id’]);[/php]

then pull the record from the db

[php]
$sql = mysql_query(“SELECT * FROM table WHERE id=’$id’”)or die(mysql_error());
$r = mysql_fetch_object($sql);
[/php]

next write a form i’ve set the action to be empty so when the form is submitted it will reload the same page so it can be processed using the same file, some people prefer to use a separate file for processing.

[php]

Title

[/php]

I’ve included an inout with a type of hidden so you won’t see this but its there its purpose is to hold the id of the record which you need when updating the record the name of the inout field will become a variable when the form is submitted and its data comes from the value attribute.

To process to form check if the form has been submitted then collect the form data then do the update

[php]
//check if form has been submitted
if(isset($_POST[‘submit’)){

//secure form data and add to vars 
$id = mysql_real_escape_string($_POST['id']);
$title = mysql_real_escape_string($_POST['title']);

//update db
mysql_query("UPDATE table SET title='$title' WHERE id='$id'")or die(mysql_error());

//inform success
echo '<h1>Updated!</h1>';

}
[/php]

To put it all together: edit.php

[php]

//connect to database first

//check if form has been submitted
if(isset($_POST[‘submit’)){

//secure form data and add to vars 
$id = mysql_real_escape_string($_POST['id']);
$title = mysql_real_escape_string($_POST['title']);

//update db
mysql_query("UPDATE table SET title='$title' WHERE id='$id'")or die(mysql_error());

//inform success
echo '<h1>Updated!</h1>';

}

//get the id then pull that record from the db
$id = mysql_real_escape_string($_GET[‘id’]);
$sql = mysql_query(“SELECT * FROM table WHERE id=’$id’”)or die(mysql_error());
$r = mysql_fetch_object($sql);

//now show a form
?>

Title

[/php]

I’ve written a tutorial on this goes more in depth also has form validation too might be helpful http://daveismyname.com/tutorials/php-tutorials/edit-data-from-a-database/

Hello Dave,

thank you for the reply.

I thought about doing it on a new page and it seems easy i think i can do it with no hassle however i want to do it that when i click an update which is the lil pencil see image below

when i click that pencil i should be able to update it right there, so all of content should loaded into texboxes and i shouldbe able to save changes and the update image will change to save. i want it to be this way because it will look more fancy to me.

however if this is something that is hard to do i would do it the way you suggested

Thanks,
Wilson B

NOTE: that the second record it is how it should look like when update image is clicked.

you could do it on that page by having an if statement that looks for the id of the record if its there then show an editable box

[php]if($row->id = $_GET[‘id’]){
//show form
}[/php]

the downside is if you have a lot of entries on a page when the page reloads after click the pencil icon you have to scroll down to the textbox or you could setup an anchor as part of the link.

Another way is to use jquery and edit the content in place as soon as you click on it, have a look at http://www.appelsiini.net/projects/jeditable

Thanks dave i will postback with whatever i decided to use

Hey Dave, I got everything working now Thanks,

one more thing i want to have javascript message box so i can confirm delete request

http://www.phphelp.com/forum/index.php?topic=17813.0

Sponsor our Newsletter | Privacy Policy | Terms of Service