上传一个图片和与图片PHP相关的新闻标题


Upload an image and newsTitle related to image PHP

我需要上传一个图像并将其添加到我的幻灯片中,并在我上传的图片前面给出相关的新闻标题。我是一个新的PHP,并试图学习如何从我的admin.php文件发送数据到我的index.php文件,并在html中添加更多的图像与<form>

我的问题是,我可以上传图像,但不能得到我的新闻标题打印到我的主页是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

首先,如果你试图让每个新闻与文本你收集它单独与$_POST,但请注意,一旦你刷新页面的参数消失了,因为表单处理的一切,所以没有空间输出文本,但如果你使用get参数仍然存在,因为你没有存储post方法和get方法在数据库中。试试这个

 <?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'];
     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);
     }
    }
    ?>
<?php
          $newsImage = $_POST['newsImage'];
    //this would give a parse error    echo '<h2><?php echo 'htmlspecialchars($_POST['newsImage']);'';
try
          echo <?php echo $newsimage; ?> 
    ?>