首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

php+mysql 怎么用循环的方式把表单内容update到数据库

2012-09-16 
php+mysql 如何用循环的方式把表单内容update到数据库我目前需要在一个网页动态创建表格,比如10行,填写表

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>";


echo "<td><input name=\"<?php echo $i;$i++; ?>\" type=\"text\"></td>";

你这样写表单 就是 <input name=‘1’ type='text'> 用数字的名字。。。
建议你先在php打印 priny_r($_POST) 看一下有没有值。
再者建议改一下input 
echo "<td><input name=\"<?php echo 'time'.$i; ?>\" type=\"text\"></td>";

热点排行