0

first I'm sorry for my English. I've got a problem with my code. I've got a school assigment to make a Game called Lotto Weekend Miljonairs and I got stuck on one thing. When you start the game you need to fill in some information about yourself, that creates an ID automatically in my php-database but the question is how does it direct you to a tab with the ID?

My code:

//Lotto.php?speler=  nummerid speler &id=1

        // Maken van verbinding database
        try {
          $db = new PDO('mysql:host=localhost;dbname=lotto', 'root','');
        }
        catch(PDOException $e) {
          echo $e->getMessage();
        }

        //nummer check spelers 
        if(!isset($_GET['speler'])) {
            exit('Geen speler');
        }

        //Controle antwoord van vraag
  if(isset($_POST['antwoord']) & isset($_POST['id'])) {

              $id = (int)$_POST['id'];
              $sql = "SELECT * FROM vragen WHERE ID = ".$id;
              $sql = $db->query($sql);
              $row = $sql->fetchAll();
              $row = $row[0];

              if($row['antwoord'] != $_POST['antwoord']) {
        //voortgang speler opslaan 
                $sql = "UPDATE spelers SET voortgang = '" . $id."' WHERE id = '" . $_GET['speler']."'";
        $resultaat = $db->exec($sql);
            header('Location: '.$uri.'/meedoen.html');
                  exit('Fout');
              }

        } elseif(!isset($_GET["ID"]) || $_GET["ID"] === 1) {

        } else {
            exit();
        }
        //ophalen van speler
  $sql = "SELECT * FROM spelers WHERE ID = '".$_GET['speler']."'";
  $sql = $db->query($sql);
    $speler = $sql->fetchAll();
  $speler = $speler[0];

  //Ophalen van (volgende) vraag
  $id = 1;
  if(isset($_GET["id"])) {
     $id = $_GET["id"];
  }

  $sql = "SELECT * FROM vragen WHERE ID = ".$id;
  $sql = $db->query($sql);
    $row = $sql->fetchAll();
    $row = $row[0];
     //volgende vraag bepalen
  $id_next = (int)$id;
    $id_next++;
            echo '<form action="Lotto.php?speler='.$_GET['speler'].'&id='.$id_next.'" method="post">';
                    echo 'vraag: '.$row['vraag'].'<br>';
                  echo '<input name="id" type="hidden" value="'.$id.'" >';
                    echo ' <input type="radio" name="antwoord" value="A" checked> antwoord A: '.$row['A'].'<br>';
                    echo ' <input type="radio" name="antwoord" value="B"> antwoord B: '.$row['B'].'<br>';
                    echo ' <input type="radio" name="antwoord" value="C"> antwoord C: '.$row['C'].'<br>';
                    echo ' <input type="radio" name="antwoord" value="D"> antwoord D: '.$row['D'].'<br>';
                    echo ' <input type="submit" name="verzend"> ';
            echo '</form>';

            //hulpmiddel publiek
           if($speler['publiek']==0) {
                echo 'Publiek gebruikt<br>';
           } elseif(isset($_POST['publiek']) && $_POST['publiek']==true) {

                $sql = "UPDATE spelers SET publiek = '0' WHERE id = '" . $_GET['speler']."'";
                $resultaat = $db->exec($sql);

                $values = publiek();
                echo 'A: '.$values[0].'<br>';
                echo 'B: '.$values[1].'<br>';
                echo 'C: '.$values[2].'<br>';
                echo 'D: '.$values[3].'<br>';

            } else {
                echo '<form action="Lotto.php?speler='.$_GET['speler'].'&id='.$id.'" method="post">';
                echo '<input name="publiek" type="hidden" value="true" >';
                echo ' <input type="submit" value="publiek"> <br>';
                echo '</form>';                   
            }
            //hulpmiddel 5050
           if($speler['halfhalf']==0) {
               echo '5050 gebruikt<br>';
           } elseif(isset($_POST['halfhalf']) && $_POST['halfhalf']==true) {
                $sql = "UPDATE spelers SET halfhalf = '0' WHERE id = '" . $_GET['speler']."'";
                $resultaat = $db->exec($sql);

                $values = halfhalf($id,$db);
                echo $values[0].' is fout<br>';
                echo $values[1].' is fout<br>';
            } else {
                echo '<form action="Lotto.php?speler='.$_GET['speler'].'&id='.$id.'" method="post">';
                echo '<input name="halfhalf" type="hidden" value="true" >';
                echo ' <input type="submit" value="halfhalf"> <br>';
                echo '</form>';                   
            }
            //hulpmiddel hint
           if($speler['hint']==0) {
              echo 'Hint gebruikt<br>';
           } elseif(isset($_POST['hint']) && $_POST['hint']==true) {

                $sql = "UPDATE spelers SET hint = '0' WHERE id = '" . $_GET['speler']."'";
                $resultaat = $db->exec($sql);
              echo ''.$row['hint'].'<br>';

            } else {
                echo '<form action="Lotto.php?speler='.$_GET['speler'].'&id='.$id.'" method="post">';
                echo '<input name="hint" type="hidden" value="true" >';
                echo '<input type="submit" value="hint"> <br>';
                echo '</form>';                   
            }

        function publiek() {

          $values = array();
          $total = 100;

          for($i=0;$i<3;$i++) {
            $temp = rand(0, $total-1);
            $values[$i] = $temp;      
            $total = $total - $temp;
          }              

          $values[3] = $total;
          return $values;
        }

        function halfhalf($id,$db) {

            $sql = "SELECT * FROM vragen WHERE ID = ".$id;
            $sql = $db->query($sql);
            $row = $sql->fetchAll();
            $row = $row[0];

            $values = array();

            while(!isset($values[0])) {
                $value = nummervraag(rand(0,3));
                if($value!==$row['antwoord']) {
                    $values[0] = $value;
                }
            }

            while(!isset($values[1])) {
                $value = nummervraag(rand(0,3));
                if($value!==$row['antwoord'] && $value!==$values[0]) {
                    $values[1] = $value;
                }
            }

            return $values;
        }

        function nummervraag($nummer) {

            switch($nummer) {
                case 0:
                    return 'A';
                case 1:
                    return 'B';
                case 2:
                    return 'C';
                case 3:
                    return 'D';
            }

        }

        ?>

