十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
form action="" method="post"
专注于为中小企业提供成都网站建设、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业察布查尔锡伯免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了超过千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
select name="host"
option value="localhost" selectedlocalhost/option
option value="127.0.0.1"127.0.0.1/option
/select
brbr
user:input type="text" name="user" value=""brbr
pwd :input type="passWord" name="pwd" value=""brbr
input type="submit" value="connent"
input type="reset" value="reset"
/form
?php
error_reporting(~E_ALL);
$host = $_POST['host'];
$user = $_POST['user'];
$pwd = $_POST['pwd'];
if(isset($_POST['host']) isset($_POST['user']) isset($_POST['pwd'])){
if(strlen($host)1 or strlen($user)1 or strlen($pwd)1){
echo "请完善相关数据库链接信息。";
exit(0);
}
$conn = mysql_connect($host, $user, $pwd) or die("Error-数据库连接失败!");
if($conn){
echo "OK—数据库连接成功!";
}
}
?
比如有个user的表,且表里有数据。
$user=M('User');
$list=$user-find();
dump($list);
若有数据,则表示连接成功。土办法。如果楼主还有不明白的,建议去后盾人找找相关教学视频看看。
参考下面代码及代码中的注释即可:
PHP代码:
conn.php是连接MySQL数据库的。代码如下:
?php
$dbhost = "localhost:3306";
$dbuser = "root"; //我的用户名
$dbpass = ""; //我的密码
$dbname = "testlogin"; //我的mysql库名
$cn = mysql_connect($dbhost,$dbuser,$dbpass) or die("connect error");
@mysql_select_db($dbname)or die("db error");
mysql_query("set names 'UTF-8'");
?
login.php代码:
?php
include ("conn.php");//连接数据库
$username=str_replace(" ","",$_POST['name']);//接收客户端发来的username;
$sql="select * from users where name='$username'";
$query=mysql_query($sql);
$rs = mysql_fetch_array($query);
if(is_array($rs)){
if($_POST['pwd']==$rs['password']){
echo "login succeed";
}else{
echo "error";
}
}
?
class LoginHandler implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//get username and password;
userName = user_name.getText().toString().trim();
password = pass_word.getText().toString().trim();
//连接到服务器的地址,我监听的是8080端口
String connectURL="网站地址/text0/com.light.text/login.php/";
//填入用户名密码和连接地址
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//判断返回值是否为true,若是的话就跳到主页。
if(isLoginSucceed){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);
proDialog.dismiss();
}else{
proDialog.dismiss();
// Toast.makeText(ClientActivity.this, "登入错误", Toast.LENGTH_LONG).show();
System.out.println("登入错误");
}
}
}
//登入的方法,传入用户 密码 和连接地址
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //用来取得返回的String;
boolean isLoginSucceed = false;
//test
System.out.println("username:"+userName);
System.out.println("password:"+password);
//发送post请求
HttpPost httpRequest = new HttpPost(connectUrl);
//Post运作传送变数必须用NameValuePair[]阵列储存
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//发出HTTP请求
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200则请求成功,取到返回数据
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出字符串
result=EntityUtils.toString(httpResponse.getEntity());
ystem.out.println("result= "+result);
}
}catch(Exception e){
e.printStackTrace();
}
//判断返回的数据是否为php中成功登入是输出的
if(result.equals("login succeed")){
isLoginSucceed = true;
}
return isLoginSucceed;
}