gpt4 book ai didi

php - 从MySQL数据库中选择帖子和图片

转载 作者:行者123 更新时间:2023-11-29 15:34:53 26 4
gpt4 key购买 nike

我正在创建一个社交网络,新闻提要类似于Twitter。用户可以上传图片和帖子,但不能同时上传。在新闻提要中,我试图显示数据库中的所有帖子和图片。但是我唯一能显示的是帖子,而不是图片。我尝试将其合并为一个,但是上传图片时无法保存。我整天都在尝试其他问题和视频,这是我的最后选择。

那么,如何按时间戳顺序从两个表中获取所有内容呢?

newsfeed.php(将图像上传到数据库):

<form id="form2" method="post" enctype="multipart/form-data">

<input type="file" name="fileToUpload" id="fileToUpload">
<br><input id="sub1" type="submit" value="Upload Pic" name="uploadPic"><br
/>
</form>

<?php

$username = trim( isset($_SESSION['username']) ? $_SESSION['username'] : "" );
$pic_name = isset($_SESSION['pic_name']) ? $_SESSION['pic_name'] : "";

if(!empty($username))
{
if (isset($_FILES['fileToUpload'])) {
$errors= array();
$file_name = $_FILES['fileToUpload']['name'];
$file_size = $_FILES['fileToUpload']['size'];
$width = 200;
$height = 200;
$file_tmp = $_FILES['fileToUpload']['tmp_name'];
$file_type = $_FILES['fileToUpload']['type'];
$tmp = explode('.',$_FILES['fileToUpload']['name']);
$file_ext=strtolower (end ($tmp));

$extensions= array("jpeg","jpg","png");

if(in_array($file_ext,$extensions)=== false){
$errors[]="Please choose a JPEG or PNG file.";
}

if ($file_size > 8097152) {
$errors[] = 'File size must be 2 MB';
}

if ($width > 200 || $height > 200) {
echo"File is to large";
}

if(empty($errors)==true)
{
$pic_name = $file_name;
move_uploaded_file($file_tmp,"uploads/".$pic_name);

$stmt = $conn->prepare("INSERT INTO pics (username, pic_name) VALUES(?, ?)");
$stmt->bind_param('ss', $username, $pic_name);

/* execute prepared statement */
$stmt->execute();

printf("", $conn->affected_rows);

/* close statement and connection */

}else{
print_r($errors);
echo"Couldn't upload picture";
}

}
}
else
{
echo "Invalid Username";
}

?>


newsfeed.php(在新闻提要中显示帖子和图像):

<?php

error_reporting(0);

include("connect.php");
include("auth_login.php");

$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$username = trim(isset($_GET['username']) ? $_GET['username'] : $username);

//Write the query
$sql = "SELECT * FROM posts, pics ORDER BY post_id DESC";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {

echo '<div id="rcorners2">';

echo '<p id="p2">' .$row['username']. '</p>';

echo '<p id="p4">' .$row['date_time']. '</p>';

echo '<hr id="hr2">';

echo '<p id="p3">' .$row['body']. '</p>';

echo '<div class="test"> <a href="#" class="fill-div"></a></div>';

echo'</div>';
echo'</br>';

}
}
else {

}
?>

最佳答案

您可以在一张表格中使用包含图片和帖子的单一表格来使用它。此处是代码,请根据您的要求进行自定义

<form id="form2" method="post" enctype="multipart/form-data">
<textarea name="post_content" placeholder="Content"></textarea>
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<input id="sub1" type="submit" value="Upload Pic" name="uploadPic">
<br
/>
</form>

<?php

$username = trim(isset($_SESSION['username']) ? $_SESSION['username'] : "");
$pic_name = isset($_SESSION['pic_name']) ? $_SESSION['pic_name'] : "";

if (!empty($username)) {

$pic_name="";
if (isset($_FILES['fileToUpload'])) {
$errors = array();
$file_name = $_FILES['fileToUpload']['name'];
$file_size = $_FILES['fileToUpload']['size'];
$width = 200;
$height = 200;
$file_tmp = $_FILES['fileToUpload']['tmp_name'];
$file_type = $_FILES['fileToUpload']['type'];
$tmp = explode('.', $_FILES['fileToUpload']['name']);
$file_ext = strtolower(end($tmp));

$extensions = array("jpeg", "jpg", "png");

if (in_array($file_ext, $extensions) === false) {
$errors[] = "Please choose a JPEG or PNG file.";
}

if ($file_size > 8097152) {
$errors[] = 'File size must be 2 MB';
}

if ($width > 200 || $height > 200) {
echo "File is to large";
}

if (empty($errors) == true) {
$pic_name = $file_name;
move_uploaded_file($file_tmp, "uploads/" . $pic_name);

}
} else {
print_r($errors);
echo "Couldn't upload picture";
}
$post_content=$_POST['post_content'];

$stmt = $conn -> prepare("INSERT INTO pics (username, pic_name,post_content) VALUES(?, ?,?)");
$stmt -> bind_param('sss', $username, $pic_name,$post_content);

/* execute prepared statement */
$stmt -> execute();

printf("", $conn -> affected_rows);

/* close statement and connection */

} else {
echo "Invalid Username";
}
?>

关于php - 从MySQL数据库中选择帖子和图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58348605/

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