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

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。