我试图传递$ _SESSION var

时间:2016-01-06 08:51:28

标签: php html session session-variables

为什么这不起作用?我的错误在哪里,我试图将$_SESSION var传递到另一个网站:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Buch eintragen</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
 <table><form method='post'>
        <tr><td>ISBN: </td><td><input type='text' name='isbn' /></td></tr>
        <tr><td>Titel: </td><td><input type='text' name='title' /></td></tr>
        <tr><td>Autor: </td><td><input type='text' name='autor' /></td></tr>
        <tr><td>Verlag: </td><td><input type='text' name='verlag' /></td></tr>
        <tr><td><input type='submit' value='neues Buch anlegen'/></td></tr>
        </form></table>
        <?php 
        session_start();
        if (isset($_POST['isbn'])){
        $_SESSION['isbn'] = $_POST['isbn'];
        $_SESSION['title'] = $_POST['title'];
        $_SESSION['autor'] = $_POST['autor'];
        $_SESSION['verlag'] = $_POST['verlag'];

        //header('Location: /KlausurSchulzke/buch-eintragen.php');
        } ?>

        <strong><?php echo $_SESSION['isbn'];?></strong>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

在发送任何标头之前,

session_start()需要位于页面顶部。

您必须在文件的顶部放置您想要进行会话的内容:

<?php
session_start(); // session is always at the top here.
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Buch eintragen</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
 <table><form method='post'>
        <tr><td>ISBN: </td><td><input type='text' name='isbn' /></td></tr>
        <tr><td>Titel: </td><td><input type='text' name='title' /></td></tr>
        <tr><td>Autor: </td><td><input type='text' name='autor' /></td></tr>
        <tr><td>Verlag: </td><td><input type='text' name='verlag' /></td></tr>
        <tr><td><input type='submit' value='neues Buch anlegen'/></td></tr>
        </form></table>
        <?php 
        if (isset($_POST['isbn'])){
        $_SESSION['isbn'] = $_POST['isbn'];
        $_SESSION['title'] = $_POST['title'];
        $_SESSION['autor'] = $_POST['autor'];
        $_SESSION['verlag'] = $_POST['verlag'];

        //header('Location: /KlausurSchulzke/buch-eintragen.php');
        } ?>

        <strong><?php echo $_SESSION['isbn'];?></strong>
</body>

</html>

答案 1 :(得分:1)

感谢您的所有努力。 我发现/记得一个简单的解决方法。 我知道,这不是我的问题的真正答案,但我没有时间。我的老师也禁止我在我的html文件中使用php。

HTML的文件

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Buch eintragen</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
 <table><form method='post' action="buch-eintragen.php">
        <tr><td>ISBN: </td><td><input type='text' name='isbn' /></td></tr>
        <tr><td>Titel: </td><td><input type='text' name='title' /></td></tr>
        <tr><td>Autor: </td><td><input type='text' name='autor' /></td></tr>
        <tr><td>Verlag: </td><td><input type='text' name='verlag' /></td></tr>
        <tr><td><input type='submit' value='neues Buch anlegen'/></td></tr>
        </form></table>
        <div class='button menu' onclick="javascript:location.href='/KlausurSchulzke/SchulzkeKlausurBuecherSQL.php'">Menu</div>
</body>

</html>

PHP-文件

<?php
$baseURI = $_SERVER['PHP_SELF'];
        openSQL();
        $sqlbefehl = 'INSERT INTO `buecher` VALUES (\''.$_POST['isbn'].'\',\''.$_POST['title'].'\',\''.$_POST['autor'].'\',\''.$_POST['verlag'].'\')';
        @mysql_query("SET NAMES 'utf8'");
        @mysql_query($sqlbefehl);
        header('Location: /KlausurSchulzke/SchulzkeKlausurBuecherSQL.php?command=work');

function openSQL()
{
    $server   = "localhost";
    $user     = "root";
    $passwort = "";
    $db       = "schulzke";
    $dblink   = @mysql_connect($server, $user, $passwort);
    if (!@mysql_select_db($db)) {
        echo "<br>Keine Verbindung zur Datenbank $db möglich!";
        echo "<br>" . mysql_error();
        global $baseURI;
        echo "<div class='button menu' onclick=\"javascript:location.href='$baseURI'\">Menu</div>";
        die();
    }

}
?>