-3

I've the following PHP code. I wanted to display the output (in my case, it's the student's marks) in proper table that should be readable.

<?php

include_once("config.php");

$enrollmentno = $_POST['enrollmentno'];
$enrollmentno = mysql_real_escape_string($enrollmentno); // For MySQL injection

$result = mysql_query("SELECT * FROM exam WHERE enrollmentno='$enrollmentno'");

if ($row = mysql_fetch_array($exam)) {
    echo $row['enrollmentno'];
    echo $row['studentname'];
    echo $row['schoolname'];
    echo $row['class'];
    echo $row['physics'];
    echo $row['chemistry'];
    echo $row['zeology'];
    echo $row['botany'];
    echo $row['marksscored'];
    echo $row['totalmarks'];
    echo $row['percentagescored'];
} else {
    echo "Invalid Enrollment Number";
}

?>
BadHorsie
  • 13,695
  • 28
  • 110
  • 180
ExTex
  • 3
  • 1
  • so you are asking how to use `` tag ...?
    – Random Feb 09 '17 at 14:49
  • 2
    by *writing a proper HTML table*? you know about HTML, don't you? also: mysql_real_escape_string does **not** protect you from SQL Injection. use PDO or MySQLi instead of the deprecated mysql-functions and reap the benefits of **parameterized statements** – Franz Gleichmann Feb 09 '17 at 14:49
  • 1
    What have you tried? What are you stuck on? This isn't a site to have people do your homework for you. – Carcigenicate Feb 09 '17 at 14:49
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Feb 09 '17 at 14:53
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Feb 09 '17 at 14:54
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Feb 09 '17 at 14:54

3 Answers3

0

If you want an actual HTML table then you can do something like:

if($row = mysql_fetch_array($exam))
{
    echo "<table>"; # Begin the table
    echo "<tr>"; # Begin a row
    echo "<td>" . $row['enrollmentno'] . "</td>"; # First column
    ...
    echo "<td>" . $row['percentagescored'] . "</td>"; # Last column
    echo "</tr>"; # Close the row
    echo "</table>"; # Close the table
}
A C
  • 655
  • 6
  • 8
0

Try this instead your if() statement:

  echo '<table>';
if($row = mysql_fetch_array($exam))
{
echo '<tr>';
echo '<td>'.$row['enrollmentno'].'</td>';
echo '<td>'.$row['studentname'].'</td>';
echo '<td>'.$row['schoolname'].'</td>';
echo '<td>'.$row['class'].'</td>';
echo '<td>'.$row['physics'].'</td>';
echo '<td>'.$row['chemistry'].'</td>';
echo '<td>'.$row['zeology'].'</td>';
echo '<td>'.$row['botany'].'</td>';
echo '<td>'.$row['marksscored'].'</td>';
echo '<td>'.$row['totalmarks'].'</td>';
echo '<td>'.$row['percentagescored'].'</td>';
echo '</tr>';
}
echo '</table>';
0
<?php if ($row = mysql_fetch_array($exam)) : ?>

<table>
    <tr>
        <td><?= $row['enrollmentno']; ?></td>
        <td><?= $row['studentname']; ?></td>
        <td><?= $row['schoolname']; ?></td>
        <td><?= $row['class']; ?></td>
        <td><?= $row['physics']; ?></td>
        <td><?= $row['chemistry']; ?></td>
        <td><?= $row['zeology']; ?></td>
        <td><?= $row['botany']; ?></td>
        <td><?= $row['marksscored']; ?></td>
        <td><?= $row['totalmarks']; ?></td>
        <td><?= $row['percentagescored']; ?></td>
    </tr>
</table>

<?php else : ?>

<p>Invalid Enrollment Number</p>

<?php endif; ?>
BadHorsie
  • 13,695
  • 28
  • 110
  • 180