从URL中的$ _GET中检索变量时的未定义索引

时间:2018-05-26 04:07:07

标签: php mysql sql mysqli

好的,所以我几乎完成了这个脚本,但是当我尝试在“网站”上提交页面时,我遇到了一个问题,即我收到了一个未定义的索引错误。 index,用于$ _GET以从URL获取它,以及提交。

<?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>

到目前为止,你们对我的其他错误感到惊讶,我希望你能指出我正确的方向。而且我知道,我应该使用准备好的陈述,但我现在只是学习如何做,因为我已经被告知他们是什么。我会在适当的时候解决这个问题。现在,我只需要让这个更新页面正常工作。

感谢。

2 个答案:

答案 0 :(得分:1)

什么是$ sitenumber = $ _GET ['site']; //这里的未定义索引应该是什么时候没有设置?什么是默认值?

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

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

看起来你可能想要:

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

因为你在顶部定义了var。

答案 1 :(得分:0)

这里的问题是GET [&#39; site&#39;]可以在这些位置取消设置。 你有一个很好的检查,看看是否在代码的第一部分设置了$ _GET [&#39; site&#39;],但是你绝对确定$ _GET [&#39; site&#39;实际上是在PHP结束标记之前设置了结束。之后,$ _GET [&#39;网站&#39;]永远不会再次检查是否存在。

有两种方法可以解决这个特殊问题:
您可以将默认值设置为$sitenumber,并在您需要$ _GET [&#39;网站&#39;]的代码中的所有位置使用sitenumber 或者你也把第二部分放在if语句中,这意味着你需要复制当前的HTML并粘贴它而不用$ _GET [&#39; site&#39;]以及它所依赖的一切

我希望你应该设置一个默认值。最简单的方法是使用null coalesce operator,这意味着您需要进行两次小的更改。
第一个也是最明显的:将$sitenumber = $_GET['site']; //Undefined index here更改为$sitenumber = $_GET['site'] ?? "your default value" ;
第二项更改为<td><input type="hidden" name="site" value="<?php echo $_GET['site'];?>"></td><td><input type="hidden" name="site" value="<?php echo $sitenumber?>"></td>