Recent

Author Topic: Please Help With this Code  (Read 5922 times)

shonay

  • Full Member
  • ***
  • Posts: 169
Please Help With this Code
« on: January 31, 2015, 10:13:45 am »
Ok I have a Code i have been playing with, currently i am studying Wininet and i want to send the parameters by GET request so my php can save it in the database.
Code Looks like this

Code: [Select]
program InfoWininet;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes , Windows , Wininet
  { you can add units after this };

var
  FirstName : string;
  LastName: string;
  Email: string ;
  Sex : string;
  IDNumber: string;
  IOpen, IURL: HINTERNET;
  Read: Cardinal;
  URL : string;
  data: string;
  Result : string;

begin
    Writeln('Enter Your First Name: ');
    Readln(FirstName);
    Writeln('Enter your Last Name: ');
    Readln(LastName);
    Writeln('Enter Your Email: ');
    Readln(Email);
    Writeln('Enter your ID Number: ');
    Readln(IDNumber);

    data := URL + 'data.php?fname=' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;
   begin
Result:='';
try
IOpen := InternetOpen('Oxysys',INTERNET_OPEN_TYPE_PRECONFIG, '', '',INTERNET_FLAG_NEED_FILE);
  if IOpen<>nil then
    try
IURL:= InternetOpenUrl(IOpen, 'localhost', nil, 0,INTERNET_FLAG_DONT_CACHE, 0);
if IURL<> nil then
try
  SetLength(data,4096);
repeat
if InternetReadFile(IURL,@data[1],4096,Read) then
Result:=Result + Copy(data,1,Read)
else
Break;
    until Read = 0;
finally
  InternetCloseHandle(IURL);
end;
finally
  InternetCloseHandle(IOpen);
        end;
          except
    end;
end;
end.


Errors i gat looks like this, i want it to output ''Message transfered" i can code a script to save in db using php, but what is wrong with this code

Code: [Select]

InfoWininet.lpr(34,13) Warning: Variable "URL" does not seem to be initialized
InfoWininet.lpr(46,45) Hint: Variable "Read" does not seem to be initialized
InfoWininet.lpr(57,1) Fatal: Syntax error, "EXCEPT" expected but "END" found

« Last Edit: January 31, 2015, 10:19:39 am by shonay »
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

hy

  • Full Member
  • ***
  • Posts: 221
Re: Please Help With this Code
« Reply #1 on: January 31, 2015, 10:20:56 am »
As the last error message states you need to insert
Code: [Select]
except
   <something>
   end;
or
Code: [Select]
finally
   <something>
   end;
before the last end.
_____
***hy
OS: debian sid(64bit)  [fpc 3.20] Lazarus 2.0.12

balazsszekely

  • Guest
Re: Please Help With this Code
« Reply #2 on: January 31, 2015, 10:22:49 am »
I'm not trying to by rude or anything, but you should learn the basics first.  If you cannot figure out what is wrong with that code, you're not ready to do socket programming(which is not easy by the way)

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please Help With this Code
« Reply #3 on: January 31, 2015, 10:25:56 am »
@Getmem, i know where the errors are now, I fixed it and it compiles but i dont seem to see anything in the php database and thats why am complaining ok?

@hy, Could you be of help, Further, Please
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please Help With this Code
« Reply #4 on: January 31, 2015, 10:43:11 am »
ok my code looks like this now

Code: [Select]
program InfoWininet;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes , Windows , Wininet
  { you can add units after this };

var
  FirstName : string;
  LastName: string;
  Email: string ;
  IDNumber: string;
  IOpen, IURL: HINTERNET;
  Read: Cardinal;
  localhost : string;
  data: string;
  Result : string;

begin
    Writeln('Enter Your First Name: ');
    Readln(FirstName);
    Writeln('Enter your Last Name: ');
    Readln(LastName);
    Writeln('Enter Your Email: ');
    Readln(Email);
    Writeln('Enter your ID Number: ');
    Readln(IDNumber);

    data := localhost + 'data.php?fname=' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;
   begin
Result:='';
try
IOpen := InternetOpen('Oxysys',INTERNET_OPEN_TYPE_PRECONFIG, '', '',INTERNET_FLAG_NEED_FILE);
  if IOpen<>nil then
    try
IURL:= InternetOpenUrl(IOpen, 'localhost', nil, 0,INTERNET_FLAG_DONT_CACHE, 0);
if IURL<> nil then
try
  SetLength(data,4096);
repeat
if InternetReadFile(IURL,@data[1],4096,Read) then
Result:=Result + Copy(data,1,Read)
else
Break;
    until Read = 0;
finally
  InternetCloseHandle(IURL);
end;
finally
  InternetCloseHandle(IOpen);
        end;
          except
    end;
          Writeln('Message Sent Successfully...');
          Readln;
end;
end.

[/code ]

Am i getting it correctly? What am i not getting correctly? i dont seem to see any errors now , just warnings.  this is the only way i get to learn here, so please no one should get pissed.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

balazsszekely

  • Guest
Re: Please Help With this Code
« Reply #5 on: January 31, 2015, 01:53:45 pm »
Quote
@Getmem, i know where the errors are now, I fixed it and it compiles but i dont seem to see anything in the php database and thats why am complaining ok?

I really think, you should learn the basics first... from now on, I will shut up! Here you go:
https://theroadtodelphi.wordpress.com/category/wininet/

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Please Help With this Code
« Reply #6 on: January 31, 2015, 03:30:27 pm »
First try debugging your code !!

