简体   繁体   English

用PHP回送表单和输入

[英]Echo a form and an input with PHP

It's my first question here and I hope I can get some help. 这是我的第一个问题,希望我能得到一些帮助。

The code should display the comments present in the database and add an EDIT button if the user that is logged in is the same as the author of the comment. 如果登录的用户与注释的作者相同,则代码应显示数据库中存在的注释,并添加一个EDIT按钮。

This is the part of the code that is causing problems. 这是导致问题的代码部分。

 if($_SESSION['login_user'] == $row["user"]) 
{
    echo"<form action ='editform.php' method='POST'>";
    echo "<input type='hidden' name='id' value= '". $row["id"] ."'/>";
    echo "". $row["user"] . "". "<br>" . $row["messaggio"] . "<br>". "";
    echo "<input name='submit' type='submit'  value='edit'>". "<hr>";
    echo "</form>";
}

I can not figure out what I am doing wrong. 我不知道我在做什么错。 Only the comments and the user are displayed, and no Edit button. 仅显示注释和用户,没有编辑按钮。

Any idea on what I am doing wrong? 有什么想法我做错了吗?

If the form is not being printed to the HTML, it means that: 如果表单未打印到HTML,则表示:

$_SESSION['login_user'] != $row["user"]

Start from there. 从那里开始。

Try using print_r or die to debug the value of the variables. 尝试使用print_r或die调试变量的值。

The submit button is also missing a / at the end, but that is not certainly the reason. 提交按钮的末尾也缺少/,但这不一定是原因。

I've copy pasted the code into a blank php-file and removed the if-statement. 我已经将代码复制粘贴到一个空白的php文件中,并删除了if语句。

It showed up an edit button for me. 它为我显示了一个编辑按钮。 I think there were errors in your formatting. 我认为您的格式有误。

I often type code like this, to not have any issues with formatting: 我经常键入这样的代码,以确保格式没有任何问题:

if ($_SESSION['login_user'] == $row['user'] { ?>

    <form>

        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

        <?php echo $row['user']; ?><br>
        <?php echo $row['message']; ?><br>

        <input name="submit" type="submit" value="edit"><hr>

    </form>

<?php
}

Also, if working with $_SESSION variables, make sure to have session_start() on top of each page where you need to access $_SESSION variables. 另外,如果使用$_SESSION变量,请确保在需要访问$_SESSION变量的每个页面的顶部都有session_start()

I tried with sample data and it works: ;) 我尝试使用示例数据,它的工作原理是:;)

<?php
    $u_id = "Rapax_id";
    $user = "Rapax";
    $message = "This is the message!";

    $login_user = "Rapax";



     if($login_user == $user) 
    {
        echo"<form action ='editform.php' method='POST'>";
        echo "<input type='hidden' name='id' value= '". $u_id ."'/>";
        echo "". $user . "". "<br>" . $message . "<br>". "";
        echo "<input name='submit' type='submit'  value='edit'>". "<hr>";
        echo "</form>";
    }
    else
    {
        echo "Your are not Authorized";
    }
?>
   <?php if($_SESSION['login_user'] == $row["user"]) { ?>
   <form action ='editform.php' method='POST'>
   <input type='hidden' name='id' value="<?php echo $row["id"]; ?>" />
   <?php echo $row["user"]; ?>
   <br>
   <?php echo $row["messaggio"]; ?>
   <br>
   <input name='submit' type='submit'  value='edit'>
   <hr>
   </form>   
   <?php } ?>

use this and try.. 用这个试试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM