-1

我正在为 mysql 数据库上的注册表单和数据查询编写代码。报名表在这里:-

<form action="index.php" method="POST" enctype="multipart/form-data">

    First Name:<input type="text" name="name"><br>

    Last Name : <input type="text" name="lname"><br>

    Username : <input type="text" name="uname"><br>

    Password : <input type="text" name="password"><br>

    age : <input type="text" name="age"><br>

    Email : <input type="text" name="email"><br>

    Chose_Images : <input type="file" name="images"><br>

    <input type="submit" name="submit">

</form>

现在,index.php文件在这里:-

<?php

require'store.inc.php';

if (isset($_POST['submit'])) {
    # code...


$first_name = mysql_real_escape_string(htmlentities($_POST['name']));

$last_name = mysql_real_escape_string(htmlentities($_POST['lname']));

$username = mysql_real_escape_string(htmlentities($_POST['uname']));

$password = mysql_real_escape_string(htmlentities($_POST['password']));

$password_hash = md5($password);

$age = mysql_real_escape_string(htmlentities($_POST['age']));

$email = mysql_real_escape_string(htmlentities($_POST['email']));

    if (isset($first_name) && isset($last_name) &&isset($username) &&isset($password) &&isset($age) &&isset($email)) {
        # code...

            if (!empty($first_name) && !empty($last_name) &&!empty($username) &&!empty($password) &&!empty($age) &&!empty($email)) {


        $errors = array();



// cheking string limit............

        if (strlen($first_name) > 50) {
            # code...

            $errors[] = 'PLease dont cross the strign limit in first name colum'.'<br>';

        }elseif (strlen($last_name) > 50) {
            # code...

            $errors[] = 'PLease dont cross the strign limit in first name colum'.'<br>';

        }elseif (strlen($username) > 50) {
            # code...
            $errors[] = 'Your username is out of string limit'.'<br>';

        }elseif (strlen($password) > 40) {
            # code...

            $errors[] = 'Your password is too long';

        }elseif (strlen($age) > 50) {
            # code...
            $errors[] = 'you can not register into the site';


        }elseif (strlen($email) > 50) {
            # code...

            $errors[] = 'You are out of Email string limit';

        }

// coding of the first function start...
        function connect_database(){

            $server_connect = mysql_connect('localhost','root','');

            $server_database = mysql_select_db('reg_log',$server_connect);

                            if ($server_connect && $server_database) {
                                # code...
                                            return true;
                                } else {
                                        return false;
                                                }
                                            }

// coding of the first function END...........


// coding of the function check_data() start...
function check_data(){

    global $username;
        connect_database();

        $select = "SELECT `username` FROM `users` WHERE `username` = '$username'";

        $select_query = mysql_query($select);

        $num_rows = mysql_num_rows($select_query);

        if ($num_rows == 1) {
            # code...
            return false;
        } elseif ($num_rows == 0) {
            # code...
            return true;
        }

}

//coding of the function End..................

// *********Varibles about Images which will be Global varibles..........Using addslashes for security

$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));

$image_name = addslashes($_FILES['images']['name']);

$image_size = addslashes(getimagesize($_FILES['images']['tmp_name']));


//*******Varible Stored.....................................................

//Coding of Inserting Data in the database...By this function code will insert data in to database after all check...........

function insert_data(){

global $first_name,$last_name,$username,$password_hash,$age,$email,$images;
    connect_database();

    $insert = "INSERT INTO users VALUES('','$first_name','$last_name','$username','$password_hash','$age','$email','$images')";

    $insert_query = mysql_query($insert);

    if ($insert_query) {
        # code...
        return true;
    }
}


        }

        }

}
    if (empty($errors)) {
        # code...

        if (check_data()) {
            # code...
            insert_data();
        }

    }else{

        foreach ($errors as $error) {
            # code...
            echo $error.'<br>';
        }
            }





?>

两个文件是一样的。我的意思是,这两个代码都存储在同一个名为“index.php”的文件中。'store.inc.php' 仅包含:-

$server_connect = mysql_connect('localhost','root','');

$server_database = mysql_select_db('reg_log',$server_connect);

现在,当我在浏览器中通过 localhost 打开 index.php 时,它显示错误:-

Fatal error: Call to undefined function check_data() in C:\xampp\htdocs\oop\user_reg\index.php on line 145

但是,我已经有一个名为 check_data() 的函数,并且该函数本身运行良好。但是我的代码发生了一些不好的事情。我想修复它并且无法做到。我非常需要你们的帮助。谢谢你。

4

1 回答 1

1

check_data()在 if 下使用此函数,然后从 if 外部调用它。

尝试在这行之前移动这个函数

      if (empty($errors)) {
    # code...

    if (check_data()) { 
于 2013-06-23T18:10:34.563 回答