我正在失去一个变量......在哪里?

时间:2014-10-20 11:33:42

标签: php

我有这段代码:

<?php
$id_sent = $_POST['id'];
echo  $id_sent;
include ($_SERVER['DOCUMENT_ROOT']."/upload/upload_class.php");

$max_size                      = 1024*250*500;
$my_upload                     = new file_upload;
$my_upload->upload_dir         = $_SERVER['DOCUMENT_ROOT']."/uploads/";
$my_upload->extensions         = array(".pdf");
$my_upload->max_length_filename= 50;
$my_upload->rename_file        = true;
$my_upload->id_search          = $id_sent;

if(isset($_POST['Submit'])) {
    $my_upload->the_temp_file  = $_FILES['upload']['tmp_name'];
    $my_upload->the_file       = $_FILES['upload']['name'];
    $my_upload->http_error     = $_FILES['upload']['error'];

    if ($my_upload->upload()) {
        mysql_query(sprintf("UPDATE psi_avize SET pdf = 'T' WHERE id = '%s'", $my_upload->id_search));?>
        <table width="800" border="0">
            <tr>
              <th width="167" rowspan="2" scope="col"><img src="images/figure_check_mark_celebrate_anim_md_wm.png" width="129" height="142"></th>
              <th width="471" height="29" scope="col"><div align="left">Succes!</div></th>
              <th width="148" scope="col">&nbsp;</th>
            </tr>
            <tr>
              <td height="104">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
        </table><?php
        echo $my_upload->show_error_string();
    }
}
else {?> <strong>Insert file!</span></p>
  </strong>
  <table width="800" border="0">
    <tr>
      <th width="167" rowspan="2" scope="col"><img src="images/document.png" width="150" height="152"></th>
      <th width="471" height="29" scope="col"><div align="left"></div></th>
      <th width="148" scope="col">&nbsp;</th>
    </tr>
    <tr>
      <td height="104">Max =  5 MB.</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <p>&nbsp;</p>
  Load file <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>"
      enctype="multipart/form-data" method="post">
     <div align="center">
         <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <th width="11%" scope="col">&nbsp;</th>
                <th width="26%" scope="col">&nbsp;</th>
                <th width="51%" scope="col"><div align="left">
                  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>" />
                  <?php
                    echo $my_upload->create_file_field("upload", "Select file...", 25, false); ?></div></th>
                <th width="12%" scope="col">&nbsp;</th>
            </tr>    
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><div align="left">
                  <input name="Submit" type="submit" id="Submit" value="Upload" />
                </div></td>
                <td>&nbsp;</td>
            </tr>
          </table>
      </div>
  </form>   <?php  }
  ?>

我的问题是,我以某种方式松开变量$ id_sent(它是从其他页面使用POST发送的),我无法使用id正确查询。 当我回显第3行中的变量正在工作但在那之后,我失去了那个变量,我无法使用它。 谢谢!

1 个答案:

答案 0 :(得分:2)

看起来您遇到的问题是$id_sent来自其他网页的帖子。这就是你能够首先正确回应它的原因。

加载当前页面后,如果单击当前页面表单的“提交”按钮,则上一页的POST值将被此页面中的POST值覆盖。

为了保留此页面帖子的值,请将其存储在隐藏字段中,如此

<input type ='hidden' name='id' value='<? php echo $id_sent; ?>'>

所以在这里,$id_sent周围的第一次被设置为上一页帖子中id的值。它也会在此页面的表单中设置为名为id的隐藏字段,然后将其用于后续表单提交