无法使程序在PHP中识别非空/空变量

时间:2016-05-23 09:19:32

标签: php html

在以下PHP代码中,如果变量$emptyfields为空,我试图使true成为$name

<? php
/* Gathering Data*/
$namme = $_POST['objectname'];
$species = $_POST['species'];
$power = $_POST['power'];
$properties = $_POST['properties'];

/*Writing Data to File
$filename = $DOCUMENT_ROOT.'Galaxy/data/'.'characters.txt';
$fp = fopen($filename, 'a');
$looper =1; */
$emptyfields = false;
if (empty($name)) {
  print '$name';
  $emptyfields= true;

}


if (!$emptyfields) {
  print '<p> Character Added! </p>
Your characer with the following details has been successfully added.  <br> <br>

Details: <br>';
  print '<table> <tr> <td> <b> Name: </b> </td> <td> '.$namme.
  '</td> </tr>';
  print '<tr> <td><b> Species: </b></td> <td>'.$species.
  '</td></tr>';
  print '<tr> <td> <b> Power: </b></td><td> '.$power.
  '</td> </tr>';
  print '<tr> <td> <b> Properties: </b></td> <td> <textarea disabled="true">'.$properties.
  '</textarea></td> </tr></table>';
} else {
  print '<p> Character Not Added! </p> 
Your characer with the following details could not be added due to empty fields.  <br> <br>';
}

?>

上面的代码已经习惯了。但不知何故,即使变量$name不为空,变量$emptyfields也会变为true。如何防止?

2 个答案:

答案 0 :(得分:2)

$namme!= $name

您使用了

$namme = $_POST['objectname'];

将其更改为

$name = $_POST['objectname'];
   ^

答案 1 :(得分:0)

您的情况很好,您只是输入错字错误,只需更改此内容:

<强> PHP

$namme = $_POST['objectname'];

用这个

$name = $_POST['objectname'];