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

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

我目前正在做一个涉及mysql和php的项目。我创建了一个表单来捕获包括护照在内的用户详细信息,但是每当用户填写表单并单击提交时,它就会变成空白,而不会将数据插入数据库,但图像将被移动到指定的目录。我该如何解决这个问题,让一切顺利进行?以下是我的代码,我遇到的主要问题是图片上传。

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个回答
0
投票

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

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