We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Code Extension Needed

error77
error77 asked
on
Medium Priority
275 Views
Last Modified: 2012-06-27
Hi all,

The following code lists records from a database.

Can anyone help extending the code so it also edit's and delete's please?

Thanks very much and code is below.
<?php
  try
   {
     $connection = mysql_connect("localhost","username","password");
     mysql_select_db("my-database", $connection);
     $result = mysql_query("SELECT * FROM my-table");
 
     while($row = mysql_fetch_array($result))
       {
         echo $row['date'];
	 echo $row['textarea'];
							
       }
 
         mysql_close($connection);
           }
             catch(Exception $e)
                {
                   echo $e->getMessage();
                }
  ?>

Open in new window

Comment
Watch Question

What data you want to edit or delete...

Author

Commented:
Data from a mysql db table.

If you look at the current code:

 while($row = mysql_fetch_array($result))
       {
         echo $row['date'];
       echo $row['textarea'];
                                          
       }

There are 2 fields that are displayed ... I need to edit / delete these fields too.

Hope this helps

You want to edit/delete all the data from that table at once? I will say choose the data you want to edit/delete and then do it on one particular data.
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Check this :

***CAUTION*** edit and delete will "update" all your data so ***CAUTION***
<?php

	// we need to send a JSON back
    $response = array();

    try
    {
        $connection = mysql_connect("localhost","root","offdalip");
        mysql_select_db("experts_exchange", $connection);

		if( isset( $_POST["action"] ) && isset( $_POST["id"] ) && isset( $_POST["date"] ) && isset( $_POST["textarea"] ) ) {

			$action = $_POST["action"];
	
			$id = $_POST["id"];
			$date = $_POST["date"];
			$textarea = $_POST["textarea"];
			
			if($action == "list") {
				$result = mysql_query("SELECT * FROM `my-table`",$connection);
				if($result) {
					while($row = mysql_fetch_array($result))
					{
						$response["data"] = array("id"=> $row["id"], "date"=> $row["date"], "textarea"=> $row["textarea"]);
					}
				}
				else {
					$response["data"] = array();
				}
			}
			else if($action == "save") {
				//$query = "UPDATE `my-table` SET date = '" . mysql_real_escape_string($date) . "',textarea = '" . mysql_real_escape_string($textarea) . "' WHERE id = " . mysql_real_escape_string($id);
				$query = "UPDATE `my-table` SET date = '" . mysql_real_escape_string($date) . "',textarea = '" . mysql_real_escape_string($textarea) . "'";
				$response["query"] = $query;
				$result = mysql_query($query,$connection);
				$response["data"] = array("id"=> mysql_insert_id());
				//$response["data"] = array();
			}
			else if($action == "delete") {
				//$query = "DELETE FROM `my-table` WHERE id = " . mysql_real_escape_string($id);
				$query = "DELETE FROM `my-table`";
				$response["query"] = $query;
				$result = mysql_query($query,$connection);
				$response["data"] = array("id"=> mysql_insert_id());
				//$response["data"] = array();
			}
		
			$response["status"] = "OK";
		}
		else {
			$response["status"] = "Missing parameter(s)";
		}
    }
    catch(Exception $e)
    {
	    $response["status"] = $e->getMessage() . "\n" . $query;
    }

	$json = json_encode($response);
	header("Content-type: application/json");
	echo $json;

?>

Open in new window

Author

Commented:
I'll try it out...thanks :)
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Hmmm.. really strange...I've saved the code and when I try to run the code firefox tries to download the .php file ... very strange thing :o/
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
the name of the php file is : edit delete read.php
check on line 22

Author

Commented:
How do I use this code with mine?
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
rename or change the filename somewhere

Author

Commented:
When I run your code I can see the fields and the Edit and Delete button but as there is not list it's not specified what I'm editing or deleting ... It's probably me not understanding what to do ..

Is what I'm saying making any sense?
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You're are the one to know what you want to do

Author

Commented:
I'll try to get it together. Thanks for your code.

Author

Commented:
Thanks again
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You're welcome! Good luck and I you allow me to comment, take your time to understand the code. It miss a lot of thing depending what you want to do, f
If you need infos about it, don't hesitate to post here.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.