PUTTING IT ALL TOGETHER
The last data-handling procedures of this experiment, are about modifying records.
In fact, there is little new here, but, a lot of that we already done.
- We need to identify the record to modify (as in delete procedures).
- We need to handle parameters to hold the content of ‘first’ and ‘last’ fields.
The full new concept, will be the UPDATE, SQL command, that we will use (as you can imagine) to change the content of an existing record.
Now… our ‘update.php’ server procedure:
<?php
$id = $_REQUEST["id"];
$first = $_REQUEST["first"];
$last = $_REQUEST["last"];
$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 = "UPDATE people SET first='".$first."', last='".$last."' WHERE id=".$id;
$query = mysqli_query( $db, $sql );
if( !$query ) :
die("error SQL");
endif;
mysqli_close($db);
echo "Ok";
?>
HMG (WINDOWS DESKTOP) CLIENT CODE:
Function main_modify_record_action
// Get Selected Grid Row
I := Main.Grid_1.Value
IF I == 0
RETURN
ENDIF
// Get Id of record to be modified
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/update.php'
// Add parameters ('Id' of record to modify, 'first' and 'last')
// Please, consider that for real-world use, the
// parameters content, must be parsed (discussed in part V)
cGet += '?'
cGet += 'id=' + cId
cGet += '&'
cGet += 'first=' + alltrim ( InputBox('Enter Your First Name','Please...', Alltrim(Main.Grid_1.Cell(I,2)) ) )
cGet += '&'
cGet += 'last=' + alltrim ( InputBox('Enter Your Last Name','Thanks!', Alltrim(Main.Grid_1.Cell(I,3))) )
// 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
JMG (WEB/MOBILE APP) CODE
function ajaxEdit()
{
// On our JMG sample, the table holding our records, is
// called 'table2'.
// We want only one row selected for editing.
if ( get('table2','selectedRowCount') == 0 )
{
alert('No Row Selected!');
return;
}
if ( get('table2','selectedRowCount') > 1 )
{
alert('You must select only one row!');
return;
}
// Getting current values from table.
var selectedRows = get('table2','selectedRows');
var selRowNumber = selectedRows[0];
var edit_id = get('table2','cell',selRowNumber,1) ;
// Prompting users for new values.
var edit_first = prompt('Please Enter First:',get('table2','cell',selRowNumber,2));
var edit_last = prompt('Please Enter Last:',get('table2','cell',selRowNumber,3));
// Calling the update.php server procedure with the required parameters.
// We will assume our server local for this tutorial
var server = '127.0.0.1';
var request = $.ajax( { url: "http://" + server + "/php/update.php" , data: { id: edit_id , first: edit_first , last: edit_last } } );
// Finally, we define the callbacks.
request.done(function()
{
alert('Row Edit Complete!');
ajaxRead();
});
request.fail(function(jqXHR, textStatus)
{
alert( "Request failed: " + textStatus );
});
}
BUT… ALL THIS IS REALLY CONFUSING… 🙂
Well… I have bad news and good news… which do you want to know first?
The bad?… I agree! 🙂
The bad: This is a tutorial, created to keep things, as simple as possible… in a real-world situation, you must consider, a lot of other things!
The Good: In the next (and final) post, I’ll publish the full source code of the example apps, and give you some useful tips!
TO BE CONTINUED…
You can discuss about this post on the HMG SourceForge forum Here