-2

I have a html file showing a quiz webside with questions and answers and a php file getting the questions from my local mysql server.

The html file and the php file are working fine, but now I want to get the data from the server when the side is loaded and put the data into the right buttons and labels (Loading a question and the answers and display it on my prefabricated html file).

This is my php code how i get the question from my database:

$round = $_POST['round'];

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}else{
    $sql = "select * from questions where round = $round order by rand() 
limit 1;";
    while($row = mysql_fetch_assoc($sql)) {
        echo $row['id'] . " " . $row['category'] . " " . $row['round'] . 
" " . $row['question'] . " " . $row['rightanswer'] . " " . 
$row['wronganser1'] . " " . $row['wronganser2'] . " " . 
$row['wronganser3'];
}...

and this is my html file displaying the label and buttons with the question and answers:

<div id="question">
                    <h4 id="category">Kunst und Literatur</h4>
                    <p style="margin-bottom: 0px"         class="question">Question=</p>
                </div>
                <ul id="answers">
                    <li id="answer1" class="button">Answer1</li>
                    <li id="answer2" class="button">Answer2</li>
                    <li id="answer3" class="button">Answer3</li>
                    <li id="answer4" class="button" style="margin-bottom:     0px">Answer4</li>
                </ul>...

Instead of "Question" and "Answer1","Answer2"... i want the data loaded from my server.

I appreciate every idea and if anything is unclear don't hesitate to ask.

Thanks :)

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
Pumpanickel
  • 91
  • 2
  • 10
  • 5
    Please read about [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection). Instead of building queries with string concatenation, use [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) with [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). See [**this page**](https://phptherightway.com/#databases) and [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for some good examples. – Alex Howansky Jun 24 '19 at 16:03
  • 4
    PS `mysql_` is a deprecated library - use `mysqli_` instead, or PDO) – Ed Heal Jun 24 '19 at 16:08
  • ...do you also have a answer to my question? – Pumpanickel Jun 24 '19 at 17:12

1 Answers1

1

If you need to put some content from the database into your webpage you can just replace the HTML code like that:

<div id="question">
                <h4 id="category">Kunst und Literatur</h4>
                <p style="margin-bottom: 0px"         class="question"><?php echo $row['question']; ?></p>
            </div>
            <ul id="answers">
                <li id="answer1" class="button"><?php echo $row['answer1']; ?></li>
                <li id="answer2" class="button"><?php echo $row['answer2']; ?></li>
                <li id="answer3" class="button"><?php echo $row['question']; ?></li>
                <li id="answer4" class="button" style="margin-bottom:     0px">Answer4</li>
            </ul>...

If you want to make a site with more questions just wrap your while loop over the HTML code.

Cyberduck
  • 385
  • 6
  • 20