0

在屏幕上上传任何图像之前在屏幕上显示无效的错误消息 index.php

    <html><body>    
    <form action="index.php" method="POST" enctype="multipart/form-data">
    <table  align="center" cellspacing="0" cellpadding="6"  bgcolor="#CCCCCC" border="1" bordercolor="#000000">
    <tr>
    <td >Firstname:</td><td><input type="text"  name="fname" /></td></tr><br/><br/><br/>
    <tr>
    <td >Regsternumber:</td><td><input type="text" name="regno"/></td></tr><br/>
    <tr>
    <td>Uploadphoto:</td><td><input type="file" name="image" /></td></tr>
    </table>

    <br><br><br>
    <center>
          <input  type="submit" /></center>
    </form>
    </form>
    <form action ="table.php" method="post">
    search :<select   name="search">
    <option>Please select
    </option>
    <?php
    mysql_connect("localhost","root","")or die(mysql_error());
    mysql_select_db("databaseimage") or die(mysql_error());
    $query ="select reg FROM store";
    $result=mysql_query($query);
    while($nt=mysql_fetch_array($result)){
    echo "<option value=".$nt['reg'].">".$nt['reg']."</option>";
    }
     ?>
     </select>
    <input type="submit" name="submit">
    </form>
    <?php 
    //connect to database
    mysql_connect("localhost","root","")or die(mysql_error());
    mysql_select_db("databaseimage") or die(mysql_error());

     $name=$_POST['fname'];
     $regno=$_POST['regno'];



    //file properties

        $file   =$_FILES['image']['tmp_name'];
        if(!isset($file))
            {

                echo "Please select an Image";

            }
        else
            {
                 $image              =addslashes(file_get_contents ($_FILES['image']['tmp_name']   ) );
                 $image_name    =addslashes($_FILES['image']['name']);
                $image_size         =getimagesize($_FILES['image']['tmp_name']);
            }

        if($image_size==FALSE)  
         {
                    echo "Not a Valid Image";
                    }

                    else
                    {
                    $order = "INSERT INTO store(name,image,fname,reg)VALUES('$image_name','$image','$name','$regno')";

    if (!mysql_query($order)) { // Error handling

        echo "<script>alert('Duplicate data entered');</script>";

    }
                    }
    ?>
       </body>
    </html>

在上传任何图像“不是有效图像”之前在屏幕上显示无效错误消息请帮助我如何纠正此错误消息。

4

1 回答 1

0

您只需要首先通过验证 $_POST 是否存在来检查表单是否已提交。

在这里验证isset()和/或is_array()类似,并且仅在表单已提交时才尝试处理上传(因此 $_POST 变量填充了 POST 数据)。

[...]
<?php 

if ( isset( $_POST ) && is_array( $_POST ) ) {

//connect to database
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());

 $name=$_POST['fname'];
 $regno=$_POST['regno'];



//file properties

    $file   =$_FILES['image']['tmp_name'];
    if(!isset($file))
        {

            echo "Please select an Image";

        }
    else
        {
             $image              =addslashes(file_get_contents ($_FILES['image']['tmp_name']   ) );
             $image_name    =addslashes($_FILES['image']['name']);
            $image_size         =getimagesize($_FILES['image']['tmp_name']);
        }

    if($image_size==FALSE)  
     {
                echo "Not a Valid Image";
                }

                else
                {
                $order = "INSERT INTO store(name,image,fname,reg)VALUES('$image_name','$image','$name','$regno')";

if (!mysql_query($order)) { // Error handling

    echo "<script>alert('Duplicate data entered');</script>";

}
                }
} // Closing from if(..)
?>
[...]
于 2013-08-24T08:03:30.997 回答