1

I have two tables in one database. The first one is the g1 where the buttons' data is located. The second is the gradeone, where the enrollees' data is located. I want to display the data from the table "gradeone" by clicking the specific buttons.

Assuming that I added 2 sections. Section 1 and section 2. I click the button section1. By clicking it, I want to display the data of the enrollee from table "gradeone" where the section is 1.

<?php

$dsn = 'mysql:host=localhost;dbname=admin';
$username = 'root';
$password = '';

try{
  // Connect To MySQL Database
  $con = new PDO($dsn,$username,$password);
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (Exception $ex) {
  echo 'Not Connected '.$ex->getMessage();
}
$sectionnumber ="";
$datasuccess ="";
$error ="";

function getPosts(){
  $posts = array();
  $posts[1] = $_POST['sectionnumber'];

  return $posts;
}


if(isset($_POST['add'])){
  $data = getPosts();
  if(empty($data[1])){ 
    $error = 'Enter The User Data To Insert';
  }else {
    $insertStmt = $con->prepare('INSERT INTO g1(sectionnumber) VALUES(:sectionnumber)');
    $insertStmt->execute(array(
                ':sectionnumber'=> $data[1]
    ));
    if($insertStmt){
       $datasuccess = "<font color='#f8234a'>New added</font>";
    }
  }
}

?> //Code for adding a button

<?php
require 'connection.php';

$sql = "SELECT sectionnumber FROM g1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "<button type='button' class='btn'>Section " .     $row["sectionnumber"] . "</button></a><hr>";
  }
} else { echo "<B>No Sections</B>"; }

$con->close();
?> //Code for displaying the button



<html>
<body>
  <form action=">
    <input type="number" name="sectionnumber">
    <input type="submit" name="add">
  </form>
</body>
</html>
MD. Jubair Mizan
  • 1,539
  • 1
  • 12
  • 20
Cassandra
  • 29
  • 9
  • AJAX is your friend. – Difster Dec 24 '18 at 12:11
  • How? I don't have an idea on using such – Cassandra Dec 24 '18 at 12:17
  • Take a look at the [AJAX](https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX) documentation and [this](https://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters) answer for more details. Once you go through these two you'll have a bigger understanding of how to solve this. – guychouk Dec 24 '18 at 12:36

0 Answers0