-3

当我在没有进入设置的情况下访问 mainpage.php 时,我的页面给了我所有变量的未定义变量错误,但是当我在设置中输入内容并提交时,它不再显示。无论如何,这是到目前为止的代码。

主页.php

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="settings.php">Settings</a>

</form>
</body>
</html>

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$bio = $_POST['bio'];
$hobbies = $_POST['hobbies'];
$past = $_POST['past'];
$work = $_POST['work'];
echo $bio;
echo $email;
echo $name;
echo $hobbies;
echo $past;
echo $work;
?>

设置.php

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="mainpage.php">Main Page</a>

<form action="mainpage.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Bio: <input type="text" name="bio"><br>
Hobbies: <input type="text" name="hobbies"><br>
Past School: <input type="text" name="past"><br>
Work History: <input type="text" name="work"><br>
<input type="submit">
</form>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Choose Profile Picture:</label>
<input type="file" name="file" id="file"> 
<input type="submit" name="pic" value="Submit">
</form>

</body>
</html>

我似乎无法找出如何摆脱未定义的错误。有人建议我相信 isset。

4

2 回答 2

1

$_POST是空的

mainpage.php - 文件上传和输入字段属于两个不同的表单,并且有两个提交按钮。信息永远不会同时发送。我用一个提交按钮将这些表单组合成一个表单。

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="mainpage.php">Main Page</a>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
   Name: <input type="text" name="name"><br>
   E-mail: <input type="text" name="email"><br>
   Bio: <input type="text" name="bio"><br>
   Hobbies: <input type="text" name="hobbies"><br>
   Past School: <input type="text" name="past"><br>
   Work History: <input type="text" name="work"><br>
   <!--input type="submit"-->
<!--/form-->

<!--form action="upload_file.php" method="post" enctype="multipart/form-data"-->
   <label for="file">Choose Profile Picture:</label>
   <input type="file" name="file" id="file">   
   <br/>
   <input type="submit" name="pic" value="Submit"> 
</form>

</body>
</html> 

upload_file.php - 文件信息和输入信息属于两个不同的数组。

<?php  

   // the reason we should check for each $_POST variable and not just
   // if (!empty($_POST)) { ... }
   // is because you will still get errors if one of those keys do not
   // exist inside of $_POST... if you do $file = $_FILE['file'] there
   // will be an error.  however, $_POST is always going to exist, so 
   // we don't need to check it.  the keys may or may not exist, though

   $name    = isset($_POST['name'])    ? $_POST['name']  : '';
   $email   = isset($_POST['email'])   ? $_POST['email'] : '';
   $bio     = isset($_POST['bio'])     ? $_POST['bio']   : '';
   $hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : '';
   $past    = isset($_POST['past'])    ? $_POST['past']  : '';
   $work    = isset($_POST['work'])    ? $_POST['work']  : ''; 

   echo "bio:  $bio<br/>";
   echo "email:  $email<br/>";
   echo "name:  $name<br/>";
   echo "hobbies:  $hobbies<br/>";
   echo "past:  $bio<br/>";
   echo "work:  $work<br/>";  

   print "<pre>";

   if (isset($_POST))
   {
      print "this is the \$_POST information:\n"; 
      print_r($_POST); 
   }
   else { print "no post information sent."; }

   print "\n\n";

   if (isset($_FILES))
   {
      print "this is the \$_FILE information:\n"; 
      print_r($_FILES);
   }
   else { print "no file information sent."; }

   print "</pre>"; 
?> 

所以......只是一个免责声明,在处理$_POST值时要小心 SQL 注入。

输出看起来像这样......(所以你可以看到的结构$_POST $_FILE

bio: three
email: two
name: one
hobbies: four
past: three
work: 
this is the $_POST information:
Array
(
    [name] => one
    [email] => two
    [bio] => three
    [hobbies] => four
    [past] => 
    [work] => 
    [pic] => Submit
)


this is the $_FILE information:
Array
(
    [file] => Array
        (
            [name] => Capture.PNG
            [type] => image/png
            [tmp_name] => C:\wamp\tmp\php1D7E.tmp
            [error] => 0
            [size] => 12968
        )

)

(另外,请注意,我会为遇到此问题的未来用户不断更新)

于 2013-11-07T03:23:00.667 回答
0

使用检查空 $_POST

if (!empty($_POST)){
    $name = $_POST['name'];
    $email = $_POST['email'];
    /* ... */
}
于 2013-11-07T03:24:10.327 回答