Sunday, August 2, 2009

What is the correct way to edit a database entry in PHP?

I fetch results with PHP by selecting a dates. I have a (lowerlimit) and (upperlimit). Ex.When i select Nov-2006, it displays all entries(journal) for the month of Nov in 2006 in a typical forum format.





I would like to add a %26lt;a href%26gt; tag at the top right of each result that comes back that would do the following. The a-href would direct the user to a different page say 'editentry.php?y=2004%26amp;m=12%26amp;d=14 . At the top of editentry.php, id use _GET to fetch that particular entry and enter the results into a form. The form would be identical to the original form that was used to enter the information into it.





So the user would have all those entries fields in plain view. The auto-increment-primary key field and the date field would be viewable but not editable.





Anywho, at the bottom of editentry.php when all the changes are made, there would be a "submit changes" button and a "delete entry" button.





What would the mysql command be to


a)resubmit


b)delete





tyvm

What is the correct way to edit a database entry in PHP?
Let's assume you have a table with four columns: The uniqueid field, a datetime field called mydate, a text field called myname and a text field called mytext.





From your menu page, you would simply list off the records in a table. A column on the left would be the edit link column, and a column at the right would be the delete column.





%26lt;?


'your connection info


$rs = mysql_query("SELECT * FROM mytable");





if(mysql_num_rows( $rs ) == 0) {


echo "No records";


}


else {


echo "%26lt;table%26gt;";


echo "%26lt;tr%26gt; %26lt;th%26gt;Edit%26lt;/th%26gt; %26lt;th%26gt;My Name%26lt;/th%26gt; %26lt;th%26gt;My Date%26lt;/th%26gt; %26lt;th%26gt;My Text%26lt;/th%26gt; %26lt;th%26gt;Delete%26lt;/th%26gt; %26lt;/tr%26gt;";


while($row = mysql_fetch_array($rs)) {


echo "%26lt;tr%26gt;";


echo "%26lt;td%26gt; %26lt;a href=\"edit.php?id=$row[uniqueid]\"%26gt; Edit%26lt;/a%26gt; %26lt;/td%26gt;";


echo "%26lt;td%26gt; $row[myname] %26lt;/td%26gt;";


echo "%26lt;td%26gt;" . date( 'F j Y g:i a', $row['mydate']) . "%26lt;/td%26gt;";


echo "%26lt;td%26gt; $row[mytext] %26lt;/td%26gt;";


echo "%26lt;td%26gt;%26lt;a href=\"delete.php?id=$row[uniqueid]\"%26gt; Delete%26lt;/a%26gt;%26lt;/td%26gt;";


echo "%26lt;/tr%26gt;";


}


echo "%26lt;/table%26gt;";


}


?%26gt;





This code calls on two pages: edit.php or delete.php. It uses the uniqueid column to know which record to edit.





Let's do delete.php first, because it's quick and dirty. All we'll do is run a SQL command to delete the record and kick the user back to the list page (which we'll call list.php). We don't need any HTML; just connect to the DB and let PHP do the work.





%26lt;?


'your connection script





if(!isset($_GET['id'])) {


header('Location: list.php');


}


else {


$sql = "DELETE FROM mytable WHERE uniqueid = $_GET[id]";


$rs = mysql_query($sql);


header('Location: list.php');


}


?%26gt;





Edit.php is a bit more complicated. We need to both get the current record, so we have something to edit, and update the record. We first check for page postback; if it is set, we go ahead and update the record. Then, we get a recordset corresponding to the same ID, and populate the form fields with the data:





%26lt;?


'your connection info





if(!isset($_GET['id'])) {


header('Location: list.php');


}





if(isset($_POST['submit'])) {


$sql = "UPDATE mytable SET myname = '$_POST[myname]', mydate = $_POST[mydate], mytext = '$_POST[mytext]' WHERE uniqueid = $_GET[id]";


$rs = mysql_query($sql);


}


$sql = "SELECT * FROM mytable WHERE uniqueid = $_GET[id]";


$rs = mysql_query($sql);


$row = mysql_fetch_array($rs);


?%26gt;


%26lt;html%26gt;


%26lt;head%26gt;%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;form method="post"%26gt;


Record ID: %26lt;? echo $row['uniqueid']; ?%26gt;



My Name: %26lt;input type="text" id="myname" value="%26lt;? echo $row['myname']; ?%26gt;" /%26gt;



My Date: %26lt;input type="text" id="mydate" value="%26lt;? echo $row['mydate']; ?%26gt;" /%26gt;



My Text: %26lt;input type="text" id="mytext" value="%26lt;? echo $row['mytext']; ?%26gt;" /%26gt;



%26lt;input type="submit" id="submit" value="submit" /%26gt;


%26lt;/form%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





Note that this code is not very secure and does not trap for errors. It's just illustrative. It works but it's not ready for prime time.
Reply:http://www.dougv.com/blog/2006... Report It

Reply:I am assuming that you are passing the row id of the item they are editing to your editentry.php and putting it into a hidden form field or embedding it directly into a form "action" URL (the address where your form is to be submitted to make changes... this could be submitting back to editentry.php or to another page it is up to you.)





So now they have hit either the update or delete button and it reloads the page with the row id and the new values that have changed. So simply all you would then do is issue an SQL update or delete according.





%26lt;?php





$action = $_POST['action'];


$id = $_POST['id'];





if (($action != null) %26amp;%26amp; ($id %26gt; 0)) {





if ($action == 'update') {


$sql = "Update table set column = $newfield, column2='$newfield2' where id = $id";


}





if ($action == 'delete') {


$sql = "Delete from table where id = $id";


}





mysql_query($sql);


}





?%26gt;





I just threw that out to give you a rough feel of the flow. That is how you would go about it. Again make sure to check your values for reasonableness / correct info before inserting into dynamic queries like this (to prevent SQL injection attacks).





Hope you get the idea out of that quick scribble. Good luck!


No comments:

Post a Comment