When looking at your code the first thing I notice is that you don't initialize your variables. (Like what value has your variable localhost ???)

Second, what do you do with your variable data? You do an InternetOpenUrl on just "localhost" (without specifying a page) and later on you read it's content in the variable data (overwriting the value you set earlier without using it). I can't imagine that's what you meant to do.

I think what you were meant to do is a InternetOpenUrl with data (= url) as parameter. But you need to build it properly. It should be something like Data := 'http://localhost/data.php?fname=' + ....etc and then InternetOpenUrl(IOpen, data, ... etc
« Last Edit: January 31, 2015, 03:32:26 pm by rvk »

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please Help With this Code
« Reply #7 on: January 31, 2015, 07:53:35 pm »
Ok My code Looks like this now

Code: [Select]
program InfoWininet;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes , Windows , Wininet
  { you can add units after this };

var
  FirstName : string;
  LastName: string;
  Email: string ;
  IDNumber: string;
  IOpen, IURL: HINTERNET;
  Read: Cardinal;
  data: string;
  Result : string;
  http : HINTERNET;

begin
    Writeln('Enter Your First Name: ');
    Readln(FirstName);
    Writeln('Enter your Last Name: ');
    Readln(LastName);
    Writeln('Enter Your Email: ');
    Readln(Email);
    Writeln('Enter your ID Number: ');
    Readln(IDNumber);

    data := http://localhost/data.php?fname=' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;
   begin
Result:='';
try
IOpen := InternetOpen('Oxysys',INTERNET_OPEN_TYPE_PRECONFIG, '', '',INTERNET_FLAG_NEED_FILE);
  if IOpen<>nil then
    try
IURL:= InternetOpenUrl(IOpen, data, nil, 0,INTERNET_FLAG_DONT_CACHE, 0);
if IURL<> nil then
try
  SetLength(data,4096);
repeat
if InternetReadFile(IURL,@data[1],4096,Read) then
Result:=Result + Copy(data,1,Read)
else
Break;
    until Read = 0;
finally
  InternetCloseHandle(IURL);
end;
finally
  InternetCloseHandle(IOpen);
        end;
          except
    end;
          Writeln('Message Sent Successfully...');
          Readln;
end;
end.


See what my error now looks like
Code: [Select]
InfoWininet.lpr(33,13) Warning: Variable "http" does not seem to be initialized
InfoWininet.lpr(33,13) Error: Incompatible types: got "Pointer" expected "AnsiString"
InfoWininet.lpr(33,17) Fatal: Syntax error, ";" expected but ":" found


I have the http initialized and stuff, dont know whats wrong again..
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Please Help With this Code
« Reply #8 on: January 31, 2015, 08:37:59 pm »
    data := http://localhost/data.php?fname=' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;
You can't see what's wrong here? Not even with the warning that's given on that line?
You're forgetting the opening ' (string limiter) when assigning something to data.
It should be:
Code: [Select]
    data := 'http://localhost/data.php?fname=' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;

IURL:= InternetOpenUrl(IOpen, data, nil, 0,INTERNET_FLAG_DONT_CACHE, 0);
You didn't get an error here yet (because of the first one) but when fixing the first one this would be the second. Kind of my fault because I typed it out of my head and forgot to put pchar around data (which this function needs). So change it to:
Code: [Select]
IURL:= InternetOpenUrl(IOpen, pchar(data), nil, 0,INTERNET_FLAG_DONT_CACHE, 0);

After that it should work fine.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please Help With this Code
« Reply #9 on: January 31, 2015, 09:07:48 pm »
Many thanks RVK. Am most grateful

Another Question that demands clarity. I wanted to make use of a program that uses Resource for instance , now the first .exe file is loaded into the second viz resource ,  i wanted it to be something like a Builder and stub thing,
Now the stub is suppose to fetch the URL from the builder and then use the URL in its function as the case maybe.
if am to use the URL from the main builder file, do i do it like this

Code: [Select]
data := 'textBox1.text' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' + IDNumber;
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Please Help With this Code
« Reply #10 on: January 31, 2015, 09:21:31 pm »
data := 'textBox1.text' + FirstName + '&lastName=' + LastName + '&Email=' + Email +  '&IdNumber=' +
First of all. You need to know the difference between string and variable.
This is a string: `text`
This is a variable: variable
If you do this data := 'textBox1.text' you are assigning 'textBox1.text' to data.
I don't think that is what you meant to do. You probably want te CONTENT of textBox1.text assigned to data. In that case you would do
Code: [Select]
data := textBox1.text + ' plus more string stuff';(Do you see the difference?)

Quote
I wanted to make use of a program that uses Resource for instance , now the first .exe file is loaded into the second viz resource ,  i wanted it to be something like a Builder and stub thing.
In that case you want communication between the two programs. You can start the second .exe from the first with parameters. You could also use a .dll instead of a .exe. You can define a function in the .dll and load the dll and call that function from your main program. It all depends on why you want to separate the two. Why would you load a second .exe if it is in a resource? If you code both programs yourself it's much easier to just code all the functionality in one program.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please Help With this Code
« Reply #11 on: January 31, 2015, 09:27:34 pm »
@rvk, Thanks, you have been most helpful. I will continue my reading now. Thank you again.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

 

TinyPortal © 2005-2018