Delphi通过POST传递参数给PHP


本文整理自网络,侵删。

 Delphi代码

************************************************************************************************************************************

unit Unit1;


interface


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


type
  TForm1 = class(TForm)
    Button1: TButton;
    IdHTTP1: TIdHTTP;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  strlist_ParamPost : TStringList ;
  class_IdHttp : TIdHTTP ;
begin
  strlist_ParamPost := TStringList.Create() ;
  class_IdHttp := TIdHTTP.Create(nil);
  try
    // 向目标PHP网址POST参数
    // strlist_ParamPost.Add('1=测试1') ;
    
       strlist_ParamPost.Add('1=aaaaa');


    // TidHTTP属性设置
    class_IdHttp.ReadTimeout := 30*1000 ; // 超时设置
    class_IdHttp.Post('http://localhost/DelphiRequest/index.php', strlist_ParamPost) ;
    //打开网页,ShellExecute需要引入uses ShellAPI
    // ShellExecute(Application.Handle, nil, 'http://localhost/DelphiRequest/index.php', nil, nil, SW_SHOWNORMAL);
  finally
    FreeAndNil(class_IdHttp);
    strlist_ParamPost.Free() ;
end;
end;

end.
**************************************************************************************************************************************

php文件代码

新建一个文件夹DelphiRequest,然后在这个文件夹里建一个index.php写入代码

然后在输入本地地址http://localhost/DelphiRequest/index.php/就可以看到该PHP网页

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<?php


if(count($_POST)!= 0)
{
    $var_PostAllParma = "" ;
    foreach($_POST as $var_Key => $var_Value)
    {
        $var_PostAllParma  .= $var_Value."\r\n" ;
    }

    $host = 'localhost';
    $database = 'test';
    $username = 'root';
    $password = '****';
    $selectName = '1';//要查找的用户名,一般是用户输入的信息
    $pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);//创建一个pdo对象
    $pdo->exec("set names 'utf8'");
    $sql ="UPDATE delphi_test_content SET content= '$var_PostAllParma'  WHERE ID='2'";
    $stmt = $pdo->prepare($sql);
    $rs = $stmt->execute(array($selectName));
//    if ($rs) {
//        // PDO::FETCH_ASSOC 关联数组形式
//        // PDO::FETCH_NUM 数字索引数组形式
//        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//            $name = $row['id'];
//            $age = $row['content'];
//            echo "Name: $name ";
//            echo "Age: $age ";
//            echo "\n";
//        }
//    }
    $pdo = null;//关闭连接

}

?>
</body>
</html>
**************************************************************************************************************************************

数据库信息(PDO方式连接数据库)
表名:delphi_test_content

字段:id(主键),content

**************************************************************************************************************************************

DEMO效果:
F9运行delphi 按下按钮


按下后,把‘aaaa’这个信息放在POST里传递给http://localhost/DelphiRequest/index.php,该网页获取POST值并且把值保存在表delphi_test_content的ID为2的content里

但是如果是中文的话貌似不显示,我也不知道为啥....

后来解决了这个中文显示问题(2016.12.16)

需要添加一行代码


$a=mb_convert_encoding($var_PostAllParma, "UTF-8", "GBK");//delphi7用post传递值给php中文需要字符转码
然后把查询语句的$var_PostAllParma参数更换为$a

$sql ="UPDATE delphi_test_content SET content= '$a' WHERE ID='2'";
最后就解决中文存储到数据库的问题~
--------------------- 

原文:https://blog.csdn.net/s371795639/article/details/53640483 

相关阅读 >>

Delphi getwebbrowserhtml 获取网页源代码

Delphi thttpclient posturl

Delphi webbrowser 加载html成web

Delphi unigui 获取files路径

Delphi keydown与keyup、keypress的区别

Delphi 如何确定活动桌面是否已启用

Delphi服务器端如何防止ddos

Delphi deletedirectory 删除目录下所有文件包括子文件夹下所有文件

Delphi中使用tpathanimation

Delphi xe6取得android智能手机的电话号码等的终端信息

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...