创建一个新的HTML页面并使用PHP添加内容

时间:2017-06-11 13:54:17

标签: php html admin panel

我正在尝试使用PHP创建新页面(文件)。

我已经打开并创建了页面,我已经从MySQL添加了内容,但我无法从上一页获得id变量。

我的管理员面板是这样的:

<?php
    if(isset($_POST['sayfaekle'])) {
        $baslik = $_POST['baslik'];
        $text = $_POST['editor1'];
        $sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";

        $db->exec($sql);
        $id=$db->lastInsertId();

        echo "basarılı";
        include "sayfagonder.php";
    }                                   
?>

在这里,我得到了baslik和yazi并成功插入数据库。我也成功了。

在创建新PHP页面代码的其他页面中,如下所示:

<?php
    $_GET['id'] = $id;
    ini_set('display_errors','0');
    error_reporting(0);

    $pagename = '../'.$baslik. '.php' ;

    $myfile = fopen( $pagename   , "w");
    $text = ' <?php 

    include "config.php";

    $id = $_GET["id"];

    echo $id;
    $sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
    $sayfasor->execute(array(0));
    $sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);

    include "header.php";
?>

<div class="content">
    <div class="row">
        <div class="pageana">
            <div class="col-sm-8">
                <h1><?php  echo $sayfacek["sayfa_baslik"];  ?></h1>
                <?php  echo $sayfacek["sayfa_text"]; ?>
            </div>      
        </div>
    </div>
</div>
</div>

<?php
    include "footer.php";
?> ';

    fwrite($myfile , $text);
    fclose($myfile);
?>

我收到了这个错误:

Notice: Undefined index: id in C:\xampp\htdocs\doktor\deneme56.php on line 5

我无法获得id的值..

1 个答案:

答案 0 :(得分:0)

您正在获取$id的值,但该值不会持续存在。在php $_SESSION变量中设置值,然后在新页面上访问它。像这样的东西 -

<?php
    session_start();
    if(isset($_POST['sayfaekle'])) {
        $baslik = $_POST['baslik'];
        $text = $_POST['editor1'];
        $sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";

        $db->exec($sql);
        $id=$db->lastInsertId();

        $_SESSION['id'] = $id;
        echo "basarılı";
        include "sayfagonder.php";
    }
?>

现在您可以在新页面上访问此值 -

<?php
    session_start();
    $_GET['id'] = $_SESSION['id'];
    ini_set('display_errors','0');
    error_reporting(0);

    $pagename = '../'.$baslik. '.php' ;

    $myfile = fopen( $pagename   , "w");
    $text = ' <?php 

    include "config.php";

    $id = $_GET["id"];

    echo $id;
    $sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
    $sayfasor->execute(array(0));
    $sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);

    include "header.php";
    ?>
    <div class="content">
            <div class="row">
            <div class="pageana">
            <div class="col-sm-8">
            <h1><?php  echo $sayfacek["sayfa_baslik"];  ?></h1>
    <?php  echo $sayfacek["sayfa_text"]; ?>
            </div>

            </div></div>
    </div>
    </div>
<?php
    include "footer.php";
?> ';

fwrite($myfile , $text);
fclose($myfile);
?>
  

现在来看你的代码。当你已经在Session变量中有值时,你不需要在$ _GET变量中赋值。直接访问它。