Search code examples
phpsqlindexingoffset

Undefined index and undefined offset for form


I'm a new student in development and for myself I created a website where I can put my results when I did some sport (bicycle for example), but I have many problems in there. The first one: Undefined index on lines 34 and 35 The second one: Undefined offset of every input of my form

You can check my code here:

<?php
    session_start();
    //Connexion
    include('connexionBDD.php');
    //Form
    if(isset($_POST['enregistrement'])) {

        if($_POST['seance'] == 'new') {
            //REQ (INSERT)
            $req = "INSERT INTO `donnees_velo` (`SEANCE`, `DATE`, `CALORIES`, `DUREE`,`DISTANCE`,`PUISSANCE`, `CALORIES PAR HEURE`,`MOVEs`,`FCMOY`,`FCMAX`,`PERFORMANCE`,`VITESSE`) 
                    VALUES (NULL, '".$_POST['date']."','".$_POST['calories']."', '".$_POST['duree']."', '".$_POST['distance']."','".$_POST['puissance']."', '".$_POST['calories_heure']."', '".$_POST['MOVEs']."','".$_POST['FCMOY']."', '".$_POST['FCMAX']."', '".$_POST['PERFORMANCE']."','".$_POST['VITESSE']."');";
                    echo $req;
        } else {
            echo $req = "UPDATE `donnees_velo` 
                    SET `DATE` = '".$_POST['date']."', 
                        `calories` = '".$_POST['calories']."'
                        `DUREE` = '".$_POST['duree']."'
                        `DISTANCE` = '".$_POST['distance']."'
                        `CALORIES PAR HEURE` = '".$_POST['calories_heure']."'
                        `MOVEs` = '".$_POST['MOVEs']."'
                        `FCMOY` = '".$_POST['FCMOY']."'
                        `FCMAX` = '".$_POST['FCMAX']."'
                        `PERFORMANCE` = '".$_POST['PERFORMANCE']."'
                        `VITESSE` = '".$_POST['VITESSE']."'
                        WHERE `seance` = '".$_POST['idvelo']."'";
                        echo $req;
        }

        //EXE REQ (QUERY)
        $db->query($req);
        header('Location: index.html');
        exit();
    }
    if($_GET['seance'] != 'new') {
        $req = "SELECT * FROM `donnees_velo` WHERE `seance` = '".$_GET['id']."'";

        //REQ LAUNCHING
        $rep = $db->query($req);

        //REQ TO TBL
        $tbl_velo = $rep->fetchAll();
    } else {
        $tbl_velo[0]['id'] = 'new';
        $tbl_velo[0]['date'] = '';
        $tbl_velo[0]['calories'] = '';
        $tbl_velo[0]['duree'] = '';
        $tbl_velo[0]['distance'] = '';
        $tbl_velo[0]['calories_heure'] = '';
        $tbl_velo[0]['MOVEs'] = '';
        $tbl_velo[0]['FCMOY'] = '';
        $tbl_velo[0]['FCMAX'] = '';
        $tbl_velo[0]['PERFORMANCE'] = '';
        $tbl_velo[0]['VITESSE'] = '';
    }
    var_dump($tbl_velo);
?>


<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="test.css">
<link rel="stylesheet" href="pagedeux.css">
<title>Vélo</title>
</head>
<body>
<div class="overlay"></div>
    <p class="line-1 anim-typewriter">Le vélo</p>
    <form action="velo.php" method="POST" >
    <input type="hidden" value="<?php echo $tbl_velo[0]['SEANCE']; ?>" name="seance">
    Date : <input name="date" value="<?php echo $tbl_velo[0]['date']; ?>" type="date"><br/>
    Calories : <input name="calories" value="<?php echo $tbl_velo[0]['calories']; ?>" type='text'><br/>
    Durée : <input name="duree" value="<?php echo $tbl_velo[0]['duree']; ?>" type="text">
    Distance : <input name="distance" value="<?php echo $tbl_velo[0]['distance']; ?>" type="text">
    Calories par heure : <input name="calories_heure" value="<?php echo $tbl_velo[0]['calories_heure']; ?>" type="text">
    MOVEs : <input name="MOVEs" value="<?php echo $tbl_velo[0]['MOVEs']; ?>" type="text">
    Fréquence cardiaque moyenne : <input name="FCMOY" value="<?php echo $tbl_velo[0]['FCMOY']; ?>" type="text">
    Fréquence cardiaque mxaimale : <input name="FCMAX" value="<?php echo $tbl_velo[0]['FCMAX']; ?>" type="text">
    Performance : <input name="PERFORMANCE" value="<?php echo $tbl_velo[0]['PERFORMANCE']; ?>" type="text">
    Vitesse : <input name="VITESSE" value="<?php echo $tbl_velo[0]['VITESSE']; ?>" type="text">
  <input type="submit" name="enregistrement" value="Envoyer">
</form>

Thank you a lot!


Solution

  • You are fetching the 'seance' and 'id' from your $_GET variable, while the two of them are present in your $_POST variable due to being sent from a form.
    You should replace the instances of $_GET by $_POST in these two places.
    Furthermore, be careful about your $_POST['id'] variable which is susceptible to SQL Injection, I'd recommend sanitizing it.