日期:2014-05-17  浏览次数:20416 次

php+mysql 如何用循环的方式把表单内容update到数据库
我目前需要在一个网页动态创建表格,比如10行,填写表格后通过表单提交
代码如下:
PHP code
    
        echo "<form name=\"form1\" method=\"post\" action=\"process.php\"><table border='1' id=\"oTable\">
    <tr>
    <td>文件名</td>
    <td>文件大小</td>
    <td>播放时间1</td>
    <td>播放时间2</td>
    <td>播放时间3</td>
    </tr>";
    $i = 0;
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['filename'] . "</td>";
        echo "<td>" . $row['filesize'] . "</td>";//数据表中已经有文件名和文件大小,但是播放时间都默认为0,通过网页修改来update数据库

        echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";
        echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";
        echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";
        echo "</tr>"; 
    }
        echo "<input name=\"submit\" type=\"submit\" value=\"保存\" />";
        echo "<br>";
        echo "<input name=\"cancel\" type=\"button\" value=\"取消\" onClick=\"window.location.href=\'admincentre.php\'\"/>";
        echo "</table></form>"; 




我想在process.php中进行处理,
$w = 0;
while($row = mysql_fetch_array($result))
{
$filename = $row['filename'];
$time1 = $_POST["$w"];
$w++;
$time2 = $_POST["$w"];
$w++;
$time3 = $_POST["$w"];
$w++;
echo "此时的w=".$w."<br>";
$sql = "UPDATE fileinfo SET time1='".$time1."',time2='".$time2."',time3='".$time3."' WHERE filename='".$filename."'";
$re = mysql_query($sql) or die("error:".mysql_error());
echo $sql."---结果是:".$re."<br>";
}

post过来的$time1,$time2,$time3都是空值,改成$time1 = $_POST[$w]也不行,$time1 = $_POST['$w']也不行,不知道$_POST[]里面的值能否是变量,该怎么表示啊。。。。怎么处理这种提交一个表格的情况!!

------解决方案--------------------
你查看一下前台页面,命名对吗?
------解决方案--------------------
这样试试
PHP code
 $i = 0;
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['filename'] . "</td>";
        echo "<td>" . $row['filesize'] . "</td>";//数据表中已经有文件名和文件大小,但是播放时间都默认为0,通过网页修改来update数据库

        echo "<td><input name=\"<?php echo 'time1_'.$i; ?>\" type=\"text\"></td>";
        echo "<td><input name=\"<?php echo 'time2_'.$i; ?>\" type=\"text\"></td>";
        echo "<td><input name=\"<?php echo 'time3_'.$i;; ?>\" type=\"text\"></td>";
        echo "</tr>";
 $i++;
    }

------解决方案--------------------
你先 print_r($_POST); 看看
------解决方案--------------------
echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";
echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";