2016-12-29 55 views
2

我一直在努力清理我的代碼的可讀性,並從這裏得到了很多答案,但是我已經得到了很多,但是我無法讓我的代碼在我嘗試拆分HTML PHP。當我在PHP代碼塊中使用echo語句時,代碼工作正常。我試圖將存儲過程的結果輸出到PHP塊外部的HTML表格,但仍使用PHP變量,這裏是我的代碼:獨立的PHP和HTML

<?php 
include_once ('includes/admin.php'); 
if (isset($_GET['submit'])) { 
    $id = $_GET['val1']; 
} 
$wResult = mysqli_query($con, "call getwisherid($id)"); 
?> 

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
     <link href="sqlcss.css" type="text/css" rel="stylesheet"> 
    </head> 
    <body> 
     <form> 
      <input type="text" name="val1" value="" /> 
      <input type="submit" value="submit" name="submit" /> 
     </form> 

     <?php while($row = mysqli_fetch_array($wResult)){ ?> 

<div class="wish_result"> 
    <table> 
    <tr> 
     <td><?php echo $row['name'];?></td> 
    <td><?php echo $row['password'];?></td> 
    </tr> 
    </table> 
</div> 
    </body> 
</html> 
+2

什麼是這裏的問題其實? –

+0

當我運行該文件時,窗體不顯示,並且在Netbeans中出現錯誤(帶有感嘆號的紅色圓圈)。 – Marc

+0

我想教自己的PHP,HTML,CSS,JS等。已經做了一些代碼學院的在線課程,並使用堆棧溢出/谷歌嘗試獲得答案,但只有在這一個約一個星期。 – Marc

回答

2

一些增強你的代碼(連同錯誤報告和查詢); -

<?php 
error_reporting(E_ALL); 
ini_set('display_errors',1); 
?> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
     <link href="sqlcss.css" type="text/css" rel="stylesheet"> 
    </head> 
    <body> 
     <form> 
      <input type="text" name="val1" value="" /> 
      <input type="submit" value="submit" name="submit" /> 
     </form> 
     <?php 
      include_once ('includes/admin.php'); 
      if (isset($_GET['val1']) && !empty($_GET['val1'])) { 
       $id = $_GET['val1']; 
       if($con){ 
        $wResult = mysqli_query($con, "SELECT name,password FROM <table name> WHERE id = $id"); // put here the appropriate table name 
        // i don't know what is this :- call getwisherid($id) and it is correct or not 
        if($wResult){ 
         if(mysqli_num_rows($wResult)>0){ 
     ?> 
      <div class="wish_result"> 
       <table> 
        <?php while($row = mysqli_fetch_assoc($wResult)){ ?> 
        <tr> 
         <td><?php echo $row['name'];?></td> 
         <td><?php echo $row['password'];?></td> 
        </tr> 
        <?php }?> 
       <?php }else{echo "<tr>No Record Available.</t>";}?> 
      </table> 
      <?php }else{"Query error".mysqli_error($con);}?> 
     </div> 
     <?php }else{echo "connection error".mysqli_connect_error();}?> 
    </body> 
    <?php }else{echo "please fill the form value;"}?> 
</html> 
+0

感謝您提出改進代碼的建議,並提供了一些有價值的錯誤捕獲。 PS:我如何回答這個問題? – Marc

1

您可以將更多的進入包括文件,或做這樣的事情:

<?php 
    include_once ('includes/admin.php'); 

    if (isset($_GET['submit'])) { 
     $id = $_GET['val1']; 
    } 

    $out = ' 
     <div class="wish_result"> 
      <table> 
    '; 
    $wResult = mysqli_query($con, "call getwisherid($id)"); 
    while($row = mysqli_fetch_array($wResult)){ 
     $out .= '<tr><td>' .$row['name']. '</td><td>' .$row['password']. '</td></tr>'; 
    } 
    $out .= '</table></div>'; 
?> 

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
     <link href="sqlcss.css" type="text/css" rel="stylesheet"> 
    </head> 
    <body> 
     <form> 
      <input type="text" name="val1" value="" /> 
      <input type="submit" value="submit" name="submit" /> 
     </form> 

     <?php echo $out; ?> 

    </body> 
</html>