【发布时间】:2015-07-22 09:33:26
【问题描述】:
我想知道是否有人可以看看我下面的代码,让我知道我可能做错了什么。我希望同时更新多个记录。
当我加载页面时,所有当前值都被输入,但是当我提交任何更改时,什么都没有保存,也没有错误。/
我没有使用任何编号 ID,因为项目名称是静态且唯一的,并且我将其用作主键。
代码如下:
<?php
require_once('deets.php');
mysql_connect($hostname,$username,$password) OR DIE ('Unable to connect to database! Please try again later.');
@mysql_select_db($dbname) or die( "Unable to select database");
$query_Recordset1 = "SELECT * FROM stock_info";
$Recordset1 = mysql_query($query_Recordset1);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
if(isset($_POST['submit'])) {
// $count = count($_POST['item']);
// $count=mysql_num_rows($Recordset1);
$submit = $_GET['submit'];
$i = ($_POST['count']);
$current_levels = ($_POST['current_levels']);
$stock_req = ($_POST['stock_req']);
$item = ($_POST['item']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE stock_info SET current_levels='{$_POST['current_levels'][$i]}',
stock_req='{$_POST['stock_req'][$i]}',
WHERE item='{$_POST['item'][$i]}'";
$row_Recordset1=mysql_query($sql1);
}
if($row_Recordset1){
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form2" method="post" action="">
<table width="634" border="1">
<tr>
<td>item</td>
<td>current levels</td>
<td>stock required</td>
</tr>
<?php do { ?>
<tr>
<td><?php $item[]=$row_Recordset1['item']; ?><?php echo $row_Recordset1['item']; ?>
<input name="item[]" type="hidden" value="<?php echo $row_Recordset1['item']; ?>" /></td>
<td><input name="current_levels[]" type="text" value="<?php echo $row_Recordset1['current_levels']; ?>"></td>
<td><input name="stock_req[]" type="text" value="<?php echo $row_Recordset1['stock_req']; ?>"></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<p>
</p>
</body>
</html>
提前致谢。
杰森
【问题讨论】:
-
$count已被评论。所以你不能进去for,他永远都是假的。您需要声明 $count。