2017-06-11 33 views
0

我正尝试使用PHP创建新页面(文件)。创建一个新的HTML页面并使用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和伢子并插入到数据库成功。我也成功地获得了ID。

在其他页面,创建一个新的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的值..

+0

我假设你动态创建的页面。但是当你访问你创建的页面时,你需要有'url /?id = something'。这会让你在'$ _GET ['id']'里面有一个值。仅仅因为你已经在当前页面中设置了$ _GET ['id'] = something',并不意味着它会在下一个(甚至是在生成的页面中) – FirstOne

+0

'$ text = '<?php等''???那将不起作用 – RamRaider

+1

btw;太多的问题,没有人接受。 –

回答

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); 
?> 

Now coming to your code. When you already have value in Session variable you don't need to assign in $_GET variable. Access it directly.

+0

缺少'session_start();'。 – FirstOne

+0

@FirstOne完成。 –

+0

它没有工作。注意:未定义的索引:在第5行的C:\ xampp \ htdocs \ doktor \ am.php中的标识 – Emre

相关问题