php post返回值delphi,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网页

{

$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 = newPDO("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;//关闭连接

}

?>

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

数据库信息(PDO方式连接数据库)

表名:delphi_test_content

字段:id(主键),content

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

DEMO效果:

F9运行delphi 按下按钮

0f0ffb366f4aec9f81ece2d598aad1ff.png

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

最终结果

54da0a2f9806517ba824236e67c0980c.png

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

后来解决了这个中文显示问题(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‘";

最后就解决中文存储到数据库的问题~

谢谢观看~

http://blog.csdn.net/s371795639/article/details/53640483

原文:http://www.cnblogs.com/findumars/p/6323714.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值