0

我的脚本有问题,它是几年前在 Uni 中为作业创建的,当时它运行良好,但我没有收到此通知,现在对于每个字段,我都会收到“通知:未定义索引”。

我读过这与未能声明变量有关,我需要起诉 isset 吗?但我仍然不确定如何在代码中实现它。

这是代码:

<?php
require 'server.php';

$results = $dbh->prepare('SELECT * FROM details WHERE id = :id');
$results->execute(array(
     ':id' => $_GET['id'],
     ));

if (isset($_POST['submit'] )) header("Location: template/header.php"); {
    $update = $dbh->prepare('UPDATE details SET firstname = :firstname, surname = :surname, houseno = :houseno, street = :street, town= :town,
           county = :county, postcode = :postcode,  mobile = :mobile, nickname = :nickname, website = :website,
           homephone = :homephone WHERE id = :id');
    $update->execute(array(
        ':id' => $_POST['id'],
        ':firstname' => $_POST['firstname'],
        ':surname' => $_POST['surname'],
        ':houseno' => $_POST['houseno'],
        ':street' => $_POST['street'],
        ':town' => $_POST['town'],
        ':county' => $_POST['county'],
        ':postcode' => $_POST['postcode'],
        ':mobile' => $_POST['mobile'],
        ':nickname' => $_POST['nickname'],
        ':website' => $_POST['website'],
        ':homephone' => $_POST['homephone'],    
    ));

    $row = $results->fetch();
}

require 'template/header.php';
?>

关于如何解决问题的任何想法?图片在这里:

https://dl.dropbox.com/u/10062971/phpnotice.JPG

4

2 回答 2

0

在使用或isset上的任何索引之前,用于检查它们是否存在。 $_POST$_GET

if (isset($_GET['id'])) {
  $results->execute(array(
    ':id' => $_GET['id'],
  ));
}

...

':id' => isset($_POST['id']) ? $_POST['id'] : '',
':firstname' => isset($_POST['firstname']) ? $_POST['firstname'] : '',
// and so on
...
于 2012-10-06T18:15:26.843 回答
0

您的代码没有任何意义:$_POST['submit'] )设置时,您重定向到另一个页面,因此使用POST变量的代码将在$_POST['submit'] )未设置时运行。

这就是 php 的样子:

if (isset($_POST['submit'] ))
{
   header("Location: template/header.php");
}

$update = $dbh->prepare('UPDATE details SET firstname = :firstname, surname = :surname, houseno = :houseno, street = :street, town= :town,
       county = :county, postcode = :postcode,  mobile = :mobile, nickname = :nickname, website = :website,
       homephone = :homephone WHERE id = :id');
$update->execute(array(
    ':id' => $_POST['id'],
    ':firstname' => $_POST['firstname'],
    ':surname' => $_POST['surname'],
    ':houseno' => $_POST['houseno'],
    ':street' => $_POST['street'],
    ':town' => $_POST['town'],
    ':county' => $_POST['county'],
    ':postcode' => $_POST['postcode'],
    ':mobile' => $_POST['mobile'],
    ':nickname' => $_POST['nickname'],
    ':website' => $_POST['website'],
    ':homephone' => $_POST['homephone'],    
));

$row = $results->fetch();

究竟是什么header呼吁?

顺便说一句,你可能想要这样的东西:

require 'server.php';

if (isset($_POST['submit'] ))
{
  $update = $dbh->prepare('UPDATE details SET firstname = :firstname, surname = :surname, houseno = :houseno, street = :street, town= :town,
       county = :county, postcode = :postcode,  mobile = :mobile, nickname = :nickname, website = :website,
       homephone = :homephone WHERE id = :id');
  $update->execute(array(
    ':id' => $_POST['id'],
    ':firstname' => $_POST['firstname'],
    ':surname' => $_POST['surname'],
    ':houseno' => $_POST['houseno'],
    ':street' => $_POST['street'],
    ':town' => $_POST['town'],
    ':county' => $_POST['county'],
    ':postcode' => $_POST['postcode'],
    ':mobile' => $_POST['mobile'],
    ':nickname' => $_POST['nickname'],
    ':website' => $_POST['website'],
    ':homephone' => $_POST['homephone'],    
  ));

  // show message or something
}
else
{
  // normal get request, nothing posted
  $results = $dbh->prepare('SELECT * FROM details WHERE id = :id');
  $results->execute(array(':id' => $_GET['id'],
     ));
  $row = $results->fetch();

  // show rest of the page
}
于 2012-10-06T17:27:32.713 回答