can anyone help to check my coding? what i need is to enter the groupcode and when i click search,it will retrieve all the modules. but only one module that appear instead of all.

<?php

$server="localhost";
$username="root";
$password="";
$connect_mysql=mysql_connect($server,$username,$password) or die ("Connection Failed!");
$mysql_db=mysql_select_db("reportdatabase",$connect_mysql) or die ("Could not Connect to Database");
    $db_sch = "SELECT * FROM cweb01group WHERE  `GroupCode` ='$GroupCode'";
    //echo $db_sch;
    $i = 0;
    $res = mysql_query($db_sch) or die(mysql_error());
    //echo mysql_num_rows($res);
    while($row = mysql_fetch_assoc($res))
        {
        echo " <tr>
              <td><a href='testingagain.php?module=".$row['ModuleCode']."</td>
              <td>".$row['ModuleTitle']."</td></br>
              </tr>";

        }
         mysql_close($connect_mysql);
            ?>

thanks :)

Dani AI

Generated

Brief diagnosis and next steps

correctly flagged that $GroupCode was never defined; pointed toward using the posted form value. Beyond that, the most likely cause of “only one module appears” is malformed HTML produced inside the loop: the anchor tag and/or table cells are not closed correctly, so the browser renders the rest of the rows incorrectly. Confirm whether the query actually returns multiple rows (use a row count or dump the result set) and view the page source to see where the markup breaks. Also check there’s no accidental LIMIT in the SQL, and that the posted group value matches the DB (trim and normalize case).

Safer, modern approach (example)

Use parameterized queries and proper output-escaping; this avoids SQL injection and HTML-breakage. Example using mysqli prepared statements:

<?php
$mysqli = new mysqli('localhost','root','','reportdatabase');
if ($mysqli->connect_error) { die('Connect error'); }

$group = isset($_POST['group']) ? trim($_POST['group']) : '';
if ($group === '') { echo '<tr><td colspan="2">No group supplied</td></tr>'; exit; }

$stmt = $mysqli->prepare('SELECT ModuleCode, ModuleTitle FROM cweb01group WHERE GroupCode = ?');
$stmt->bind_param('s', $group);
$stmt->execute();
$stmt->bind_result($code, $title);
while ($stmt->fetch()) {
    echo '<tr><td><a href="testingagain.php?module=' . htmlspecialchars($code) . '">'
         . htmlspecialchars($code) . '</a></td><td>' . htmlspecialchars($title) . '</td></tr>';
}
$stmt->close();
$mysqli->close();
?>

Troubleshooting checklist

  • Enable errors during development: error_reporting(E_ALL); ini_set('display_errors',1);.
  • Print mysqli->affected_rows or row count to verify the query result.
  • View source to find broken tags (missing </a>, stray <br> inside table rows).
  • Avoid deprecated mysql_* functions; prefer mysqli or PDO and prepared statements.
  • Trim/normalize the posted group value to avoid mismatches.

These steps address the undefined variable, potential injection risk, and the broken HTML that most often causes the “only one row shows” symptom.

Recommended Answers

All 3 Replies

Where is $GroupCode being defined? The lack of a definition (due to it being indicative of having register_globals turned on) is more worrysome than the rest of the issues in your code. Let's start with that and work our way through it.

This is my html form. if i insert groupcode and click search it will proceed to the next page which is semMod.php which i send earlier.

<form id="form1" name="form1" method="POST" action="semMod.php">
<p>
  <label for="groupcode"></label>Group Code:
  <input type="text" name="group" id="group" class="intext" />
  eg.CWEB0212  </p>
<p>
  <input name="submit" id="submit" type="submit" value="Search"/><br />

</form>

In that case you have to use this

<?php
$server="localhost";
$username="root";
$password="";
$connect_mysql=mysql_connect($server,$username,$password) or die ("Connection Failed!");
$mysql_db=mysql_select_db("reportdatabase",$connect_mysql) or die ("Could not Connect to Database");
    $db_sch = "SELECT * FROM cweb01group WHERE  `GroupCode` ='".$_POST['group']."'";
    //echo $db_sch;
    $i = 0;
    $res = mysql_query($db_sch) or die(mysql_error());
    //echo mysql_num_rows($res);
    while($row = mysql_fetch_assoc($res)) {
        echo " <tr>
              <td><a href='testingagain.php?module=".$row['ModuleCode']."</td>
              <td>".$row['ModuleTitle']."</td></br>
              </tr>";
        }
mysql_close($connect_mysql);
?>

Anyway, you should check if there is any value in the post request and be aware of the injection possibilities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.