gpt4 book ai didi

php - 为注册用户简单上传数据库中的图像

转载 作者:可可西里 更新时间:2023-11-01 01:01:16 25 4
gpt4 key购买 nike

使用 php 和 pdo 我能够制作一个注册页面但没有保存图像

$firstname = trim($_POST['fn']); //at a minimus clear whitespace.
$lastname = trim($_POST['ln']);
$username = trim($_POST['un']);
$password = trim($_POST['pw']);
$confirmpassword= trim($_POST['cp']);
$stmt = $dbh->prepare("INSERT INTO registration (fname,lname,username,password) VALUES (?,?,?,?)");
$stmt->bindValue(1,$firstname,PDO::PARAM_STR);
$stmt->bindValue(2,$lastname,PDO::PARAM_STR);
$stmt->bindValue(3,$username,PDO::PARAM_STR);
$stmt->bindValue(4,$password,PDO::PARAM_STR);
if($stmt->execute()){
echo "YOUR REGISTRATION IS COMPLETED...";
}

我找到了这个tutorial但它对我来说太复杂了,而且没有解释清楚

表单

<form method="POST" action="crud.php">
<tr>
<td>

</td>
<td>
<input type="file" name="image" />
</td>
</tr>
<tr>
<td>First Name</td>
<td>
<input type="text" name="fn">
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type="text" name="ln">
</td>
</tr>
<tr>
<td>User Name</td>
<td>
<input type="text" name="un">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="pw">
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td>
<input type="password" name="cp">
</td>
</tr>
<tr>
<td>
<input id="button" type="submit" value="Back" name="back"/>
</td>
<td>
<input id="button" type="submit" value="SignUp" name="signup"/>
</td>
</tr>
<tr><td><div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div></td></tr>
</form>

最佳答案

这是一个如何实现这一点的简单示例:

if(is_uploaded_file($_FILES['image']['tmp_name'])){ 
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo "succesful upload, we have an image!";

$firstname = trim($_POST['fn']);
$lastname = trim($_POST['ln']);
$username = trim($_POST['un']);
$password = trim($_POST['pw']);
$confirmpassword= trim($_POST['cp']);

$stmt = $dbh->prepare("INSERT INTO registration (fname,lname,username,password, img_url) VALUES (?,?,?,?,?)");
$stmt->bindValue(1,$firstname,PDO::PARAM_STR);
$stmt->bindValue(2,$lastname,PDO::PARAM_STR);
$stmt->bindValue(3,$username,PDO::PARAM_STR);
$stmt->bindValue(4,$password,PDO::PARAM_STR);
$stmt->bindValue(5,$full_path,PDO::PARAM_STR);

if($stmt->execute()){
echo "YOUR REGISTRATION IS COMPLETED...";
}else{
echo 'YOUR REGISTRATION COULD NOT BE COMPLETED...';
}

} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}

在查询中,我包含了一个名为 img_url 的字段。

图片上传成功后执行PDO插入查询。

关于php - 为注册用户简单上传数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com