0
我使用HttpClient
將幾個文本框的內容發送到我的網站。我的PHP
腳本將數據插入到數據庫中。 但是在點擊btnSubmit按鈕後,它會運行代碼,但沒有任何內容添加到我的數據庫中,並且不會引發異常。我的代碼有什麼問題?發送參數到URL
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Net.Http;
namespace PhoneApp2
{
public partial class Submit2 : PhoneApplicationPage
{
public Submit2()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("barcode", out parameter))
{
Barcode.Text = parameter;
}
}
public static int typeConnection()
{
switch (Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType)
{
default:
return 0;
case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandCdma:
return 1;
case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandGsm:
return 1;
case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.None:
return 2;
}
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
try
{
if (typeConnection() < 2)
{
string URI = "http://cocktailpws.net23.net/requests/add_contribution.php";
string myParameters = "barcode=" + Barcode.Text + "&booze=" + Name.Text + "&email=" + Email.Text;
sendData(URI, myParameters);
}
else
{
MessageBox.Show("No database connection could be established. Please check your internet connection and try again.");
}
}
catch (Exception myExc)
{
Console.WriteLine(myExc.Message);
}
}
public async void sendData(string URI, string myParameters)
{
using (HttpClient hc = new HttpClient())
{
var response = await hc.PostAsync(URI, new StringContent(myParameters));
}
}
}
}
PHP:
if(isset($_POST['barcode']) && isset($_POST['booze']) && isset($_POST['email'])){
include_once "../inc/inc_db.php";
$booze = sqlesc($_POST['booze']);
$barcode = sqlesc($_POST['barcode']);
$email = sqlesc($_POST['email']);
date_default_timezone_set('Europe/Amsterdam');
$date = date("Y-m-d H:i:s");
$query = "INSERT INTO contr_barcode(booze,barcode,email,datum) VALUES ('$booze','$barcode','$email',$date')";
if(mysqli_query($con,$query)){
echo "1";
}else{
echo "0";
}
}
要解決這個問題的唯一方法就是逐步完成它。你有沒有試過正確調試它? –
@DeeMac是的,我做到了。跟着它使用斷點。我認爲我的PHP方法有問題,而不是c#.. – JelleKerkstra
我對PHP的瞭解有限,但我想象的是相同的;你需要通過它。 –