重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
form action="" method="post"
创新互联建站是一家专业提供巴中企业网站建设,专注与成都做网站、成都网站设计、H5网站设计、小程序制作等业务。10年已为巴中众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
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—数据库连接成功!";
}
}
?
参考下面代码及代码中的注释即可:
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;
}
比如有个user的表,且表里有数据。
$user=M('User');
$list=$user-find();
dump($list);
若有数据,则表示连接成功。土办法。如果楼主还有不明白的,建议去后盾人找找相关教学视频看看。