【问题标题】:i keep on getting errors whenever i use if else on my html [closed]每当我在我的 html 上使用 if else 时,我都会不断收到错误 [关闭]
【发布时间】:2015-04-08 08:27:03
【问题描述】:

我们被要求创建一个网站,我想问一下我的表单是否有问题,因为每当我在浏览器上打开它时,我的 html 结束标记总是出错。


<?php 
 session_start();
 include('connect.php');
 ?>

<?php

  if (isset($_POST['Login'])){

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

$login_query=mysql_query("select * from user where username='$username' and     password='$password' and Usertype='admin'") or die(mysql_error());
$login_query1=mysql_query("select * from user where username='$username' and password='$password' and Usertype='user'") or die(mysql_error());



$count=mysql_num_rows($login_query);
$count1=mysql_num_rows($login_query1);

$row=mysql_fetch_array($login_query);
$row1=mysql_fetch_array($login_query1);

?>
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="mystylelogin.css"> 
</head>
<body style="background-color: #66A9EB">
    <header>
        <div class="header"><h1><a href="login.php">Tidbits<a></h1></div>
        <div class="header-cont"> 
    </header>
<form method="POST" action="home.php">
        <fieldset>
                <p> Welcome </p>
                    <br>
                    Username: <input type="text" name="uname">
                    <br><br><br>
                    Password: <input type="password" name="pword">
                    <br>
                <div id="button">
                    <br>
                    <input type="submit" style="font-family: Verdana; color: white; font-size: 20px; background-color: #99CCFF; border: 1px solid #3399FF; border-radius:5px; margin-left: 150px" name="do" value="Log In"/>    
                </div>
                <div>
                    <P>

                </div>
            <p id="create"><a href="register.php">Create an Account</a></p>
 <?php 
                        if($count == 1){
                        $_SESSION['id']=$id;
                        header('Location:  admin.php');

                        }
                        if($count1 == 1){
                        session_start();
                        $_SESSION['id']=$id;
                        header('location:user.php');
                        }

                ?>


        </fieldset>
    </form>
<!----------------------------------SLIDESHOW------------------------------------------------------->
    <script type="text/javascript">

        var slideimages = new Array() // create new array to preload images
        slideimages[0] = new Image() // create new instance of image object
        slideimages[0].src = "cookiesandcream.jpg" // set image src property to image path, preloading image in the process
        slideimages[1] = new Image()
        slideimages[1].src = "pizza.jpg"
        slideimages[2] = new Image()
        slideimages[2].src = "waffle.jpg"

        </script>
        </head>
        <body>
        <img src="cookiesandcream.jpg" id="slide" width="700" height="500" />

        <script type="text/javascript">

        //variable that will increment through the images
        var step=0

        function slideit(){
         //if browser does not support the image object, exit.
         if (!document.images)
          return
         document.getElementById('slide').src = slideimages[step].src
         if (step<2)
          step++
         else
          step=0
         //call function "slideit()" every 2.5 seconds
         setTimeout("slideit()",2500)
        }

        slideit()
    </script>

    </body>
    </html>

您能告诉我代码中的错误吗?

【问题讨论】:

  • 在slideit()后加分号;和 setTimeout("slideit()",2500);像这样
  • 什么错误?准确告诉我们您看到的错误以及您期望看到的错误。
  • 不知道还有什么关系,或者你得到什么错误,我仍然可以说有错误。您绝不能使用在 SQL 查询中连接的用户发布值! header/session_start 也可能在页面中间不起作用(除非输出缓存打开)。
  • 我没有看到 if(isset){ 块的关闭。
  • if (isset($_POST['Login'])){ 的右大括号在哪里

标签: php jquery html forms login


【解决方案1】:

header 必须在任何输出完成之前位于页面顶部(在您开始 html 之前)

将其移至 doctype 标签上方:

if($count == 1){
    $_SESSION['id']=$id;
    header('Location:  admin.php');
}

if($count1 == 1){
    // session_start(); ---------- remove this line as you already started the session at the top of the page
    $_SESSION['id']=$id;
    header('location:user.php');
}

more information about header

你错过了打开html标签:

<!DOCTYPE html>
<html>

而且您还没有关闭最初的 if (isset($_POST['Login'])){ - 您需要关闭 }

您还需要在初始幻灯片脚本之后删除以下标签:

</head>
<body>

【讨论】:

  • 我确实忘记了结束标签并将分号放在我的幻灯片和我的 php 上我认为这是错误的原因之一
【解决方案2】:

您的意思是您收到一条消息说您的 html 无效?缺少您的起始 &lt;html&gt; 标记。 正确的标记如下所示:

<!DOCTYPE html>
<html>
<head>
    <title>Some Website</title>
</head>
<body>
    …
</body>
</html>

【讨论】:

    【解决方案3】:

    您缺少第一个 if 语句末尾的右大括号。

    适当的缩进将极大地帮助您在未来发现这些错误,如果您遇到问题无法发现,请尝试通过 php 验证器运行代码,例如 http://phpcodechecker.com/

    【讨论】:

    • 感谢我使用该验证器检查了我的 php 代码,我看到了我的错误
    猜你喜欢
    • 2014-03-22
    • 2015-02-10
    • 1970-01-01
    • 2015-07-20
    • 2021-10-18
    • 1970-01-01
    • 2017-11-27
    • 2013-03-22
    • 2014-08-13
    相关资源
    最近更新 更多