duanjiu4498 2014-10-20 11:33
浏览 17
已采纳

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

I have this code:

<?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  }
  ?>

My problem is that somehow I loose the variable $id_sent (it is sent using POST from other page) and I am not able to query correctly using id. When I echo the variable in line 3 is working but after that somehow I am losing that variable and I am not able to use it. Thank you!

  • 写回答

1条回答 默认 最新

  • duandou8120 2014-10-20 11:43
    关注

    Looks like the problem you are having is that the $id_sent is coming from another page's post. This is the reason you are able to echo it correctly in the first place.

    After the current page is loaded, and you click on submit button of the current page's form, the POST values from the previous page is overwritten by the POST values from this page.

    In order to retain the value across this page's post as well, store it in a hidden field like so

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

    So here, the first time around the $id_sent is set as the value of id from previous page post. It is also set into a hidden field named id in this page's form, which will then be considered for subsequent form submissions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题