0

This is my table info in DB.

id         name        age
1          Vale        23      
2          Alex        13
3          Scott       15
4          Tino        18

I have 2 select box and when i pick "Vale" from first and "Scoot" from second selectbox, i like to generate table like this:

 id         name        age
 1          Vale        23      
 3          Scott       15

Selectbox.

<select name="name1">
   <option value="Vale">Vale</option>
   <option value="Tino">Tino</option>
</select>

<select name="name2">
   <option value="Alex">Alex</option>
   <option value="Scoot">Scoot</option>
</select>
.
.
.

My page_to_process.php. What i need more, or where is my errors.

<?php
require('includes/config.php');\\CONNECTION to Database

$sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':name' => $name1]);


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

?>
<table border="0">
   <tr COLSPAN=2 BGCOLOR="#6D8FFF">
      <td>ID</td>
      <td>Name</td>
      <td>Age</td>
   </tr>
 <?php     
 while($row=$sql->fetch()) 
 {
      echo "<tr>".
           "<td>".$row["id"]."</td>".
           "<td>".$row["name"]."</td>".
           "<td>".$row["age"]."</td>".
           "</tr>";
 }

}
else
{
     echo "don't exist records for list on the table";
}

?>
</table>
3
  • 1
    We cannot help you debug your code if you do not provide errors that are coming up. Please post them so we can help you out. Commented Jan 10, 2016 at 16:26
  • @ValentinGjorgoski where did you define $name1 Commented Jan 10, 2016 at 17:38
  • I removed "(EDIT)SOLVED:" from the title in an edit. Accepting a given answer is enough to mark it as solved. Commented Jan 10, 2016 at 18:48

1 Answer 1

1

Try this approach:

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

function get_info($dbh, $name)
{
    $sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
    $sql->setFetchMode(PDO::FETCH_ASSOC);
    $sql->execute([':name' => $name]);
    if ($row = $sql->fetch()) {
        return $row;
    }
    return false;
}

?>


<table border="0">
    <tr COLSPAN=2 BGCOLOR="#6D8FFF">
        <td>ID</td>
        <td>Name</td>
        <td>Age</td>
    </tr>
    <?php
    if (isset($_POST['name1'])) {
        if ($row = get_info($dbh, $_POST['name1'])) {
            echo "<tr>" .
                "<td>" . $row["id"] . "</td>" .
                "<td>" . $row["name"] . "</td>" .
                "<td>" . $row["age"] . "</td>" .
                "</tr>";
        } else {
            echo "don't exist records for list on the table";
        }
    }
    ?>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

I need one more thing. When user ones choise selectbox and table is generated, everytime when his log in, table and selectbox to be the value from first choise. Saved in session or whatever else...
I didnt understand completely, but you can use sessions to persist data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.