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