USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART III/V)

COMPLICATING ALL THIS A LITTLE 🙂

Well.. we added records and retrieved them… now will delete some of them.

I’ve avoided to mention some things to keep this as simple as possible, but I can’t keep hiding certain fundamentals, so…

Our SQL server is doing some kind of magic, that consist of automatically storing an unique id on each record, in a special column that I’ve created for that.

Such column (as you can imagine) is called ‘id’.

This column (field) has the ‘AUTO_INCREMENT’ attribute and besides that, it is a primary key (we need that at least one of our columns be a primary key, because a lot of reasons).

So, each time that we add a record (on our sample, using the INSERT SQL command) the ‘id’ field is automatically filled with an unique new value that serves the purpose of identify a record (row) univocally.

This column (id) is retrieved and stored in the first grid column of our HMG example (or the first column of our table in the JMG example).

We will use this ‘id’ value to identify the record to be deleted.

THE SERVER PROCEDURE

This procedure requires only one parameter (the id of the record to be deleted):

 <?php
 
 $id = $_REQUEST["id"];

 $db = mysqli_connect( '127.0.0.1', 'root', '');

 if( ! $db ) :
 die( mysql_error() );
 endif;

 if( ! mysqli_select_db($db, 'test')):
 die( mysql_error() );
 endif;

 $sql = "DELETE FROM people WHERE Id=".$id;
 
 $query = mysqli_query( $db, $sql );

 if( !$query ) :
 die("error SQL");
 endif; 

 mysqli_close($db);

  echo "Ok";

?>

THE HMG (WINDOWS DESKTOP) CLIENT CODE Simply, we get the record id, stored in the first column of the selected grid record and… delete it!

Function main_delete_record_action

 // Get Selected Grid Row

 I := Main.Grid_1.Value
 
 IF I == 0
 RETURN
 ENDIF
 
 // Get Id of record to be deleted
 
 cId := Alltrim(Main.Grid_1.Cell(I,1))

 // Lets assume the server are local
 // and the server procedures are in the 'php' subfolder.

 cServer := '127.0.0.1'
 cGet := 'http://' + cServer + '/php/delete.php'

 // Add parameters (Id of record to delete)

 cGet += '?'

 cGet += 'id=' + cId

 // Call remote server procedure with required parameters

 oHttp:= TIpClientHttp():new( cGet )

 // Check for error
 
 IF .NOT. oHttp:open()
 MsgStop ( "Connection not available!" )
 oHttp:close()
 Return
 EndIf

 // Get returned data

 cData := oHttp:readAll()

 oHttp:close() 

 MsgInfo(cData)
 
 // Update grid.
 
 main_get_data_action()
 
Return Nil

THE JMG CLIENT CODE (IF YOU WANT A MOBILE APP TOO): 

function ajaxDelete()
{ 

// In our JMG sample, the retrieved server data
// is on 'table2' widget

 // If no rows selected the action will be canceled.

 if ( get('table2','selectedRowCount') == 0 )
 {
 alert('No Row Selected!');
 return;
 }

 // We want that only one row be selected for deletion at a time!

 if ( get('table2','selectedRowCount') > 1 )
 {
 alert('You must select only one row!');
 return;
 }

 // Getting the 'id' (column 1) of the selected row.

 var selectedRows = get('table2','selectedRows');

 var selRowNumber = selectedRows[0];

 var delete_id = get('table2','cell',selRowNumber,1) ;

 // With the 'id' we call the delete.php server procedure.

// we assume that server is local
 var server = '127.0.0.1'

 var request = $.ajax( { url: "http://" + server + "/php/delete.php" , data: { id: delete_id } } );

 request.done(function( data )
 {
 ajaxRead();
 alert('Selected Row Deleted!');
 });

 request.fail(function(jqXHR, textStatus)
 {
 alert( "Request failed: " + textStatus );
 });

}

TO BE CONTINUED (update records and FULL WORKING CODE!)

You can discuss about this post on the HMG SourceForge forum Here

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART III/V)

  1. esgici says:

    Thanks Maestro 😀

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s