gpt4 book ai didi

php - 在数据库列中保存图像 url 路径

转载 作者:可可西里 更新时间:2023-10-31 23:37:26 25 4
gpt4 key购买 nike

<分区>

我正在通过 php 表单更新注册用户数据库中的姓名、电子邮件。它工作正常。

class.usr.php

public function update($uname,$email, $tax)
{
try {
$stmt = $this->conn->prepare('UPDATE tbl_users SET userName = ?, userEmail = ? , tax = ? WHERE userID = ? ');
$stmt->execute(array($uname,$email, $tax , $_SESSION['userSession']));
return $stmt->fetch();
} catch(PDOException $e) {
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
}

表单

<form action="profile.php" method="POST" enctype="multipart/form-data">

Name :
<input type="text" name="txtuname" value="<?php echo $row['userName'] ?>" /><br/>
Email :
<input type="text" name="txtemail" value="<?php echo $row['userEmail'] ?>" /><br>
Image
<input type="file" name="photo" id="fileSelect"><br>

<input type="submit" name="submit" value="Save" />

</form>

form相关代码存入db

<?php

$user_home = new USER();

if(!$user_home->is_logged_in())
{
header("Location: index.php");
die();
}

if (isset($_POST['submit'])) {
// new data
$uname = $_POST['txtuname'];
$email = $_POST['txtemail'];
$tax = trim($_POST['tax']); // image url path

$uid = (isset($_SESSION['userSession']) ? intval($_SESSION['userSession']) : 0);

if ($uid > 0 && $user_home->update($uname,$email, $tax, $uid))
{
header("Location: profile1.php");
die();
}
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

在此之后,现在我使用以下代码通过相同的 php 表单成功将图像上传到文件夹。

    <?php 
if(isset($_FILES["photo"]["error"])){
if($_FILES["photo"]["error"] > 0){
echo "Error: " . $_FILES["photo"]["error"] . "<br>";

} else{
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];

// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");

// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");

// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $_FILES["photo"]["name"])){
echo $_FILES["photo"]["name"] . " is already exists.";

} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);

echo "Your file was uploaded successfully.";
}
} else{

echo "Error: There was a problem uploading your file - please try again.";
}

}

} else{
echo "";
}

?>

现在图像只是保存在文件夹中,我需要的是我希望将该图像路径保存在数据库中并将该图像路径分配给数据库中的上传用户。这样一个注册用户就可以更新现有图片,但不再上传一张图片。

我试过下面的代码,但没有用:

<?php
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
$tax= $full_path;

if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $_FILES["photo"]["name"])){
echo $_FILES["photo"]["name"] . " is already exists.";

} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);

echo "Your file was uploaded successfully.";
}
} else{

echo "Error: There was a problem uploading your file - please try again.";
}

}

} else{
echo "";
}
?>

db columns : userName, userEmail, tax, photo

在 google 的帮助下我完成了以上所有工作,我是 php 新手,所以请帮助我。

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