通过PHP更新多个SQL记录

时间:2015-07-22 09:33:26

标签: php sql forms records

我想知道是否有人可以查看下面的代码,让我知道我可能做错了什么。我希望同时更新多个记录。

当我加载页面时,输入所有当前值,但是当我提交任何更改时,没有任何保存并且没有错误./

我没有使用任何编号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>

提前致谢。

杰森

1 个答案:

答案 0 :(得分:0)

您保存了$i = ($_POST['count']);。我认为它应该是$count=$_POST['count'];

如果没有,请定义$count;