0

POSTメソッドを介してWindows Phone 7からphpページにデータを送信する必要があります.wp7側に次のコードがあります

    public void SendPost()
    {
        var url = "http://localhost/HelpFello/profile.php";

        // Create the web request object
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        // Start the request
        webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
        MessageBox.Show("data sent");
    }

    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the stream request operation
        Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);

        // Create the post data
        // Demo POST data 
        string postData = "user_id=3&name=danish&email_id=mdsiddiquiufo&password=12345&phone_Number=0213&about_me=IRuel2&rating=5";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
    }

    void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response;

            // End the get response operation
            response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamResponse);
            var Response = streamReader.ReadToEnd();
            streamResponse.Close();
            streamReader.Close();
            response.Close();

        }
        catch (WebException e)
        {
            MessageBox.Show(e.ToString());
        }
    }

ローカルホストをフォローして、データをデータベースに送信します

<?php
require_once("constants.php");
$user_id = $_POST['user_id'];
$name = $_POST['name'];
$email_id = $_POST['email_id'];
$password = $_POST['password'];
$phone_number = $_POST['phone_number'];
$about_me = $_POST['about_me'];
$rating = $_POST['rating'];


$query="INSERT INTO profile(User_ID,Name,Email_ID,password,Phone_Number,About_Me,Rating) VALUES ({$user_id},'{$name}','{$email_id}','{$password}',{$phone_number}, '{$about_me}' , {$rating})";
mysql_query($query,$connection);
mysql_close($connection);
?>

コードを実行してもエラーはありません。これは、コードが正常に動作していることを意味しますが、データベースにデータが挿入されていません。

4

2 に答える 2

2

HttpWebRequest よりも良い方法があると思います。それがWebクライアントです。そこでメソッドを変更し、get string の場合と同様にデータを追加できます。key=value&key2=value 次に、そのリクエストを呼び出して応答を取得したら、デバッグして VS から出力を取得するか、それが難しい場合は、コード内のテキストブロックに文字列を割り当てるだけです。そのページが実行されたかどうかがわかります。

サンプルコード:

WebClient wc = new WebClient();
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
wc.Encoding = Encoding.UTF8;

Parameters prms = new Parameters();
prms.AddPair("email", email);
prms.AddPair("password", password);

wc.UploadStringAsync(new Uri(loginUrl), "POST", prms.FormPostData(), null);


private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
        // e.Result will contain the page's output
}


// This is my Parameters and Parameter Object classes

public class Parameters
{
public List<ParameterObject> prms;

        public Parameters()
        {
            prms = new List<ParameterObject>();
        }

        public void AddPair(string id, string val)
        {
            prms.Add(new ParameterObject(id, val));
        }

        public String FormPostData()
        {
            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < prms.Count; i++)
            {
                if (i == 0)
                {
                    buffer.Append(System.Net.HttpUtility.UrlEncode(prms[i].id) + "=" + System.Net.HttpUtility.UrlEncode(prms[i].value));
                }
                else
                {
                    buffer.Append("&" + System.Net.HttpUtility.UrlEncode(prms[i].id) + "=" + System.Net.HttpUtility.UrlEncode(prms[i].value));
                }
            }

            return buffer.ToString();
        }
    }

public class ParameterObject
{
    public string id;
    public string value;

    public ParameterObject(string id, string val)
    {
        this.id = id;
        this.value = val;
    }
}
于 2013-02-08T10:24:44.943 に答える
0

最初のエラー: エラー メッセージがない場合は成功を意味すると仮定します

2 番目のエラー: SQL インジェクション ホールのギャップ

最初の修正: 常にクエリが失敗すると想定し、その条件を確認します。

$result = mysql_query($query) or die(mysql_error());

2 番目の修正: mysql_ () 関数を捨てて、プレースホルダー付きの準備済みステートメントを使用する PDO に切り替えます。ブーム。インジェクションの問題はもうありません。また、将来の PHP バージョンでmysql_ () が削除されても、コードの動作が停止することはありません。

PS..

3 番目のエラー: 電話番号の値に引用符がありません。したがって、誰かが 867-5309 を送信すると、mysql は文字列ではなく 2 つの数値が減算されると見なしたため、最終的に -4442 を挿入することになります。

于 2013-02-07T20:00:46.317 に答える