我在用户表单中上传图像的代码有什么问题?

问题描述 投票:0回答:1

我正在开发一个 MySQL 和 PHP 项目,在该项目中我创建了一个表单来捕获用户详细信息,包括他们的护照。但是,当用户填写表单并单击“提交”时,页面会变为空白,并且数据不会插入到数据库中。奇怪的是,图像确实被移动到指定的目录。我需要帮助解决这个问题以确保一切顺利。以下是我的代码,主要问题是图片上传。

以下是相关的 PHP 和 HTML 代码:

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$image = $_FILES['image']['name'];
$regno=$_POST['regno']; 
$email=$_POST['email']; 
$password=$_POST['password'];
$status=1;
// image file directory
    $target = "images/".basename($image);

    move_uploaded_file($_FILES['image']['tmp_name'], $target);


$sql="INSERT INTO  tblstudents(name,image,regno,email,password,status) VALUES(:name,:image,:regno,:email,:password, :status)";

$query = $dbh->prepare($sql);
$query->bindParam(':name',$name,PDO::PARAM_STR);
$query->bindParam(':image',$_image,PDO::PARAM_STR);
$query->bindParam(':regno',$regno,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':password',$password,PDO::PARAM_STR);
$query->bindParam(':status',$status,PDO::PARAM_STR);

$query->execute();
$Id = $dbh->Id();
if($Id)
{
$msg="user added successfully";
}

elseif (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        $msg = "Image uploaded successfully";
}else 
{
$error="Something went wrong. Please try again";
}

}
?>

这是我的 html 表单

 <form class="form-horizontal" method="post" enctype="multipart/form-data">
                                                    <!-- image file  enctype="multipart/form-data" -->

<div class="form-group">
<label for="default" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="name" required="required" autocomplete="off">
</div>
</div>



<div class="form-group">
<label for="default" class="col-sm-2 control-label">Registration Number</label>
<div class="col-sm-10">
<input type="text" name="regno" class="form-control" id="regno" maxlength="50" required="required" autocomplete="off">
</div>
</div>

<div class="form-group">
<label for="default" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="email" required="required" autocomplete="off">
</div>
</div>


<div class="form-group">
<label for="default" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="password" required="required" autocomplete="off">
</div>
</div>



<!-- passport to upload -->

<div class="form-group">
<label for="default" class="col-sm-2 control-label">Upload Passport</label>
<div class="col-sm-10">
<input type="file" name="image" id="image">     
</div>      
</div>                               
                                                    <div class="form-group">
                                                        <div class="col-sm-offset-2 col-sm-10">
                                                            <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                                        </div>
                                                    </div>
                                                </form>
php pdo
1个回答
-1
投票

我已经解决了这个问题,现在运行良好。我使用数组来捕获所有变量。

© www.soinside.com 2019 - 2024. All rights reserved.