谁使用 localhost phpmyadmin 将数据插入 mysql 数据库?
Posted
技术标签:
【中文标题】谁使用 localhost phpmyadmin 将数据插入 mysql 数据库?【英文标题】:Who to inserting data into mysql database using the localhost phpmyadmin? 【发布时间】:2016-10-15 17:31:00 【问题描述】:我正在尝试通过 wamp 服务器向我的 msql 数据库中插入一行。数据库位于本地主机上。
我使用 android stuido 作为 ide,并且在日志中没有收到任何错误或警告。我找不到我的代码有什么问题。我猜问题是使用 httpclient 还是 php 文件,因为新的用户名和密码已成功传递给NewProductActivty.java
。如何解决这个问题?
这是我的 java 和 php 代码:
functions.php
:
<?php
$DB_HOST = "localhost";
$DB_DATABASENAME = "kimnerede";
$DB_USERNAME = "root";
$DB_PASS = "";
define("BASARISIZ", "-1");
define("BASARILI", "1");
define("ARKADAS_BULUNAMADI_ERROR", "-2");
define("ARKADAS_ZATEN_MEVCUT_ERROR", "-3");
define("PROFIL_BULUNAMADI_ERROR", "-4");
function dbConnect()
$db = mysqli_connect(DB_HOST, DB_DATABASENAME, DB_PASS, DB_USERNAME);
if (!$db)
return null;
mysqli_query($db, 'SET NAMES utf8');
return $db;
其他功能:
function arkadasEkle($db, $name, $pass)
$sorgu = "INSERT INTO deneme123 (name, pass) VALUES ('$name', '$pass')";
if(!mysqli_query($db, $sorgu))
return false;
return true;
?>
addclient.php
:
<?php
//require_once('functions.php');
include('functions.php');
if((!isset($_POST['name']) || empty($_POST['name'])) &&
(!isset($_POST['pass']) || empty($_POST['pass'])))
die(BASARISIZ);
$name= $_POST['name'];
$pass= $_POST['pass'];
$db = dbConnect();
if(arkadasEkle($db, $name, $pass))
die(BASARILI);
die(BASARISIZ);
?>
这些 php 文件位于 C:\wamp\www\kimnerede
directory 中。
这是进行连接和插入的类:
public class NewProductActivity extends AsyncTask<String,Void,String>
public static final String KIM_NEREDE_BASE_URL = "http://10.0.2.2/kimnerede/";
public static final String KIM_NEREDE_PROFIL_KAYDET_URL = KIM_NEREDE_BASE_URL + "addclient.php";
private static final String TAG = "NetworkManager";
private Context context;
public NewProductActivity(Context context)
this.context = context;
public static String Ekle(String p_name, String p_pass)
BufferedReader in = null;
try
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(KIM_NEREDE_PROFIL_KAYDET_URL);
List<NameValuePair> parametreList = new ArrayList<NameValuePair>();
parametreList.add(new BasicNameValuePair("name", p_name));
parametreList.add(new BasicNameValuePair("pass", p_pass));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
return in.readLine();
catch (Exception e)
Log.d(TAG, "Profil kaydedilirken hata olustu", e);
finally
if (in != null)
try
in.close();
catch (Exception e)
e.printStackTrace();
return null;
@Override
protected String doInBackground(String... params)
Ekle(params[0],params[1]);
return null;
这个类是通过在LoginActivity.java
中创建对象来调用的:
public void SignUpClicked(View view)
new NewProductActivity(this).execute(UserName.getText().toString(),Password.getText().toString());
【问题讨论】:
你容易受到sql injection attacks的攻击 您绝对应该考虑使用using PDO 来访问您的数据库,这应该有助于防止注入 在 Chrome 中尝试 Postman 扩展来检查您的 PHP 是否正在运行。 永远不要存储纯文本密码!请使用 PHP 的 built-in functions 来处理密码安全问题。如果您使用低于 5.5 的 PHP 版本,您可以使用password_hash()
compatibility pack。在散列之前,请确保您 don't escape passwords 或对它们使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。
【参考方案1】:
如果您没有收到任何错误,您应该确保在您的 php.ini 文件中将其设置为 actually display 错误:
display_errors = on
还要确保您已设置:
error_reporting = E_ALL & ~E_NOTICE
【讨论】:
【参考方案2】:connection.php
<?php
$connection = mysqli_connect("localhost", "root", "", "kimnerede") or die("unable to connect to the database");
?>
insertData.php
<?php
$name = $_REQUEST['name'];
$pass = $_REQUEST['pass'];
include_once("connection.php");
$query = "INSERT INTO deneme123 (name, pass) VALUES ('$name', '$pass')";
mysqli_query($connection, $query) or die("unable to execute the query");
?>
DBOperations.java
public class DBOperations
public boolean addDataToDB(String name, String pass)
try
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.sample.com/phpfilelocation/insertData.php");
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httpPost);
return true;
catch (Exception ex)
ex.printStackTrace();
return false;
catch(Exception e)
e.printStackTrace();
return false;
你的异步任务
public class NewProductActivity extends AsyncTask<String, Void, Boolean>
String name, pass;
ProgressDialog progressDialog;
DBOperations dbo = new DBOperations():
public NewProductActivity(String name, String pass)
this.name = name;
this.pass = pass;
@Override
protected void onPreExecute()
progressDialog = ProgressDialog.show(context, "Please wait", "Sending data...", true);
@Override
protected Boolean doInBackground(String... params)
try
boolean added = dbo.addDataToDB(name, pass);
return added;
catch(Exception e)
e.printStackTrace();
return false;
@Override
protected void onPostExecute(Boolean result)
if(result)
Toast.makeText(context, "successfully added.", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "failed to add data.", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
要运行 AsyncTask,
new NewProductActivity("name", "pass").execute();
将此行添加到您的清单文件中
<uses-permission android:name="android.permission.INTERNET" />
【讨论】:
你能告诉我在 asynctask 中声明上下文值的位置以及为什么使用它吗?哪个值作为参数传递给 onPostExecute?为什么 AsyncTask 需要以上是关于谁使用 localhost phpmyadmin 将数据插入 mysql 数据库?的主要内容,如果未能解决你的问题,请参考以下文章
使用 WAMP 堆栈修复 localhost/phpmyadmin/ 地址
无法访问 phpMyAdmin。我的 wampserver 版本是 2.2 localhost/phpmyadmin/
我无法打开 http://localhost/phpmyadmin/ 如何解决?