这是一个Xamarin android项目,我希望尽可能简化它,所以我按照如何使用webrequest和php将数据插入mysql数据库的教程。
这是我的Activity,我通过editTexts
检索输入using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
namespace CPDEP1
{
[Activity(Label = "Formulaire d'enregistrement", Theme="@style/Theme.AppCompat.Light")]
public class FormActivity : AppCompatActivity
{
CoordinatorLayout FrootLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
FrootLayout = FindViewById<CoordinatorLayout>(Resource.Id.FcoordLayout);
SetContentView(Resource.Layout.Form);
var inputNom = FindViewById<EditText>(Resource.Id.nom);
var inputPrenom = FindViewById<EditText>(Resource.Id.pren);
var inputTel = FindViewById<EditText>(Resource.Id.tel);
var inputID = FindViewById<EditText>(Resource.Id.iden);
var inputDep = FindViewById<EditText>(Resource.Id.dep);
var inputCom = FindViewById<EditText>(Resource.Id.com);
var savebtn = FindViewById<Button>(Resource.Id.save);
var inputAdresse = FindViewById<EditText>(Resource.Id.adresse);
var inputCourriel = FindViewById<EditText>(Resource.Id.email);
savebtn.Click += delegate
{
string nom = inputNom.Text;
string prenom = inputPrenom.Text;
string tel = inputTel.Text;
string id = inputID.Text;
string dep = inputDep.Text;
string com = inputCom.Text;
string adress = inputAdresse.Text;
string courriel = inputCourriel.Text;
//REceiving value from an activity
//string user = Intent.GetStringExtra("User");
//string pass = Intent.GetStringExtra("Password");
if (nom == null || prenom == null || tel == null || id == null || dep == null || com == null || adress == null || courriel == null)
{
////////////SSNACKBAR///////////
Snackbar snack = Snackbar.Make(FrootLayout, "Empty Field", Snackbar.LengthShort)
.SetAction("", (view) =>
{
Toast.MakeText(this, "OK", ToastLength.Short).Show();
}).SetActionTextColor(Color.ParseColor("#000000"));
View SnackView = snack.View;
SnackView.SetBackgroundColor(Color.ParseColor("#E72222"));
snack.Show();
}
else
{
WebClient client = new WebClient();
Uri uri = new Uri("http://localhost/CRUDSERVICE/CreateContact.php");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("Nom", nom);
parameters.Add("Prenom", prenom);
parameters.Add("Tel", tel);
parameters.Add("Dep", dep);
parameters.Add("Com", com);
parameters.Add("Adress", adress);
parameters.Add("Courriel", courriel);
client.UploadValuesAsync(uri, parameters);
}
};
}
// }
}
}
和PHP脚本 一个用于连接
<?php
class ConnectionInfo
{
public $mServerName;
public $mConnectionInfo;
public $conn;
public function GetConnection()
{
$this->mServerName = 'localhost';
$this->mConnectionInfo = array("Database"=>"formation", "UID"=>"root", "PWD"=>"cpdep");
$this->conn = sqlsrv_connect($this->mServerName,$this->mConnectionInfo);
return $this->conn;
}
}
?>
CRUD的另一个
<?php
require_once(dirname(__FILE__).'/ConnectionInfo.php');
if (isset($_POST['Nom']) && isset($_POST['Prenom']) && isset($_POST['Tel']) && isset($_POST['Dep']) && isset($_POST['Com']) && isset($_POST['Adress']) && isset($_POST['Courriel']))
{
//Get the POST variables
$mNom = $_POST['Nom'];
$mPrenom = $_POST['Prenom'];
$mTel = $_POST['Tel'];
$mDep = $_POST['Dep'];
$mCom = $_POST['Com'];
$mAdress = $_POST['Adress'];
$mCourriel = $_POST['Courriel'];
//Set up our connection
$connectionInfo = new ConnectionInfo();
$connectionInfo->GetConnection();
if (!$connectionInfo->conn)
{
//Connection failed
echo 'No Connection';
}
else
{
//Insert new contact into database
$query = 'INSERT INTO thinktank (nom, prenom, telephone, departement, commune, adresse, courriel) VALUES (?, ?, ?, ?, ?, ?, ?)';
$parameters = array($mNom, $mPrenom, $mTel, $mDep, $mCom, $mAdress, $mCourriel);
//Execute query
$stmt = sqlsrv_query($connectionInfo->conn, $query, $parameters);
if (!$stmt)
{ //The query failed
echo 'Query Failed';
}
else
{
//The query succeeded, now echo back the new contact ID
$query = "SELECT IDENT_CURRENT('Contacts') AS NewID";
$stmt = sqlsrv_query($connectionInfo->conn, $query);
$row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
echo $row['NewID'];
}
}
}
?>
过去3个小时我已经尝试了一切。在此先感谢您的帮助
答案 0 :(得分:2)
一个简单的解决方案是将你的php从post改为
只需用$ _GET
替换$ _POST之后,如果您想要调用您的URL,请执行此示例
现在让我们去寻找代码
String prenom="john";
String nom="doe";
String tel="025545";
String dep="security";
String com="somecom";
String adress="street431";
String courriel="youremail@gmail.com";
// let's build our link
String link="http://localhost/CRUDSERVICE/CreateContact.php?Nom="+nom+"&Prenom="+prenom+"&Tel="+tel+"&Dep="+dep+"&Com="+com+"&Adress+"+adress+"&Courriel="+courriel;
WebClient wc=new WebClient(); // don't forget using System.Net;
String res=wc.DownloadString(link); // res is data printed by echo
if(res.equals("No Connection ") || res.equals("Query Failed"))
{
// something went wrong
}
else
{
}