提交表单后的未定义索引

时间:2014-01-24 16:47:38

标签: php url foreach indexing undefined


<?php

include 'includes/connectie.php';

$product_id=$_GET['id'];


    $sql = "SELECT * FROM  `producten` WHERE product_id='$product_id'";
    $sql_result = $dbh->query($sql);

    foreach($sql_result as $row)
        {   
            $prijs=$row['prijs'];
            $product_naam=$row['product_naam'];
            $product_categorie=$row['product_categorie'];
            $product_specificaties=$row['product_specificaties'];
            $foto=$row['foto'];
            $product_id=$row['product_id']; 
            $product_soort=$row['product_soort'];


            echo "Product id nummer:", $product_id;

        }

//$_SESSION['prijs'] = $prijs;


if ($_SERVER["REQUEST_METHOD"] == "POST"){

if (!empty($product_naam) && !empty($product_specifcaties) && !empty($product_categorie) &&     
!empty($prijs)
&& !empty($product_soort)) {
    print "Product aangepast!";
    $sql = "UPDATE producten 
    SET prijs='$prijs', product_naam='$product_naam', 
    product_specificaties='$product_specificaties',
    product_categorie='$product_categorie', product_soort='$product_soort'
    WHERE product_id='$product_id'";

    $query = $db->prepare( $sql );
    $result = $query->execute();
    exit();
  }



}

?>


<html>
<form name="admin" action="producten_echt_aanpassen.php" method="POST" id="adminform" enctype="multipart/form-data">

    <p>
        <label for 'product_naam'>Naam: </label><br>
        <input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
    </p>
    <p> <label for 'product_specificaties'>Specificaties: </label><br>
        <textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
        </textarea>
    </p>
    <p>
        <label for 'prijs'>Prijs: </label><br>
        <input type="text" name="prijs" value="<?php print $prijs; ?>"/>
    </p>
        <p>
        <label for 'product_categorie'>Iphone: </label><br>
        <input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
    </p>
    <p>
        <label for 'product_soort'>Soort: </label><br>
        <input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
    </p>
      <br/>
        <label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
        <input type="file" name="file" ><br><br>


        <input type="submit" name="submit" value="Submit">

</form>
</html>

变量以可以更改产品详细信息的形式加载。表单链接到此页面上面的代码。但每当我提交表单并尝试更改te细节时,我都会收到一个未定义索引的错误。这是$ _GET在第5行中的作用.foreach循环需要定义索引,但是无论何时提交表单,URL中的索引都会消失,因此循环不会生成需要转到数据库的变量。我希望这是有道理的。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

  1. 您的html不包含名为id的字段。
  2. 您发送的表单为POST而不是GET,因此在HTML中添加正确的字段后,您需要将其称为$product_id = $_POST['id'];
  3. 不需要enctype="multipart/form-data",只有在您上传文件时才有用。否则会导致问题。

    <p>
        <label for 'product_naam'>Naam: </label><br>
        <input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
    </p>
    <p> <label for 'product_specificaties'>Specificaties: </label><br>
        <textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
        </textarea>
    </p>
    <p>
        <label for 'prijs'>Prijs: </label><br>
        <input type="text" name="prijs" value="<?php print $prijs; ?>"/>
    </p>
     <p>
        <label for 'product_id'>Product ID: </label><br>
        <input type="text" name="id" value="<?php print $product_id; ?>"/>
    </p>
        <p>
        <label for 'product_categorie'>Iphone: </label><br>
        <input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
    </p>
    <p>
        <label for 'product_soort'>Soort: </label><br>
        <input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
    </p>
      <br/>
        <label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
        <input type="file" name="file" ><br><br>
    
    
        <input type="submit" name="submit" value="Submit">
    

  4. 希望这有帮助!