我写了两个*.php文件.
 页面1代码如下::login1.php   主要是实现登录判断功能.
<? 
error_reporting(0); 
$mysql_servername = "127.0.0.1";  
$mysql_username = "root";  
$mysql_password ="root"; 
$mysql_database ="t_user";  mysql_connect($mysql_servername , $mysql_username , $mysql_password); 
mysql_select_db($mysql_database); 
$tname=$_POST['txt_user']; 
$tpassowrd=$_POST['txt_password']; 
if ($tname && $tpassowrd){ 
$sql = "SELECT * FROM t_user WHERE cUserID = '$tname' and password='$tpassowrd'"; 
$res = mysql_query($sql); 
$rows=mysql_num_rows($res); 
if($rows){ 
echo "<script langrage=javascript>alert('恭喜您登陆成功')</script>";
echo "<script>window.location.href='Index_STR.php';</script>";
exit; 

echo "<script language=javascript>alert('用户名密码错误');history.back();</script>"; 
}
else

echo "<script language=javascript>alert('用户名密码不能为空');history.back();</script>"; 

?> 页面2,就是调用 页面1的代码实现登录,
页面2 就是登陆界面.<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>STR Login</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 24px;
font-weight: bold;
}
-->
</style>
</head><body>
<table width="1002" border="0">
  <tr>
    <td width="340" height="33">&nbsp;</td>
    <td width="72">&nbsp;</td>
    <td width="267"><div align="left"><span class="STYLE1">欢迎登录STR</span></div></td>
    <td width="185">&nbsp;</td>
    <td width="104">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="30">&nbsp;</td>
    <td>User ID </td>
    <td><form id="userid" name="userid" method="post" action="">
      <label>
        <input name="txt_user" type="text" id="txt_user" />
        </label>
    </form></td>
    </tr>
  <tr>
    <td height="28">&nbsp;</td>
    <td>Pass Word </td>
    <td><input name="txt_password" type="password" id="txt_password" /></td>
  </tr>
  <tr>
 </tr>
  <tr>
    <td height="56">&nbsp;</td>
    <td>&nbsp;</td>
    <td><form id="form1" name="form1" method="post" action="login1.php">
      <label>
        <input type="submit" name="Submit" value="登  入" />
        </label>
      <label>
      <input type="submit" name="Submit2" value="注  册" />
      </label>
    </form>
    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </table>
</body>
</html>
为什么我登录的时候,它就提示我"用户名密码不能为空" ,我是输入用户名和密码..
好像代码“if ($tname && $tpassowrd){ ” 这里就跳出啦

解决方案 »

  1.   

    你的表单里没有名为 txt_user 和 txt_password 的输入框
      

  2.   

    将php代码放到if判断中,现在楼主的代码是直接执行,可是这个时候楼主还没有提交表单呢,所以楼主要在代码外面加个if判断,判断是否是提交,提交了才执行
      

  3.   

    +的确,我都没明白下面html中的意思
      

  4.   

    我的是 两个php页面...我是想在下面这html 中调用 login1.php 
      

  5.   

    对呀!
    是在下面这html 中调用 login1.php 
    <form id="form1" name="form1" method="post" action="login1.php">
      

  6.   


    //php中error_reporting(0);  
    $mysql_servername = "127.0.0.1";   
    $mysql_username = "root";   
    $mysql_password ="root";  
    $mysql_database ="t_user";   if(isset($_POST['Submit'])){
    mysql_connect($mysql_servername , $mysql_username , $mysql_password);  
    mysql_select_db($mysql_database);  
    $tname=$_POST['txt_user'];  
    $tpassowrd=$_POST['txt_password'];  
    if ($tname && $tpassowrd){  
    $sql = "SELECT * FROM t_user WHERE cUserID = '$tname' and password='$tpassowrd'";  
    $res = mysql_query($sql);  
    $rows=mysql_num_rows($res);  
    if($rows){  
    echo "<script langrage=javascript>alert('恭喜您登陆成功')</script>";
    echo "<script>window.location.href='Index_STR.php';</script>";
    exit;  
    }  
    echo "<script language=javascript>alert('用户名密码错误');history.back();</script>";  
    }
    else
    {  
    echo "<script language=javascript>alert('用户名密码不能为空');history.back();</script>";  
    }
    }
    //html中
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>STR Login</title>
    <style type="text/css">
    <!--
    .STYLE1 {
    font-size: 24px;
    font-weight: bold;
    }
    -->
    </style>
    </head><body>
    <table width="1002" border="0">
      <tr>
      <td width="340" height="33">&nbsp;</td>
      <td width="72">&nbsp;</td>
      <td width="267"><div align="left"><span class="STYLE1">欢迎登录STR</span></div></td>
      <td width="185">&nbsp;</td>
      <td width="104">&nbsp;</td>
      </tr>
      <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <td height="30">&nbsp;</td>
      <td>User ID </td>
      <td>
    <form id="form1" name="form1" method="post" action="login1.php">
    <label>
    <input name="txt_user" type="text" id="txt_user" />
    </label>
    </form>
      </td>
      </tr>
      <tr>
      <td height="28">&nbsp;</td>
      <td>Pass Word </td>
      <td><input name="txt_password" type="password" id="txt_password" /></td>
      </tr>
      <tr>
     </tr>
      <tr>
      <td height="56">&nbsp;</td>
      <td>&nbsp;</td>
      <td>
    <label>
    <input type="submit" name="Submit" value="登 入" />
    </label>
    <label>
    <input type="submit" name="Submit2" value="注 册" />
    </label>
    </form>
      </td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
      </table>
    </body>
    </html>
      

  7.   


    “你的表单里没有名为 txt_user 和 txt_password 的输入框”
    这句话不是很明白?
      

  8.   

    这是你的表单
    <form id="form1" name="form1" method="post" action="login1.php">
      <label>
      <input type="submit" name="Submit" value="登 入" />
      </label>
      <label>
      <input type="submit" name="Submit2" value="注 册" />
      </label>
      </form>
    你难道不需要输入用户名吗?而你的 login1.php 中是要做检查的
    $tname=$_POST['txt_user'];  
    $tpassowrd=$_POST['txt_password'];  
    if ($tname && $tpassowrd){