简体   繁体   English

致命错误:调用未定义的函数

[英]Fatal error: Call to undefined function

I don't see any error in this program can anyone explain this! 我看不到此程序中的任何错误,任何人都可以解释!

<?php

           if(isset($_POST['submit'])){
               if(isset($_FILES['image_up']['tmp_name']))$image = $_FILES['image_up']['tmp_name'];
               if(isset($_FILES['image_up']['name']))$imageName = $_FILES['image_up']['name'];

               $image = addslashes($image);
               $imageName = addslashes($imageName);
               $image = file_get_contents($image);
               $image = base64_encode($image);
               $upload=uploadimage($image, $imageName);



                    function uploadimage($image,$imageName){
                        //Connect to DB
                        $link = mysqli_connect('localhost','root','');
                        $selectdb = mysqli_select_db($connect2db, 'test_one');
                        if(!$selectdb)echo "Something Went wrong_DB try again later";

                        $query = "insert into upload_image(name,Image)values('$imageName','$image')";
                        $result = mysqli_query($link, $query);

                        if(!$result)    echo"Error Uploading";
                        else echo"Uploaded Successfully";


                    }
                    displayImage();

           }
           else echo"Select a File to Upload";

                    function displayImage(){
                        $link = mysqli_connect('localhost','root','');
                        $selectdb = mysqli_select_db($connect2db, 'test_one');
                        if(!$selectdb)echo "Something Went wrong_DB try again later";

                        $query = "selct * from upload_image";
                        $result = mysqli_query($link, $query);
                        $row = mysqli_fetch_array($result);

                        echo '<img width="300" height="300" src=data:Image;base64,'.$row[3].'>';
                    }

        ?>

I'm getting a fatal error as:- 我收到一个致命错误:

Undefined function uploadimage() 未定义的函数uploadimage()

can anyone explain it??? 有人可以解释吗???

And This is the layout of the table which i have created in my Database 这是我在数据库中创建的表的布局

Image Id Name 图片编号名称

Move the function outside from if(isset($_POST['submit'])){ statement. 将函数从if(isset($_POST['submit'])){语句if(isset($_POST['submit'])){外部。

 <?php

   if(isset($_POST['submit'])){
       if(isset($_FILES['image_up']['tmp_name']))$image = $_FILES['image_up']['tmp_name'];
       if(isset($_FILES['image_up']['name']))$imageName = $_FILES['image_up']['name'];

       $image = addslashes($image);
       $imageName = addslashes($imageName);
       $image = file_get_contents($image);
       $image = base64_encode($image);
       $upload=uploadimage($image, $imageName);




            displayImage();

   }
    echo "Select a File to Upload";

    function displayImage(){
        $link = mysqli_connect('localhost','root','');
        $selectdb = mysqli_select_db($connect2db, 'test_one');
        if(!$selectdb)echo "Something Went wrong_DB try again later";

        $query = "selct * from upload_image";
        $result = mysqli_query($link, $query);
        $row = mysqli_fetch_array($result);

        echo '<img width="300" height="300" src=data:Image;base64,'.$row[3].'>';
    }


    function uploadimage($image,$imageName){
        //Connect to DB
        $link = mysqli_connect('localhost','root','');
        $selectdb = mysqli_select_db($connect2db, 'test_one');
        if(!$selectdb)echo "Something Went wrong_DB try again later";

        $query = "insert into upload_image(name,Image)values('$imageName','$image')";
        $result = mysqli_query($link, $query);

        if(!$result)    
              echo"Error Uploading";
        else 
              echo "Uploaded Successfully";


    }


?>

You need to define all your functions at bottom of page outside any if else conditions. 您需要在任何其他条件之外的页面底部定义所有功能。 Here is the modified code : 这是修改后的代码:

<?php   

if (isset($_POST['submit'])) {
    if (isset($_FILES['image_up']['tmp_name']))
        $image = $_FILES['image_up']['tmp_name'];
    if (isset($_FILES['image_up']['name']))
        $imageName = $_FILES['image_up']['name'];

    $image = addslashes($image);
    $imageName = addslashes($imageName);
    $image = file_get_contents($image);
    $image = base64_encode($image);
    $upload = uploadimage($image, $imageName);    

    displayImage();
}
else
{
    echo "Select a File to Upload";
}

function uploadimage($image, $imageName) {
    //Connect to DB
    $link = mysqli_connect('localhost', 'root', '');
    $selectdb = mysqli_select_db($connect2db, 'test_one');
    if (!$selectdb)
        echo "Something Went wrong_DB try again later";

    $query = "insert into upload_image(name,Image)values('$imageName','$image')";
    $result = mysqli_query($link, $query);

    if (!$result)
        echo "Error Uploading";
    else
        echo "Uploaded Successfully";
}

function displayImage() {
    $link = mysqli_connect('localhost', 'root', '');
    $selectdb = mysqli_select_db($connect2db, 'test_one');
    if (!$selectdb)
        echo "Something Went wrong_DB try again later";

    $query = "selct * from upload_image";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_array($result);

    echo '<img width="300" height="300" src=data:Image;base64,' . $row[3] . '>';
}
?>

you are tying to call uploadimage function before its declaration 您想在声明之前调用uploadimage函数

   $upload=uploadimage($image, $imageName);
        function uploadimage($image,$imageName){

Move function declaration before any php logic code 将函数声明移到任何php逻辑代码之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM