gpt4 book ai didi

javascript - PHP - 在插入数据库之前显示图像

转载 作者:行者123 更新时间:2023-12-02 14:27:52 25 4
gpt4 key购买 nike

我想在从文件中选择图像后将其插入数据库之前显示图像,我希望图像显示在页面中。有人能帮我吗?我是 php 和 html 的新手,正在开始学习它。如果你知道会做什么,你可以向我解释一下吗?

这是我的 php 代码。

<?php
session_start();

if(isset($_SESSION['username'])){
include_once('connection.php');
$username = ucfirst($_SESSION['username']);
if(isset($_POST['submit'])){
$title = $_POST['title'];
$date = $_POST['date'];
$content = $_POST['content'];
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
$newsimage="img/" . $_FILES["image"]["name"];

if(empty($title)){
echo "Please enter a title";
}
else if(empty($date)){
echo "Please enter a date";
}
else if(empty($content)){
echo "Please enter content";
}
else{
$sql ="INSERT into news (news_title, news_date, news_content, news_image) VALUES ('$title', '$date', '$content', '$newsimage')";
mysqli_query($con, $sql);
echo "news entry posted";
}
}
}
else{
header('Location: login.php');
die();
}

if(isset($_POST['image'])){




}

?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<h1>Welcome, <?php echo $_SESSION['username']; ?>!</h1>

<form method="post" action ="admin.php" enctype="multipart/form-data">
Title<input type ="text" name ="title" /><br>
Date<input type ="text" name="date" /><br>
Content<textarea name="content"></textarea>
<input type="submit" name="submit" value="Post News Entry" />
<input class="form-control" id="image" name="image" type="file" onchange='AlertFilesize();'/>
</form>

</body>
</html>

最佳答案

尝试this ,在您的代码上实现如下所示:

   <form method="post" action ="admin.php" enctype="multipart/form-data">
Title<input type ="text" name ="title" /><br>
Date<input type ="text" name="date" /><br>
Content<textarea name="content"></textarea>

<input class="form-control" id="image" name="image" type="file" onchange='AlertFilesize();'/>
<img id="blah" src="#" alt="your image" />
<input type="submit" name="submit" value="Post News Entry" />
</form>

然后添加JS

function readURL(input) {

if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}

reader.readAsDataURL(input.files[0]);
}
}

$("#image").change(function(){
readURL(this);
});

关于javascript - PHP - 在插入数据库之前显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38066209/

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