1

Ok, so I almost have this script finished, but I'm having an issue where I am getting an Undefined Index error when I try to submit the page on the 'site' index, used in both the $_GET to get it from the URL, and the submit.

<?php 
// including the database connection file
include_once("config.php");
$sitenumber = "";
$videolink = "";
$daynight = "";
$maxtents = "";
$maxpersons = "";
$geography = "";
$view = "";
$forestcover = "";
$waterfront = "";
$firepit = "";
$description = "";
$reslink = ""; 

if(isset($_POST['update']) && isset($_GET['site']))
{    
$sitenumber = $_POST['sitenumber'];
$videolink = $_POST['videolink'];
$daynight = $_POST['daynight'];
$maxtents = $_POST['maxtents'];
$maxpersons = $_POST['maxpersons'];
$geography = $_POST['geography'];
$view = $_POST['view'];
$forestcover = $_POST['forestcover'];
$waterfront = $_POST['waterfront'];
$firepit = $_POST['firepit'];
$description = $_POST['description'];
$reslink = $_POST['reslink'];   

// checking empty fields
if(empty($sitenumber) || empty($videolink) || empty($daynight) || 
empty($maxtents) || empty($maxpersons) || empty($geography) || 
    empty($view) || empty($forestcover) || empty($waterfront) || 
empty($firepit) || empty($description) || empty($reslink)) {                
    if(empty($sitenumber)) {
        echo "<font color='red'>Site Number field is empty.</font><br/>";
    }

    if(empty($videolink)) {
        echo "<font color='red'>YouTube Link field is empty.</font><br/>";
    }

    if(empty($daynight)) {
        echo "<font color='red'>Day or overnight field is empty.</font> 
<br/>";
    }        

    if(empty($maxtents)) {
        echo "<font color='red'>Maximum Tents field is empty.</font><br/>";
    }

    if(empty($maxpersons)) {
        echo "<font color='red'>Maximum Persons field is empty.</font> 
<br/>";
    }        

    if(empty($geography)) {
        echo "<font color='red'>Geography field is empty.</font><br/>";
    }

    if(empty($view)) {
        echo "<font color='red'>View field is empty.</font><br/>";
    }        

    if(empty($forestcover)) {
        echo "<font color='red'>Forest Cover field is empty.</font><br/>";
    }

    if(empty($waterfront)) {
        echo "<font color='red'>Waterfront Access field is empty.</font> 
<br/>";
    }  

    if(empty($firepit)) {
        echo "<font color='red'>Firepit field is empty.</font><br/>";
    }        

    if(empty($description)) {
        echo "<font color='red'>Description field is empty.</font><br/>";
    }

    if(empty($reslink)) {
        echo "<font color='red'>Reservation Link Access field is empty. 
</font><br/>";
    }       
} else {    
    //updating the table
    $result = mysqli_query($mysqli, "UPDATE sites SET 

sitenumber='".$sitenumber."',videolink='".$videolink."'
,daynight='".$daynight."',
maxtents='".$maxtents."'
,maxpersons='".$maxpersons."'
,geography='".$geography."', 

view='".$view."',forestcover='".$forestcover."',
waterfront='".$waterfront."', 

firepit='".$firepit."',description='".$description."',reslink='".$reslink."' 
WHERE sitenumber='".$sitenumber."'");

    //redirectig to the display page. In our case, it is index.php
    header("Location: index.php");
  }
}
        echo mysqli_error($mysqli);
?>
<?php
//getting id from url

$sitenumber = $_GET['site']; //Undefined index here

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE 
sitenumber='".$sitenumber."'");

while($res = mysqli_fetch_array($result))
{
$sitenumber = $res['sitenumber'];
$videolink = $res['videolink'];
$daynight = $res['daynight'];
$maxtents = $res['maxtents'];
$maxpersons = $res['maxpersons'];
$geography = $res['geography'];
$view = $res['view'];
$forestcover = $res['forestcover'];
$waterfront = $res['waterfront'];
$firepit = $res['firepit'];
$description = $res['description'];
$reslink = $res['reslink'];
}
            echo mysqli_error($mysqli);
?>
<html>
<head>    
<title>Edit Data</title>
</head>

<body>
<a href="index.php">Home</a>
<br/><br/>

<form name="form1" method="post" action="edit.php">
    <table border="0">
    <tr> 
            <td>Site Number</td>
            <td><input type="number" name="sitenumber" value=<?php echo 
$sitenumber;?>></td>
        </tr>
        <tr> 
            <td>YouTube Link</td>
            <td><input type="url" name="videolink" value="<?php echo 
$videolink;?>"></td>
        </tr>
        <tr> 
            <td>Day or Overnight</td>
            <td><select name="daynight" value="<?php echo $daynight;?>">
            <option value="Day">Day</option>
            <option value="Overnight">Overnight</option></td>

        </tr>
                    <tr> 
            <td>Maximum Tents</td>
            <td><input type="number" name="maxtents" value="<?php echo 
 $maxtents;?>"></td>
        </tr>
                    <tr> 
            <td>Maximum Persons</td>
            <td><input type="number" name="maxpersons" value="<?php echo 
 $maxpersons;?>"></td>
        </tr>
                    <tr> 
            <td>Geography</td>
            <td><input type="text" name="geography" value="<?php echo 
 $geography;?>"></td>
        </tr>
                    <tr> 
            <td>View</td>
            <td><input type="text" name="view" value="<?php echo $view;?>"> 
 </td>
        </tr>
                    <tr> 
            <td>Forest Cover</td>
            <td><input type="text" name="forestcover" value="<?php echo 
 $forestcover;?>"></td>
        </tr
                    <tr> 
            <td>Waterfront Access</td>
            <td><select name="waterfront" value="<?php echo $waterfront;?>">
            <option value="Yes">Yes</option>
            <option value="No">No</option></td>
        </tr>
                    <tr> 
            <td>Firepit Availability</td>
            <td><select name="firepit" value="<?php echo $firepit;?>">
            <option value="Yes">Yes</option>
            <option value="No">No</option></td>
        </tr>
                    <tr> 
            <td>Site Description</td>
            <td><input type="text" name="description" value="<?php echo 
 $description;?>"></td>
        </tr>
                    <tr> 
            <td>Reservation Link</td>
           <td><input type="url" name="reslink" value="<?php echo $reslink;? 
 >"></td>
        </tr>
            <td><input type="hidden" name="site" value="<?php echo 

 $_GET['site'];?>"></td>  //Undefined here

            <td><input type="submit" name="update" value="Update"></td>

        </tr>
    </table>
</form>
</body>
</html>

So far you guys have been amazing with my other errors, I'm hoping you can point me in the right direction. And I know, I should use prepared statements, but I'm just learning how to do them now that I've been told what they are. I will fix that in due time. Right now, I just need to get this update page working.

Thanks.

  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jelmergu May 26 '18 at 04:40

2 Answers2

1

What is $sitenumber = $_GET['site']; //Undefined index here supposed to be when it's not setup yet? What si the default value?

$sitenumber = $_GET['site'] ?? 'DEFAULT VALUE';

same as

$sitenumber = isset($_GET['site']) ? $_GET['site'] : 'DEFAULT VALUE';

Looks like you might want:

$sitenumber = $_GET['site'] ?? $sitenumber;

Since you define that var at the top.

Danny
  • 1,185
  • 2
  • 12
  • 33
  • Well I managed to solve the issue by using isset, but the problem I'm running into now is that the script won't update my database when I hit the update button. – Damien Borden May 26 '18 at 04:36
0

The problem here is that GET['site'] can be unset at those positions. You have a nice check to see if $_GET['site'] is set at the first part of the code, but the part where you are absolutely sure that $_GET['site'] is actually set ends just before the PHP end tag. After that $_GET['site'] is never checked again for if it exists.

There are two ways to solve this particular problem:
Either you set a default value to $sitenumber and use sitenumber at all places in you code where you need the $_GET['site']
Or you also put the second part inside an if statement, which means that you'd need to copy the current HTML and paste it without the $_GET['site'] and everything it depends on

I hope it is clear that you should just set a default value. The easiest way would be to use a null coalesce operator, which will mean that you'd need to make two small changes.
The first and most obvious: change $sitenumber = $_GET['site']; //Undefined index here to $sitenumber = $_GET['site'] ?? "your default value" ;
The second change is <td><input type="hidden" name="site" value="<?php echo $_GET['site'];?>"></td> to <td><input type="hidden" name="site" value="<?php echo $sitenumber?>"></td>

Jelmergu
  • 973
  • 1
  • 7
  • 19