How to generate html table with data for select box from existing db table? PHP&MySql

My error is: “don’t exist records for list on the table” but i have records in table.

When user like to generate a table with data for “predmeti”(subject on english), must picks with select boxes.

***Example if first select box value is “Math”, in database table where column value is “Math”, generate table with that row.

This is my page_to_process.php

*I`m new in php still learning. I’m student and this need for my project.

<?php
function Conect()
{
    if (!($link=mysql_connect("localhost","root","070810125")))
    {
        echo "Error to conect to database.";
        exit();
    }
    if (!mysql_select_db("regusers",$link))
    {
        echo "Error on select database.";
        exit();
    }
    return $link;
}


$subject1=mysql_real_escape_string($_POST['predmet1']);
$subject2=mysql_real_escape_string($_POST['predmet2']);
$link=Conect();
$query="SELECT * FROM raspored WHERE predmet='".$subject1."'";
$action=mysql_query($query,$link) or die("Error: ".mysql_error());


if(mysql_num_rows($action) > 0)
{

?>
<table border="0">
   <tr COLSPAN=2 BGCOLOR="#6D8FFF">
      <td>Name1</td>
      <td>Name2</td>
      <td>Name3</td>
   </tr>
 <?php     
 while($row=mysql_fetch_array($action))
 {
      echo "<tr>".
           "<td>".$row["ID"]."</td>".
           "<td>".$row["predmet"]."</td>".
           "<td>".$row["profesor"]."</td>".
           "</tr>";
 }

}
else
{
     echo "don't exist records for list on the table";
}
mysql_close($link);
?>
</table>

This is my memberpage.php with form

<?php session_start(); 
require('includes/config.php'); 

//пренасочи ако корисникот не е логиран
if(!$user->is_logged_in()){ header('Location: login.php');exit; } 

//дефинирање наслов
$title = 'Профил';

//хедер
require('layout/header.php'); 

?>

<div class="container">

<div class="row">

    <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-   3">

            <h2>Добредојдовте: <?php echo $_SESSION['Fname']; ?> <?php echo     $_SESSION['Lname']; ?></h2>
            <p><a href='logout.php'>Logout</a></p>
            <p><a href='Update.php'>Update</a></p>
            <hr>

    </div>
</div>


</div>
<div class="container">

<div class="row">

    <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset- 3">
        <form role="form" method="post" action="page_to_process.php"  autocomplete="off">
            <h3>Предмети</h3>
            <div class="row">
                <div class="col-xs-6 col-sm-6 col-md-6">
                    <div class="form-group">
                <select required name="predmet1" id="predmet1" class="form-control input-lg">
                    <option value="" class="placeholder" style="display: none;">Предмет1</option>  
                    <option value="Алгоритми и структури на податоци">Алгоритми и структури на податоци</option>  
                    <option value="Конечна Математика">Конечна Математика</option>  
                    <option value="Софтверско инженерство">Софтверско инженерство</option>
                    <option value="Веб програмирање">Веб програмирање</option>
                    <option value="Мултимедиски мрежи">Мултимедиски мрежи</option>
                    <option value="Принципи на мултимедиски системи">Принципи на мултимедиски системи</option>
                    <option value="Проектирање и развој на инф. системи">Проектирање и развој на инф. системи</option>
                    <option value="Инженерска технологија">Инженерска технологија</option>
                </select>
                    </div>
                </div>
                <div class="col-xs-6 col-sm-6 col-md-6">
                    <div class="form-group">
                <select name="predmet2" id="predmet2" class="form-control input-lg">
                    <option value="" class="placeholder" style="display: none;">Предмет2</option>  
                    <option value="Алгоритми и структури на податоци">Алгоритми и структури на податоци</option>  
                    <option value="Конечна Математика">Конечна Математика</option>  
                    <option value="Софтверско инженерство">Софтверско инженерство</option>
                    <option value="Веб програмирање">Веб програмирање</option>
                    <option value="Мултимедиски мрежи">Мултимедиски мрежи</option>
                    <option value="Принципи на мултимедиски системи">Принципи на мултимедиски системи</option>
                    <option value="Проектирање и развој на инф. системи">Проектирање и развој на инф. системи</option>
                    <option value="Инженерска технологија">Инженерска технологија</option>
                </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6 col-sm-6 col-md-6">
                    <div class="form-group">
                <select name="predmet3" id="predmet3" class="form-control input-lg">
                    <option value="" class="placeholder" style="display: none;">Предмет3</option>  
                    <option value="Алгоритми и структури на податоци">Алгоритми и структури на податоци</option>  
                    <option value="Конечна Математика">Конечна Математика</option>  
                    <option value="Софтверско инженерство">Софтверско инженерство</option>
                    <option value="Веб програмирање">Веб програмирање</option>
                    <option value="Мултимедиски мрежи">Мултимедиски мрежи</option>
                    <option value="Принципи на мултимедиски системи">Принципи на мултимедиски системи</option>
                    <option value="Проектирање и развој на инф. системи">Проектирање и развој на инф. системи</option>
                    <option value="Инженерска технологија">Инженерска технологија</option>
                </select>
                    </div>
                </div>
                <div class="col-xs-6 col-sm-6 col-md-6">
                    <div class="form-group">
                <select name="predmet4" id="predmet4" class="form-control input-lg">
                    <option value="" class="placeholder" style="display: none;">Предмет4</option>  
                    <option value="Алгоритми и структури на податоци">Алгоритми и структури на податоци</option>  
                    <option value="Конечна Математика">Конечна Математика</option>  
                    <option value="Софтверско инженерство">Софтверско инженерство</option>
                    <option value="Веб програмирање">Веб програмирање</option>
                    <option value="Мултимедиски мрежи">Мултимедиски мрежи</option>
                    <option value="Принципи на мултимедиски системи">Принципи на мултимедиски системи</option>
                    <option value="Проектирање и развој на инф. системи">Проектирање и развој на инф. системи</option>
                    <option value="Инженерска технологија">Инженерска технологија</option>
                </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6 col-md-6"><input type="submit" name="save" value="Зачувај" class="btn btn-primary btn-block btn-lg" tabindex="5"> </div>
            </div>
        </form>
    </div>
</div>


</div>

<?php 
//футер
require('layout/footer.php'); 
?>

Please forgive the off topic reply.
But if you are a student learning php, why are you using MySql, instead of MySqli or PDO?
MySql is dead and gone, removed from php now, it is obsolete.

Are you sure the records match the value in the form? So there a record where “Алгоритми и структури на податоци” is in the predmet column?

Still, I strongly recommend that you scap this script and learn Msqli or better PDO instead.

Yes record “Алгоритми и структури на податоци” is in the predmet in table predmeti.
I think i don`t have time to learn new thinks right now, like Msqli or PDO. I need to finish this project soon ass possible :slight_smile: All my project is with MySQL

All i need is to show table with data from database table, onlly for some values that i choice with select box, or whatever else.

It is a while since I used Mysql, so I don’t remember the exact syntax and have updated and overwritten all my scripts that used it. So checking that your script is correct is difficult for me.
Maybe someone with a better memory can have a look.

Then all the more reason to switch the code to mysqli_ or PDO
And they aren’t “new” they’ve been around for more than a decade.

1 Like

Please, give it to me some scripts how to solve this with mysqli or PDO to begin make changing

Than please, give it to me code how to solve this with mysqli or PDO to begin make changing

I changed from Mysql to Mysqli some time ago. But just recently I have been changing all my scripts to PDO which is much better.
Here is an itroduction to PDO that I found useful:

After you have set up the connection, the select statement may look something like this:

$sql = $dbh->prepare("SELECT * FROM raspored WHERE predmet = :subject");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':subject' => $subject1]);

That is a prepared statement, which is more secure, but an extra line, one to prepare and one to execute.
To count the results:

if($sql->rowCount() != 0) {}

And the fetch for the while loop:

while($row=$sql->fetch()) {}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.