PHP代码在执行时显示在网页中

时间:2017-06-25 12:06:23

标签: php session

我是php的新手我正在尝试编写注册页面。但是出了点问题。每次我尝试执行代码我都会在页面中看到这样的内容

数据库详细信息

Db名称:cibil, 表名:表{id,用户名,密码,电子邮件       }

 query($sql)===true) { $_SESSION['message']='You are successfully added';
       $_SESSION['username']=$user; $_SESSION['email']=$email; 
       header(location:page.php); } else{ $_SESSION['message']='Something went wrong'; } } ?>

我的代码是

<? php
session_start()
$_SESSION['message']='';
if($_SERVER['REQUEST_METHOD']=='POST')
    {  
        $conn=new mysqli('localhost','root','','cibil') or die("error");
        $user=$_POST['user'];
        $pass=$_POST['pass'];
        $email=$_POST['email'];
        $sql="INSERT INFO table (username, password,email)VALUES('$user','$pass','$email')";
        if($conn->query($sql)===true)
        { 
            $_SESSION['message']='You are successfully added';
            $_SESSION['username']=$user;
            $_SESSION['email']=$email;
            header(location:page.php);
        }
        else{
            $_SESSION['message']='Something went wrong';
        }

    }
?>
<!DOCTYPE html>
<html>
<head>
  <title>Register form</title>
  <link rel="stylesheet" type="text/css" href="reg_style.css">
</head>
<body>
<div id="h1">
<h1>Registration Page</h1>
</div>
<div id="err"><? php echo $_SESSION['message'] ?></div>
<div id='form'>
<form method="post" name='form' onsubmit="return rvalidate()" action="">
  <p class="error">*Required</p>
  <label for="user">Enter Your Name* : </label>
  <input id="user" type="text" name="user" class="field">
  <span class='error'></span><br><br>
  <label for="email">Enter Your Email : </label>
  <input id="email" type="text" name="email" class="field">
  <br><br>
  <label for="pass">Enter Your Password* : </label>
  <input type="password" id="pass" name="pass" class="field"><span class='error'></span><br><br>
  <label for="rpass">Re-Enter Your Password* : </label>
  <input type="password" id="rpass" name="rpass" class="field"><span class='error'></span><br><br>
<input type="submit" id='submit' class="field" value="SUBMIT" onclick="validate();" >
</form>
</div>
<br><br>
<div id="login_text">
<b>If you are already register </b><br><a href="login.php">Click here</a>
</div>
<script type="text/javascript">
    var error=document.getElementsByTagName('span');
    var user=document.form.user;
    var pass=document.form.pass;
    var rpass=document.form.rpass;
function validate()
{
    if(user.value==="")
    {
        error[0].innerHTML="*Enter Your name";
        user.setAttribute("style","border-color:green");
    }else{
        error[0].innerHTML="";
        user.setAttribute("style","border-color:initial");
    }
    if(pass.value=="")
    {
        error[1].innerHTML="*Password is Required";
        pass.setAttribute("style","border-color:green");
    }else{
        error[1].innerHTML="";
        pass.setAttribute("style","border-color:initial");
    }
    if(pass.value!==rpass.value)
    {
        error[2].innerHTML="*Password Missmatch";
        rpass.setAttribute("style","border-color:green");
    }else{
        error[2].innerHTML="";
        rpass.setAttribute("style","border-color:initial");
    }

}
function rvalidate()
{
    if (error[0].innerHTML=="" && error[1].innerHTML=="" && error[2].innerHTML=="") {
        return true;
    }
    else{
        return false;
    }
}
</script>
</body>
</html>

感谢您的帮助.........

4 个答案:

答案 0 :(得分:1)

您的代码未执行并显示为文本,因为您之间有空格?和PHP在代码的开头。尝试从<?php session_start();开始。然后你就可以调试所有其余的了:)

答案 1 :(得分:0)

<div id="err"><? php echo $_SESSION['message'] 

如果你发现之间有空格?而PHP,可能会影响带来输出的东西。有几个区域你的报价不匹配所以如果你检查报价是否在关闭位置不相互关闭那么你应该没关系

你的文件名是否正确?如果它是.htm或.html文件,它可能无法解析PHP,这可能是你正在遇到的另一个问题

答案 2 :(得分:0)

你的php代码开头有空格:

<? php

应该是:

<?php

答案 3 :(得分:-1)

您的代码:

$sql="INSERT INFO table (username, password,email)VALUES('$user','$pass','$email')";

必须没有`......`:

$sql="INSERT INFO table (username, password,email)VALUES($user, $pass, $email)";