zoukankan      html  css  js  c++  java
  • 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."
    " ;
        }
    
        $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 "
    ";
    //        }
    //    }
        $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

  • 相关阅读:
    《DSP using MATLAB》 示例 Example 9.12
    《DSP using MATLAB》示例 Example 9.11
    《DSP using MATLAB》示例 Example 9.10
    《DSP using MATLAB》示例Example 9.9
    《DSP using MATLAB》示例 Example 9.8
    《DSP using MATLAB》示例Example 9.7
    《DSP using MATLAB》示例 Example 9.6
    《DSP using MATLAB》示例Example 9.5
    《DSP using MATLAB》示例 Example 9.4
    (转载)【C++11新特性】 nullptr关键字
  • 原文地址:https://www.cnblogs.com/findumars/p/6323714.html
Copyright © 2011-2022 走看看