【问题标题】:Upload an image and newsTitle related to image PHP上传图片相关的图片和新闻标题 PHP
【发布时间】: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 '&lt;h2&gt;&lt;?php echo 'htmlspecialchars($_POST['newsImage']);''; echo'&lt;h2'.'&gt;'.htmlspecialchars($newsImage["newsImage"]).'&lt;/h2&gt;'; 单独应该给你一个解析错误php.net/manual/en/function.error-reporting.php 并且开头的 `echo'

    ` 不正确。

  • //$newsTitle = $_POST['newsTitle']; 那为什么被注释掉了,你想在哪里使用/回显$newsTitle

标签: php html post


【解决方案1】:

首先,如果您尝试使用文本制作每个新闻,请使用 $_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; ?> 

    ?>

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多