2017-06-11 91 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

相關問題