【发布时间】:2021-05-27 06:45:49
【问题描述】:
我需要上传一张图片并将其添加到我的幻灯片中,并在我上传的图片前为其提供相关的新闻标题。我是 PHP 新手,正在尝试学习如何将数据从我的 admin.php 文件发送到我的 index.php 文件,并在 html 中添加更多带有 <form> 的图像。
我的问题是我可以上传图片,但无法将我的 newsTitle 打印到我的主页 index.php。
这是我在 index.php 中的 PHP 代码:
<?php
if (isset($_POST['send_object'])) {
$file_name = $_FILES['image']['name'];
$file_type = $_FILES['image']['type'];
$file_tmp_name = $_FILES['image']['tmp_name'];
//$newsTitle = $_POST['newsTitle'];
$newsImage = $_POST['newsImage'];
echo '<h2><?php echo 'htmlspecialchars($_POST['newsImage']);'';
echo'<h2'.'>'.htmlspecialchars($newsImage["newsImage"]).'</h2>';
if (move_uploaded_file($file_tmp_name,"uploader/$file_name")) {
}
}
$folder = "uploader/";
if (is_dir($folder)) {
if($handle = opendir($folder)) {
while (($file = readdir($handle)) != false) {
if ($file ==='.' || $file=== '..') continue;
echo '<img class="slider mySlides" width="100" src="uploader/'.$file.'" alt="">';
}
closedir($handle);
}
}
?>
这是我在 admin.php 中的 html 代码:
<form action="index.php" method="post" enctype="multipart/form-data">
<br><br>
<tr>
<td> NewsTitle: </td>
<td> <input type="text" name="newsTitle" placeholder="newsTitle"> </td>
</tr>
<br><br>
Select image to upload:
<input type="file" name="image">
<br><br>
<br><br>
NewsText: <textarea name="newsImage" placeholder="newsImage" rows="5" cols="40"></textarea>
<br><br>
<input type="submit" value="Send" name="send_object">
</form>
我正在尝试在不连接数据库的情况下执行此操作,只是连接到我的 apache 服务器。我尝试过使用另一个全局变量$_REQUEST,但它没有用。我知道它可以用于$_POST、$_GET 和$_COOKIES
【问题讨论】:
-
我看不到你在 index.php 中回显 newsTitle。
-
“我尝试在不连接数据库的情况下执行此操作” - 这与上传文件有什么关系?
-
echo '<h2><?php echo 'htmlspecialchars($_POST['newsImage']);''; echo'<h2'.'>'.htmlspecialchars($newsImage["newsImage"]).'</h2>';单独应该给你一个解析错误php.net/manual/en/function.error-reporting.php 并且开头的 `echo'` 不正确。
-
//$newsTitle = $_POST['newsTitle'];那为什么被注释掉了,你想在哪里使用/回显$newsTitle?