• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Code Extension Needed

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

0
error77
Asked:
error77
  • 7
  • 6
  • 2
1 Solution
 
PranjalShahCommented:
What data you want to edit or delete...
0
 
error77Author 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

0
 
PranjalShahCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
leakim971PluritechnicianCommented:
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

0
 
error77Author Commented:
I'll try it out...thanks :)
0
 
leakim971PluritechnicianCommented:
Simple client side :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
<title></title>
<link rel="stylesheet"  href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<link rel="stylesheet" href="jquery.ui.datepicker.mobile.css" /> 
<script src="jQuery.ui.datepicker.js"></script>
<script src="jquery.ui.datepicker.mobile.js"></script>
<script>
	//reset type=date inputs to text
	$( document ).bind( "mobileinit", function(){
		$.mobile.page.prototype.options.degradeInputs.date = true;
	});	
</script>
<script language="javascript">
	$(document).ready(function() {
		$("input[type='button']").click(function(e) {
			$.post("edit%20delete%20read.php", {"action":$(this).val(),"id":$("#id").val(),"date":escape($("#date").val()),"textarea":encodeURIComponent($("#textarea").val())}, function(message) {
				alert(message.status);
				for(var i=0;i<message.data.length;i++) {
					$("#id").val( message.data[i].id );
				}
			});
		});
	});
</script>
</head> 
<body> 
<div data-role="page" data-theme="b" id="jqm-home">
    <div id="jqm-homeheader">
    <h1>Some title here</h1>
</div>
<div data-role="content">
<form action="#" method="POST">
    <div data-role="fieldcontain">
        <label for="date">Date Input:</label>
        <input type="date" name="date" id="date" value="" /><br /><br />
        <label for="textarea">Event:</label>
        <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
        <input type="button" value="save" />
        <input type="button" value="delete" />
        <input type="hidden" name="id" id="id" value="0" /><br /><br />
    </div>		
</form>
</div>
</div>
</body>
</html>

Open in new window

0
 
error77Author 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/
0
 
leakim971PluritechnicianCommented:
the name of the php file is : edit delete read.php
check on line 22
0
 
error77Author Commented:
How do I use this code with mine?
0
 
leakim971PluritechnicianCommented:
rename or change the filename somewhere
0
 
error77Author 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?
0
 
leakim971PluritechnicianCommented:
You're are the one to know what you want to do
0
 
error77Author Commented:
I'll try to get it together. Thanks for your code.
0
 
error77Author Commented:
Thanks again
0
 
leakim971PluritechnicianCommented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now