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'";
最后就解决中文存储到数据库的问题~
谢谢观看~
http://blog.csdn.net/s371795639/article/details/53640483
Delphi通过POST传递参数给PHP的更多相关文章
- Delphi过程函数传递参数的几种方式
Delphi过程函数传递参数的几种方式 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out. 另一种不加修饰符的为默认按值传递参数. 一.默认方式以值方式传递参数 proced ...
- 关于Delphi中多线程传递参数的简单问题
http://bbs.csdn.net/topics/390513469/ unit uThread; interface uses Classes; type Th = class(TThread) ...
- Delphi过程函数传递参数的八种方式
今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...
- 【delphi】Delphi过程、函数传递参数的八种方式
Delphi过程函数传递参数的八种方式
- delphi的一些语法知识 以及参数传递问题,按引用方式传递参数,按值方式传递参数
//delphi中exit,abort,break,continue 的区别 exit: 退出函数体abort: 遇到异常,安静处理,就是不显示不提示break: 退出当前循环体,包括for ,whi ...
- Vue 给子组件传递参数
Vue 给子组件传递参数 首先看个例子吧 原文 html <div class="container" id="app"> <div clas ...
- [转] C++的引用传递、指针传递参数在java中的相应处理方法
原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...
- 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)
目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...
- 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数
1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...
随机推荐
- 为什么上传文件的表单里要加个属性enctype----摘录
上传文件的表单中<form>要加属性enctype="multipart/form-data",很多人只是死记硬背知道上传表单要这么写,知其然而不知其所以然.那到底为什 ...
- K-lord #2
题目描述 还记得,高中数学联赛,2005,那道毒瘤题. 如果自然数的各位数字之和等于7,那么称为“吉祥数”.将所有“吉祥数”从小到大排成一列 $a_{1}$,$a_{2}$,$a_{3}$ ... , ...
- 数三角形(codevs 3693)
题目描述 Description 给定一个n×m的网格,请计算三个点都在格点上的三角形共有多少个(三角形的三点不能共线).下图为4×4的网格上的一个三角形. 输入描述 Input Descripti ...
- Influx kafka
http://www.opscoder.info/kafka-influxdb.html
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- BZOJ——3296: [USACO2011 Open] Learning Languages
http://www.lydsy.com/JudgeOnline/problem.php?id=3296 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: ...
- 第1章 SpringBoot 简介
一.什么是Spring Boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- cut printf awk sed grep笔记
名称 作用 参数 实例 cut 截取某列,可指定分隔 -f 列号 -d 分隔符 cut -d ":" -f 1, 3 /etc/passwd 截取第一列和第三列 printf pr ...
- MySQL 5.6.20-4 and Oracle Linux DTrace
https://blogs.oracle.com/wim/entry/mysql_5_6_20_4?utm_source=tuicool&utm_medium=referral By WimC ...
- jquery 获取浏览器窗口的可视区域高度 宽度 滚动条高
原文:http://www.open-open.com/code/view/1421827925437 alert($(window).height()); //可视区域高度 alert($(docu ...