代码:
<?php for($i = 0;$i < count($smslist);$i++){ ?>
<td align="center"><?php echo $smslist[$i] -> getMo_from(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMo_to();   ?></td>
<td align="center"><?php echo $smslist[$i] -> getCreate_time(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMsg_content(); ?></td>
<td align="center">
<input type="hidden" name="status" value="<?php echo $status;   ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
        <input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
        <input type="submit" name="button" id="button" value="确认" />
         </td><?php } ?>
假设 echo $smslist[$i] -> getMsg_content();这里是从1~50,每条数据后有一个确认按钮,点击确认写数据库,现在的问题是我点击任意确认,写入数据库的都是第50条数据,请问那里出现问题了呀?

解决方案 »

  1.   

    <?php
    if(isset($_POST["submit"]))
    {
    print_r($_POST);
    }
    ?>
    <form method="POST">
    <input type='text' name="content" value='content111'><br>
    <input type='text' name="content" value='content222'><br>
    <input type="submit" name="submit" value="提交">
    </form>
      

  2.   

    <input type="hidden" name="msg_content" value=" <?php echo $smslist[$i] -> getMsg_content(); ?>">name="msg_content" 50个都一个样,提交后就只取最后一个 
      

  3.   

    把<form>标签提进来一起循环 试试!分成每个独立的表单 这样应该就没问题了!
      

  4.   


    不行的,把form扔进去一起循环还是提交最后一个
      

  5.   

    帅哥,是这个回事:
    <input type="hidden" name="status" value=" <?php echo $status;  ?>"> 
    <input type="hidden" name="mobile" value=" <?php echo $smslist[$i] -> getMo_to(); ?>"> 
          <input type="hidden" name="msg_content" value=" <?php echo $smslist[$i] -> getMsg_content(); ?>"> 你提交到的是同一个php页面,php里面写的是
     $_post['status']
     $_post['mobile']
     $_post['msg_content']
    这样你获得的肯定是50个中的最后一个;
    解决方法:  把form表单一块写到循环中,是可以的
      

  6.   

    <?php for($i = 0;$i < count($smslist);$i++){ ?> 
    ...
    ...
    <form method="post" runat="server">
    <input type="hidden" name="status" value="<?php echo $status;   ?>">
    <input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
    <input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
    <input type="submit" name="button" id="button" value="确定" />
    </from>
    我改成这样之后依然不可以啊,确定之后提交的依然是50,费解
      

  7.   

    是不是你的记录有问题,每条记录都是样的,你把50 条记录都echo 出来,看看每条记录的值
      

  8.   

    记录是没问题的,确定按钮就是在数据后面的,先确定数据正确才提交的.而且我把for循环的$i也提交上去打印了,每次$i都是50
      

  9.   

    //提交页面,接收参数
    $i  = $_POST['i'];
    $pbegin   = $_POST['pbegin'];
    $pend   = $_POST['pend']  ;
    $mobile  = $_POST['mobile'];
    $status  = $_POST['status'];
    $p_button  = $_POST['button'];
    $msg_content = $_POST['msg_content'];//判断是否写数据库
    $smsinfo = new mm_full_sms_control($conn);
    if ($p_button == "确定")
    {
        $smsinfo -> insert($mobile, $msg_content);
    }
    $smslist = $smsinfo -> selectBySMS($mobile, $pbegin, $pend);//列数据显示<td align="center">
    <form method="post" runat="server">
    <input type="hidden" name="status" value="<?php echo $status;   ?>">
    <input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
    <input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
    <input type="hidden" name="i" value="<?php echo $i ?>">
    <input type="submit" name="button" id="button" value="确定" 
    onclick="return   confirm('确认内容<?php echo $smslist[$i] -> getMsg_content(); ?>')" />
    </from>
      

  10.   

    11楼就是完整的了,都是在一个页面进行的操作.现在的问题就是点击确定之后数据提交到本页面直接输出msg_content为循环的最后一条数据
      

  11.   

    显示、提交、接收都在同一个页面,代码已经全了啊..除了表格样式什么的,其他的都在这呢思路是  for循环显示数据,在每行数据的内容后面有一个确定按钮,点击确定按钮弹出一个二次确认对话框,再次确认之后提交到当前页面进行判断然后插入数据库.问题是  我想提交的内容为"10"由于提交的msg_content名称是相同的所以每次提交的数据都是"50"(最后一条),把from表单放到for循环里面后二次确认的内容是正确的(内容为10),但是post传递到判断那里输出的不是我想要的(10)而是最后一条数据(50),可以通过GET方式解决,但是我想用POST解决.
      

  12.   

    补充一下,把from放到循环里解决不了问题,因为from表单的名字也是一样的,每次提交都是提交最后一个from里的数据
      

  13.   


    print_r($_POST);
    for($i = 0; $i < 10; $i++)

    echo "
    <form method='post'>
    <input type='hidden' name='status' value='$i'> 
    <input type='hidden' name='mobile' value='$i'> 
    <input type='hidden' name='msg_content' value='$i'> 
    <input type='submit' name='button' id='button' value='确认' /> 
    </form>
    ";

    运行以上代码你就知道加form到循环里面是可行的。至于为什么提交的都是同一条数据,那要看你是不是获取数据错误,查看页面源代码,是否每个表单的值都是相同的。
      

  14.   

    还有你的form里面没有提交地址,提交后是到当前页的。
    你是否是要提交到当前页?如果是,那你代码中没看到循环中的数据做循环
      

  15.   


    <?php for($i = 0;$i < count($smslist);$i++){ ?>
    <td align="center"><?php echo $smslist[$i] -> getMo_from(); ?></td>
    <td align="center"><?php echo $smslist[$i] -> getMo_to(); ?></td>
    <td align="center"><?php echo $smslist[$i] -> getCreate_time(); ?></td>
    <td align="center"><?php echo $smslist[$i] -> getMsg_content(); ?></td>
    <td align="center">
    <form method="post" runat="server">
    <input type="hidden" name="status" value="<?php echo $status;   ?>">
    <input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
    <input type="hidden" name="msg_content" value="<?php echo $smslist[$i] ->getMsg_content(); ?>">
    <input type="submit" name="button" id="button" value="确定" onclick="return  confirm('确认内容 <?php echo $smslist[$i] -> getMsg_content(); ?>')" /> 
    </from>
    <?php } ?>
    查看源代码的数据正常的,从1~50顺序排列.表单是提交到当前页面,上面的代码就是循环的完整代码了.$pbegin = $_POST['pbegin']; 
    $pend = $_POST['pend']  ; 
    $mobile = $_POST['mobile']; 
    $status = $_POST['status']; 
    $p_button = $_POST['button']; 
    $msg_content = $_POST['msg_content']; 
    上面是提交后的接收参数$smsinfo = new mm_full_sms_control($conn);
         if ($p_button == "确定")
        {
             $smsinfo -> insert($mobile, $msg_content);
             }
         $smslist = $smsinfo -> selectBySMS($mobile, $pbegin, $pend);
         }上面是判断参数插入数据库.18楼的代码是正确的,可是我插入的数据总是最后一条.
      

  16.   

    你代码最后的是</from>不是</form>
      

  17.   

    好象是你可以在input 的name属性里面加个括号,心数组形式提交回去,带上i一起提交,后台就取第i维。
    也可以把submit 改成 button 加个onclik事件,事件传参数i,取相应的值,用ajax提交,也可以用一个隐藏表单提交