我是PHP的初学者,并试图编写一个代码,它确实表单验证。 只是对它进行测试而已。我只写了一个测试年龄输入文本的函数,不管它是否为数字。如果不是,则会在数组中存储错误,然后在另一个页面中显示该错误。我在视频教程中看到了这种方法,但是我自己做不到。当我尝试调用的错误(在年龄不是数值),它总是显示我这个错误在我add.php页面:PHP变量范围问题
注意:未定义的变量:错误在/ home /拉斐尔/ WWW/RofaCorp /加/add.php on line 37
我该如何声明一个可以通过我的整个项目访问的变量?
这里是我的代码:
- form_validation.php
<?php function validate_number($number) { global $errors; if (is_numeric($number)) { return $number; }else { $errors[] = "Value must be number"; } if (!empty ($errors)) { header("Location: ../add.php"); exit; } } ?>
create_ind.php
<?php require_once '../../include/connection.php'; ?> <?php require_once '../../include/form_validation.php'; ?> <?php require_once '../../include/functions_database_infoget.php'; ?> <?php $family_id = get_family_info_fam("id"); $ind_name = mysql_real_escape_string($_POST["ind_name"]); $age = validate_number(mysql_real_escape_string($_POST["age"])); $gender = $_POST["gender"]; $notes = mysql_real_escape_string($_POST["notes"]); $add_query = "INSERT INTO individual (g_id , ind_name , age , gender , notes) Values ({$family_id} , '{$ind_name}' , {$age} , '{$gender}' , '{$notes}')"; if(mysql_query($add_query , $connection)){ header("Location: ../../main.php"); exit; } else {echo "ERROR " . mysql_error() ; } ?> <?php mysql_close($connection); ?>
add.php(我的代码的一部分)
<!--Main Content--> <section id="mainContent"> <header> <h3>Select where to add new Family/Individual.</h3> </header> <article> <?php if (count($errors) > 0) { for($i=0 ; $i < count($errors) ; $i++){ echo "{$errors[$i]}" . "<br/>"; } } ?> </article> </section>
谢谢了。 – rafael 2011-04-21 21:34:18