2

I have been trying to clean up the readability of my code and based off a number of answers from here, I have gotten pretty far, however I cannot get my code to work when I try split the HTML from the PHP. The code worked fine when I used the echo statement within PHP code block.I'm trying to output the result of a stored proc to an HTML table outside of PHP block, but still using PHP variables here is my code:

<?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>
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Marc
  • 31
  • 6
  • 2
    what is the problem here actually? – Serving Quarantine period Dec 29 '16 at 19:13
  • When i run the file, the form does not show and in Netbeans there is an error (red circle with exclamation mark). – Marc Dec 29 '16 at 19:23
  • I'm trying to teach myself php, HTML, CSS, JS, etc. Have done a few code academy online courses and using stack overflow / google to try and get to answers, but have only been at this for about a week. – Marc Dec 29 '16 at 19:24
  • php is definitely working. getwisherid($id) is a stored proc on mysql. I will post the code that works, which has it inline: – Marc Dec 29 '16 at 19:28
  • You are not closing your while loop. – Ferran Dec 29 '16 at 19:38
  • 1
    Thank you Ferran - that was it. I added at the end of Works as expected now. – Marc Dec 29 '16 at 19:42
  • @Ferran, perhaps add this as an answer so as to close this question. Marc, it's worth your while investigating [How to display PHP errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). This knowledge will save you so much time and stress. – Martin Dec 29 '16 at 20:09

2 Answers2

2

Some enhancement in your code (along with error reporting and query);-

<?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>
Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
  • Thank you for recommendation on improving code with some valuable error catching. PS: how do I close the question as answered? – Marc Dec 29 '16 at 19:54
1

You can move more into include files, or do something like this:

<?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>
cssyphus
  • 31,599
  • 16
  • 79
  • 97