conn.php
-------------------------<?php
$db_host="localhost";//database host
$db_user="root";//user name
$db_password="jeri";//user password
$db_name="study";//database name//connect to database
$conn=mysql_connect($db_host,$db_user,$db_password) or die("can't connect to the database host:".$db_host);
//character setting
mysql_query("set names 'utf8'");
//databse setting
mysql_select_db($db_name,$conn) or die("can't connect to the database:".$db_name); 
//execute sql(query)
$result=mysql_query($sql) or die("can't execute query");
?>
login.php
------------------------------------------------<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>管理登录</title>
<script language="javascript" src="js/jquery-1.2.6-cn.js" type="text/javascript"></script>
<script language="javascript" src="js/validate.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function checkForm()
{
var userName=$("#userName").val();
var password=$("#password").val();
var checkCode=$("#checkCode").val();
var userNameReg=/^\w{3,20}$/;//new RegExp("^\w{3,20}$");
var realNameReg=/^[\u4e00-\u9fa5]{2,4}$/;//new RegExp("^[\u4e00-\u9fa5]{2,4}$");
var checkCodeReg=/^[0-9]{4}$/;//new RegExp("^[0-9]{4}$");
//alert(checkCodeReg.test(2345));
/*
alert("password="+password);
alert("password_test="+userNameReg.test(password)); alert("userName="+userName);
alert("userName_test="+userNameReg.test(userName));
*/
if(!checkCodeReg.test(checkCode))
{
//alert(checkCode);
$("#codeMsg").html("<font color='red'>验证码不正确</font>");
return false;
}
if(!userNameReg.test(userName))
{
alert("请正确填写用户名!");
return false;
}
if(!userNameReg.test(password))
{
alert("请正确填写密码!");
return false;
}
return true;
}
</script>
</head>
 <body>
 <?php 
 if($_POST["userName"])
 {
  $userName=$_POST["userName"];
  $password=md5($_POST["password"]);
 
  $sql="select * from UserList where userName='$userName' and pwd='$password'";
   require("conn.php");
   $userInfo=mysql_fetch_array($result);
   if(!empty($userInfo))
   {
    //require("conn.php");
    $sql="update UserList set loginCount=loginCount+1,lastLogin=CURRENT_TIMESTAMP where userName='$userName'";
    //echo $sql."<br/>";
    //在这里怎么写才那个执行更新最后登录状态的SQL语句
    if($result)
    {
    echo "登录成功!";
    }
    else
    {
    echo "更新成功!";
    }
   }
   else
   {
    echo "登录失败!<a href='javascript:history.go(-1)'>返回</a>";
   }
 }
 else
 {
 ?>
 <form action="login.php" method="post" onsubmit="javascript:return checkForm();">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>用户名</td><td><input type="text" name="userName" id="userName" size="20" maxlength="20" /></td>
</tr>
<tr>
<td>密码</td><td><input type="password" name="password" id="password" size="20" maxlength="20" /></td>
</tr>
<tr>
<td>验证码</td><td><input type="text" name="checkCode" id="checkCode" size="4" maxlength="4"/><div id="codeMsg"></div><img alt="验证码" src="CheckImage.php" name="KeyImg" id="KeyImg" onclick="KeyImg.src='CheckImage.php?'+Math.random()" /></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td><td><input type="reset" value="重置"/></td>
</tr>
</table>
 </form>
 <?php
 }
 ?>
 </body>
 </html>
//在这里怎么写才那个执行更新最后登录状态的SQL语句
在源代码中有上面这么一句,请问我怎么写就可以保证登录成功之后就可以更新最后登录信息呢?

解决方案 »

  1.   


    $sql="select * from UserList where userName='$userName' and pwd='$password'";
          require("conn.php");
          $result = mysql_query($sql);//这句要加上
          $userInfo=mysql_fetch_array($result);
          if(!empty($userInfo))
          {
              //require("conn.php");
              $sql="update UserList set loginCount=loginCount+1,lastLogin=CURRENT_TIMESTAMP where userName='$userName'";
              mysql_query($sql);
              //echo $sql."<br/>";
              //在这里怎么写才那个执行更新最后登录状态的SQL语句
              if($result)
              {
                  echo "登录成功!";
              }
              else
              {
                  echo "更新成功!";
              }
          }
          else
          {
              echo "登录失败!<a href='javascript:history.go(-1)'>返回</a>";
          }