普通网友 2009-12-09 18:14 采纳率: 0%
浏览 7

Php连接数据库

I am trying to connect to a database using a login form. Currently there is one user in the database but when pressing submit the page just appears to refresh and is not redirected to the home page as it should. Here is my code:

<html>
<head><title>Login</title></head>
<body>


<?php
ob_start();
include('connect.php');

$handle = mysql_connect($hostname, $username, $password)or die("cannot connect");
$error = mysql_select_db($databasename,$handle);

$myusername=$_POST['username']; 
$mypassword=$_POST['password'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tablename WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("username");
session_register("password"); 
header("Location: home.php");
}
else {
echo "Wrong Username or Password";
}
?>


<form action='LoginREAL.php'
                method='POST' style='margin: .5in'>
    <p><label for='user_name' style='font-weight: bold;
          padding-bottom: 1em'>USER ID: </label>
       <input type='text' name='username' id='username'
          value='' /></p>
    <p><label for='password' style= 'font-weight: bold'>Password: </label>
       <input type='password' name='password' id='password'
          value='' /></p>
    <p><input type='submit' value='Login'> </p>
       <input type='hidden' name='sent' value='yes'/>

<a href= "/home/jparry2/public_html/register.php">Register</a>

    </form>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • dqfwcj0030 2009-12-09 18:18
    关注

    My guess would be the problem is not with your login functionality, but with your header() redirection statement. The header() redirection will only work if it occurs before any html is sent to the browser. Once the html has started, the http headers have already been sent and cannot be changed. Hopefully, that is your only problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等