2016-12-29 69 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>