我的函数没有检查$ _POST值

时间:2011-10-03 21:36:49

标签: php phpmyadmin

这是我的第一个PHP项目,因此请指导如何有效地进行调试:

我创建了这个表单:

<form action="<?php $self ?>" method="post">
<div class="fname">
  <label for="name"><span> Name: </span>
  <input name="name" value= "<?php 
  if($error_count != 0) {
  echo $name;
  }// To avoid filling name again in case of error?>"
  type="text" cols="20" />
  </label>
</div>
<div class="femail">
  <label for="email"><span> Email: </span>
  <input name="email" value= "<?php 
  if($error_count != 0) {
  echo $email;
  }// To avoid filling email again in case of error?>" 
  type="text" cols="20" />
  </label>
</div>
<br/>
<textarea name="post" rows="5" cols="40"><?php 
  if($error_count != 0) {
  echo $post;
  }// To avoid filling textarea again in case of error?>
  </textarea>
<input name="send" type="hidden" />
<p>
  <input type="submit" value="shout" />
</p>

并执行以下函数验证表单(在单独的文件form_validation.php中):

    <?php
function validate_shout($vmail,$vname,$vpost)
{

$error_count = 0; 

// To check email.
if(!preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/',$vmail)) {
echo "<p class =\"error\"> Please enter valid email address </p><br/>";
$error_count++;
}

// To check required fields
if($vname == NULL) {
echo "<p class =\"error\"> Oops!! You forgot to enter your name </p><br/>";
$error_count++;
}

if($vpost == NULL) {
echo "<p class =\"error\"> I guess your shout was blank </p><br/>";
$error_count++;
}

return $error_count;
}

?>

以这种方式使用

if(isset($_POST['send'])) {

if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['post'])) {
echo "<p class=\"error\">Unable to connect to the database server at this time.</p>";
}
else {
 $name = htmlspecialchars(mysql_real_escape_string($_POST['name'])); 
 $email = htmlspecialchars(mysql_real_escape_string($_POST['email'])); 
 $post = htmlspecialchars(mysql_real_escape_string($_POST['post']));


$error_count = validate_shout($email,$name,$post);
//PHP code to add shout to database
if ($error_count == 0) 
{
$query = "INSERT INTO shouts SET name='$name', email='$email', post='$post';";
  • 现在的问题是没有验证textarea 。其他 两个工作正常。代码几天前工作正常。但今天 当我打开它时,我发现了这个问题。

我注意到的另一件事是phpMyadmin,如下所示

已停用使用链接表的其他功能。要了解原因请点击此处。

点击它显示:

$cfg['Servers'][$i]['pmadb'] ...    not OK [ Documentation ]
$cfg['Servers'][$i]['relation'] ...     not OK [ Documentation ]
General relation features: Disabled

$cfg['Servers'][$i]['table_info'] ...   not OK [ Documentation ]
Display Features: Disabled

$cfg['Servers'][$i]['table_coords'] ...     not OK [ Documentation ]
$cfg['Servers'][$i]['pdf_pages'] ...    not OK [ Documentation ]
Creation of PDFs: Disabled

$cfg['Servers'][$i]['column_info'] ...  not OK [ Documentation ]
Displaying Column Comments: Disabled
Browser transformation: Disabled

$cfg['Servers'][$i]['bookmarktable'] ...    not OK [ Documentation ]
Bookmarked SQL query: Disabled

$cfg['Servers'][$i]['history'] ...  not OK [ Documentation ]
SQL history: Disabled

$cfg['Servers'][$i]['designer_coords'] ...  not OK [ Documentation ]
Designer: Disabled

$cfg['Servers'][$i]['tracking'] ...     not OK [ Documentation ]
Tracking: Disabled 

我猜两个问题一起出现,而我的任何设置或代码都没有任何变化。虽然它们看起来彼此分开。

请帮助..

主要问题是为什么$ post没有得到验证以及为什么phpMyadmin突然显示上述消息

2 个答案:

答案 0 :(得分:0)

mysql_real_escape_string()将永远不会返回null值,即使您传入null作为参数。它至少会返回一个空字符串。您的验证函数正在检查空值,但由于您通过M_R_E_S()传递这些值,因此它们永远不会为空,因此验证函数是导致问题的原因。

答案 1 :(得分:0)

== NULL比较将失败。通常,空字符串也可以“等于”NULL。 (无论如何,你最好写== ""。但是你的textarea不太可能包含一个非常空的字符串。从模板中我可以假设它至少包含换行符,或者甚至包含更多空格。

在这种情况下,您不希望将其置于空字符串上,而是探测它包含除空格之外的任何内容。为此:

if (strlen(trim($vpost))) {

无论如何,要探测字符串是否包含任何内容,请更喜欢strlen()。这里的trim()用于在检查之前过滤掉空格。

关于您的代码的其他一些注意事项:

  • htmlspecialchars(mysql_real_escape_string(订单错误。转义函数用于数据库。在将其连接到SQL之前,必须立即应用 。之后应用另一个编码(html)可能会撤消SQL转义。
  • 如果没有<form action="<?php $self ?>"
  • echo将无效
  • 电子邮件正则表达式只是被绞坏了。使用filter_var和内置FILTER_VALIDATE_EMAIL正则表达式