1

I'm trying to create a working update page in php. I am fairly confident there is nothing wrong with my syntax but I keep getting the error "Undefined variable" for the values I am putting in the input fields. Please help!

<?php
  require("connect.php");

  if (isset($_POST['submit'])) {
    $udpdateQuery = "UPDATE league SET `LeagueName` = :name, `LeagueCountry` = :country, `LeagueSize` = :size WHERE `LeagueID` = :leagueID";
    $stmt3 = $con->prepare($udpdateQuery);
    $stmt3->execute(array('name' => $_POST['l_name'], 'country' => $_POST['l_country'], 'size' => $_POST['l_size']));
    $stmt4->closeCursor();
  }

  if (isset($_GET['LeagueID'])) {
    $valuesQuery = "SELECT `LeagueName`, `LeagueCountry`, `LeagueSize` FROM league WHERE `LeagueID` = :leagueID";
    $stmt4 = $con->prepare($valuesQuery);
    $stmt4->execute(array('leagueID' => $_GET['LeagueID']));
    $result = $stmt4->fetchAll();
    $stmt4->closeCursor();

    foreach ($result as $r) {
      $name = $r['LeagueName'];
      $country = $r['LeagueCountry'];
      $size = $r['LeagueSize'];
    }
  }
  ?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>League</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
  </head>
  <body>
        <div class="wrapper">
        <form class="add" action="" method="post">
          <label>League Name</label>
          <input type="text" name="l_name" value="<?=$name?>" required><br>

          <label>League Country</label>
          <input type="text" name="l_country" value="<?=$country?>" required><br>

          <label>League Size</label>
          <input type="text" name="l_size" value="<?=$size?>" required><br>

          <input id="submit" name="submit" type="submit" value="Submit">
        </form>

        <?php include('home.php'); ?>

      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript" src="resources/js/main.js"></script>
  </body>
</html>
Cian
  • 61
  • 7
  • Not related to the error, but you are calling $stmt4->closeCursor(); in the POST handler, instead of $stmt3->closeCursor(); – David Jan 18 '18 at 21:24

2 Answers2

0

The first time your code runs, it enters only in the $_GET part. After submitted, it enters only in the $_POST part. That's why it's showing an error, because the variables are not defined.

You need to:

  1. Add a hidden field to your LeagueID:

    <input type="hidden" name="l_id" value="<?=$id?>"><br>
    
  2. Fix your update:

    $stmt3->execute(array('name' => $_POST['l_name'], 'country' => $_POST['l_country'], 'size' => $_POST['l_size'], 'leagueID' => $_POST['l_id']));
    
  3. Create your variables properly depending if it's get or post:

    if (isset($_POST['submit'])) {
        $id = $_POST['l_id'];
        $name = $_POST['l_name'];
        $country = $_POST['l_country'];
        $size = $_POST['l_size'];
    
    ...
    
    if (isset($_GET['LeagueID'])) {
    ...
        foreach ($result as $r) {
            $id = $_GET['LeagueID'];
            $name = $r['LeagueName'];
            $country = $r['LeagueCountry'];
            $size = $r['LeagueSize'];
        }
    
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • I'm sorry but I'm confused, I'm relatively new to php. Is is an update page so I want to display the original values of the entry in the input fields so that the user can 'update' them. Which would require retrieving it back from the database referencing which entry it is via what entry I have chosen to update. My question is, where in the code am I referencing the entry I have selected to do so? I am in no way criticizing your solution, I'm just dumb and don't get it haha – Cian Jan 18 '18 at 21:30
0

It looks like you could be running into a handful of issues here.

  1. In your first if statement, you're closing the wrong cursor:

    $stmt4->closeCursor();
    

    This one hasn't been defined; this should be $stmt3

  2. The variables in the form, $name, $country and $size are only defined in the second if block (for $_GET, if there is a LeagueID set and there's a result for it). If neither of those are true, the variables don't get defined - and when you submit the form, neither are true.

    To help prevent this, define them outside of the if block:

    $name = '';
    $country = '';
    $size = '';
    if (isset($_GET['LeagueID'])) {
    

    And then, update your form to also pass the LeagueID when submitting:

    <form action="/path?LeagueID=<?= htmlentities($_GET['LeagueID']) ?>" method="post" class="add">
    
newfurniturey
  • 37,556
  • 9
  • 94
  • 102