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

时间:2016-06-24 05:24:10

标签: php

我没有看到这个程序有任何错误,任何人都可以解释这个!

<?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].'>';
                    }

        ?>

我收到致命错误: -

  

未定义的函数uploadimage()

任何人都可以解释一下吗???

这是我在数据库中创建的表的布局

图像 ID 名称

3 个答案:

答案 0 :(得分:0)

将函数移出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";


    }


?>

答案 1 :(得分:0)

您需要在任何if else条件之外的页面底部定义所有函数。这是修改后的代码:

<?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] . '>';
}
?>

答案 2 :(得分:0)

你想要在声明之前调用uploadimage函数

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

在任何php逻辑代码之前移动函数声明