Link to home
Start Free TrialLog in
Avatar of faimo
faimo

asked on

send articles to my DB over the internet

i have a DB ( made by mysql ) in my web site , so i want to be able to log-in and send articls from my PC , this DB contains 4 fields  ID,Categories , Title , Text , for the first time i used the Indy to send the new article to my DB i put 2 Tedits one servs to ID ( this will be automatically filled it gets it from the DB ( ID )  , and the other servs to Title , and one Tcombobox it servs to the categories available in my DB , so the user will pull down this Combobox and choose any category , a Tmemo servs to the body of this new article a button to send the new article to my DB , but it desn't work any one can suggest a solution to this problem , or any advice
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

It would be best to have a server-side script to insert your data into your database. Send a HTTP Post with
the data you want to insert and your script should do it for you.

Does your host support PHP? If so, I can write a quick script to do it for you.
This simple little PHP script worked for me in a test environment.


Delphi code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    ComboBox1: TComboBox;
    Memo1: TMemo;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure SendPostData;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function URLEncode(const ASrc: string): string;
const
  UnsafeChars = ['*', '#', '%', '<', '>', '+', ' ']; {do not localize}
var
  i: Integer;
begin
  Result := ''; {Do not Localize}
  for i := 1 to Length(ASrc) do begin
    if (ASrc[i] in UnsafeChars) or (ASrc[i] >= #$80) or (ASrc[i] < #32) then begin
      Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2); {do not localize}
    end else begin
      Result := Result + ASrc[i];
    end;
  end;
end;

procedure TForm1.SendPostData;
var
  aStream: TMemoryStream;
  Params: TStringStream;
begin
  aStream := TMemoryStream.create;
  Params := TStringStream.create('');

  try
    with IdHTTP1 do
    begin
      Params.WriteString(URLEncode('title=' + Edit1.Text + '&'));
      Params.WriteString(URLEncode('category=' + ComboBox1.Text + '&'));
      Params.WriteString(URLEncode('text=' + Memo1.Lines.Text));
      Request.ContentType := 'application/x-www-form-urlencoded';
      try
        Post('http://localhost/addarticle.php', Params, aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  aStream.Position := 0;
  Memo2.Lines.LoadFromStream(aStream);
  except
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendPostData;
end;

end.

PHP Script:
<?php
  /*
    #
    # Table structure for table 'articles'
    #
   
    CREATE TABLE articles (
      id int(20) NOT NULL auto_increment,
      category varchar(45) default NULL,
      title varchar(45) default NULL,
      text longtext,
      PRIMARY KEY  (id)
    ) TYPE=MyISAM;
   
  */

  /*
  These are your database conenction info, change it to match your info
  */
  $dbhost= 'localhost';
  $dbuser='root';
  $dbpw='';
  $dbname='test';
  $tablename='articles';

  mysql_connect($dbhost, $dbuser, $dbpw);
  mysql_select_db($dbname);
 
  $category = $_POST["category"];
  $title = $_POST["title"];
  $text = $_POST["text"];
  if (isset($category)) {
    $sql = "insert into ".$tablename." values (0, '".$category."', '".$title."', '".$text."')";
    mysql_query($sql) or die(mysql_error());
    echo "Record successfully entered";
  } else {
    die("No category");
  }
?>
Avatar of faimo
faimo

ASKER

first of all thank you for your answer ,
second yes my host supports PHP ,
why should i creat this table as i have it in my DB its name news , and all the fields are made so all what i need is to be able to fill this DB with new articls from my App ( PC ) ,
and the PHP scripts where should i put them in my App source or where ... please could you explaine more your exemple  

Hi

On your PC, set up an ODBC Connection.  Instead of your Server name, put in the IP Address of the MYSQL Server, (your ISP will provide this, the database name that your ISP has given you and your login details).

Then, when you have an Internet Connection you can connect to your database with any desktop apllication where you could normally access a local MYSQL database - easy.

Remember, the internet is simply an extension of your local network. Set up a MYSQL database on your network (its free after all) and test it with your app.  You should also download the Manager Utitlity to manage your server and use that to test connectivity to your Server on the Internet.

Voodooman
Avatar of faimo

ASKER

could you explaine more please
ASKER CERTIFIED SOLUTION
Avatar of Voodooman
Voodooman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"opening your MYSQL on the Internet with the MYSQL Manager using the IP address"

Most times, server admins will not allow this type of access. I'd also be very careful
of this type of access, as well, as it can be abused if you do not take adequite security
precautions.

Well Eddie if you have an SQLServer database on the Internet that is exactly how your ISP will tell you to open it - using tha Admin tool over I.P.

Name your 'Adequate Security Precautions' please.

Do you think people are really analysing your IP packets now, even as we speak.  After all you connect to your ISP over IP don't you?

'Most times, server admins will not allow this type of access.' Please prove this.


Voodooman
They do not allow access to your DB EXCEPT via script access. My ISP won't allow me to connect
to my mySQL DBs via anything other than a script from the webserver. I can not connect to my mySQL
tables using ZEOS, ODBC, or any other method.

<strong>This is how the majority of ISPs handle this, they don't allow "external" access.</strong>

What I mean by security precautions is that you must have correct login credentials to access
your DB. Many people don't change the security credentials of their created mySQL DB's from
UID='root' and PWD=blank..
So, faimo, were you able to successfully connect to your database using the tips provided?
You don't say.
"why should i creat this table as i have it in my DB its name news"
That was only meant as a demo. You change the code to match your
existing table fields.

Hi

If I might add something.

Perhaps those wanting this sort of access to a database should consider moving to a host who provides database access over IP.  Changing Hosts is easy and low cost.

I prefer Windows Servers and to use ASP myself.  I used to have a service for $10 a year per site and $10 per year for 5mb SQLServer space (cant remember who it was).  I used the SQLServer Admin tool over IP to create and maintain my tables.

Maybe it's just a Microsoft thing?

Voodooman
"Maybe it's just a Microsoft thing?"

Maybe, have you been able to access mySQL tables the same way?

Hi Eddie

You can try it here....

http://www.navicat.com/download.html

There is a trial database that you open.

Voodooman

And here is a browser based one http://www.iprogramdev.com/soft.php/Mysql-Data-Manager-4264.htm

If you can connect to a database with a browser based product, you need some sort of database driver to do so, otherwise 'no can do' in Windows.

These hosts suggest you can use standard mySQL Tools 'You can also manage database via shell access using 'mysql' programs respectively.'

http://www.hostinginsites.co.za/faq.htm

How do I manage MySQL databases?
phpMyAdmin are installed and accessible via the control panel for web based management of databases. You can also manage database via shell access using 'mysql' programs respectively.



Regardless, I still can not access my database using Navicat, mySQL-Front or anything other than
script access because that is the only way that my host allows.

If you are able to access yours, cool.

Just don't say that anyone can do it because the majority of hosts do it just like mine, they do not
allow outside access to myQL datasources.



Eddie... your'e starting too look a little peevish...

..don't throw your rattle out of your pram on my account.... I'm really not that bothered...

You win - if it makes you feel any better......

Voodooman


No, I just don't like someone saying that it is so when it isn't.

I don't agree with some of your answers Eddie... but like I said...I'm not that bothered...

Perhaps you're sore because you didn't add to your 269772 points.....
      
Having 269772 points only means that you have plenty of free time....

Me? I got a job, a family, a life...

I am entitled to share my experience and opinions with others for better or for worse without your untoward churlish remarks.

I often feel that  others unfairly get points for their answers - but who told you that life is fair.

In fact, I think some of your comments are opportunistic and that you often unfairly get points.

You don't hear me crying do you?

But like I said Eddie - you win - if it makes you feel better.....

Voodooman

I wasn't beggin for points, in fact you got 'em. What difference does it make how
many points I have if I am trying to point out to the OP the truth about their situation?

Me? I have a job, family and life, too.. I don't help on these forums for the points, that's
just the result of knowledge and hard work.

I'm done here...