My SQL:

    -- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Gegenereerd op: 22 mrt 2017 om 10:42
-- Serverversie: 10.1.19-MariaDB
-- PHP-versie: 5.6.24

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `lotto`
--

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `spelers`
--

CREATE TABLE `spelers` (
  `ID` int(11) NOT NULL,
  `voornaam` text NOT NULL,
  `achternaam` text NOT NULL,
  `leeftijd` int(11) NOT NULL,
  `woonplaats` text NOT NULL,
  `bellen` int(11) NOT NULL DEFAULT '1',
  `publiek` int(11) NOT NULL DEFAULT '1',
  `halfhalf` int(11) NOT NULL DEFAULT '1',
  `voortgang` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Gegevens worden geëxporteerd voor tabel `spelers`
--

INSERT INTO `spelers` (`ID`, `voornaam`, `achternaam`, `leeftijd`, `woonplaats`, `bellen`, `publiek`, `halfhalf`, `voortgang`) VALUES
(133, 'hhfgh', 'fgh', 34, 'dfg', 1, 1, 1, 0),
(134, 'Wessel', 'Minnen', 16, 'Oosterbeek', 1, 1, 1, 0),
(135, '', '', 0, '', 1, 1, 1, 0),
(136, '', '', 0, '', 1, 1, 1, 0),
(137, 'Wessel', 'Minnen', 10, 'wwe', 1, 1, 1, 0);

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `vragen`
--

CREATE TABLE `vragen` (
  `ID` int(11) NOT NULL,
  `vraag` text NOT NULL,
  `A` text NOT NULL,
  `B` text NOT NULL,
  `C` text NOT NULL,
  `D` text NOT NULL,
  `antwoord` text NOT NULL,
  `hint` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Gegevens worden geëxporteerd voor tabel `vragen`
--

INSERT INTO `vragen` (`ID`, `vraag`, `A`, `B`, `C`, `D`, `antwoord`, `hint`) VALUES
(1, 'hoeveel poten?', '4', '5', '6', '7', 'C', 'gggg'),
(2, 'hoeveel scholen zijn er in nl?', '5000', '100000', '15', '1000000', 'A', 'dit is een test'),
(3, 'hoelang is een chinees?', '80cm', '90cm', '100cm', 'hoelang', 'D', 'het is een chinees'),
(4, 'wat is het gem. inkomen per uur?', '5 euro', '60000 euro', '7 euro', '8 euro', 'B', 'veel geld'),
(5, 'wie is de rijkste rapper van de wereld?', 'biggie', 'ali b', 'eminem', 'miley cyrus', 'C', 'afkomst is Amerikaans');

--
-- Indexen voor geëxporteerde tabellen
--

--
-- Indexen voor tabel `spelers`
--
ALTER TABLE `spelers`
  ADD PRIMARY KEY (`ID`);

--
-- Indexen voor tabel `vragen`
--
ALTER TABLE `vragen`
  ADD PRIMARY KEY (`ID`),
  ADD KEY `ID` (`ID`);

--
-- AUTO_INCREMENT voor geëxporteerde tabellen
--

--
-- AUTO_INCREMENT voor een tabel `spelers`
--
ALTER TABLE `spelers`
  MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=138;
--
-- AUTO_INCREMENT voor een tabel `vragen`
--
ALTER TABLE `vragen`
  MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!

40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

My php code to add a player:

<!DOCTYPE html>
<html>
<head>
    <title>Lotto weekend miljonairs</title>
</head>
<body>
 <?php
 // Maken van verbinding
 try {
 $db = new PDO('mysql:host=localhost;dbname=lotto', 'root','');
 }
 catch(PDOException $e) {
 echo $e->getMessage();
 }
 // De SQL opdracht
 $sql = "INSERT INTO spelers (voornaam, achternaam, leeftijd, woonplaats)
 VALUES ('$_POST[voornaam]','$_POST[achternaam]', '$_POST[leeftijd]','$_POST[woonplaats]')";
 $resultaat = $db->exec($sql);
 // Controle van de toegevoegde gegevens
 $sql = "SELECT * FROM `spelers` where voornaam=$_POST[voornaam]";
 $resultaat = $db->query($sql);
 foreach ($resultaat as $resultaat) {
    echo '<p>';
    echo 'Voornaam:' .$resultaat['voornaam'].'<br>';
    echo 'Achternaam:' .$resultaat['achternaam'].'<br>';
    echo 'Leeftijd:' .$resultaat['leeftijd'].'<br>';
    echo 'Woonplaats:' .$resultaat['woonplaats'].'<br>';
    echo '</p>';
 }
    echo "<p>is toegevoegd!</p>";
    echo "<p><form action='spel.php' method='post'>
        <input type='submit' name='submit' value='Klik hier om te beginnen!' title='Klik hier om te beginnen!' tabindex='6'>
        </form></p>";
    $db = NULL;
 ?> 
    <form action="SEG.html" method="post">
    <input type="submit" name="terug" value="Terug naar het menu." title="Terug" tabindex="7">
</form>
</body>
</html>
N kubo
  • 1
  • 1
  • 2
  • If you need more of my code please let me know. – N kubo Mar 23 '17 at 08:17
  • What is the problem with your current code? Please provide more detail. – ACEG Mar 23 '17 at 09:18
  • So I have a form that people first need to fill in to play the "game" and this form automaticlly makes a ID for the person in my myphpdatabase. After the form you will get directed to a tab where you can start the game. If you start the game it should bring you to the game but in this code you can only start the game if there is a ID but it doesnt take it. localhost/lotto.php?speler=(ID). So i want to know how to do that – N kubo Mar 23 '17 at 10:48
  • Rather than more information, you actually need to give us *less*. Look at these tips for creating a [mcve]. If the question is "How do I redirect to a different URL", we don't need to know *why* you're redirecting, or what your SQL looks like (and [how insecure it is!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)). Once you break down the question, you may be able to [search for an existing question](http://stackoverflow.com/help/searching) rather than asking a new one. – IMSoP Mar 23 '17 at 11:41
  • Okay thanks alot for your tip! I've added my SQL aswell and I already tried to search something in the same way. – N kubo Mar 24 '17 at 08:53
  • My question: I have a assigment for school to make a game where you fill in a form, the information will add in my database and automatically adds a ID. After this form it will direct you to the game but it doesnt take the ID from the form. So my question is How does it take the ID from the player? – N kubo Mar 24 '17 at 08:56

0 Answers0