0

我正在尝试保存一个数组,所以我有以下代码:

<?php

$sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);


$i = -1; // index des enregistrements
?>
<table cellpadding="5" cellspacing="5">
   <tr>
      <td><strong>CODE SCENARIO</strong></td>
      <td><strong>LIBELLE</strong></td>
      <td><strong>ACTION</strong></td>
      <td><strong>DESCRIPTION</strong></td>
      <td><strong>DATE</strong></td>
   </tr>
   <form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
      <input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
      <input type="hidden" name="n_doss" value="<?php echo $_POST['n_doss']; ?>"/>
      <input type="hidden" name="qualite" value="<?php echo $_POST['qualite']; ?>"/>
      <?php while($row = mysql_fetch_assoc($qry)): ?>
      <tr>
         <td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
         <td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
         <td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
         <td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
         <td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']) ; ?>" size="12"></td>
      </tr>
      <?php endwhile; ?>

为了保存这个我有这个代码:

if (isset($_POST['liasse']))  {
$value = $_POST['data'] ; 

foreach($value as $key => $array)
{

        $sql = 'INSERT INTO agenda SET
        liasse = "'.mysql_real_escape_string($_POST['liasse']).'",
        code_s = "'.mysql_real_escape_string($array['code_s']).'",
        date_action = "'.date('Y-m-d',strtotime($array['date'])).'", 
        libelle = "'.mysql_real_escape_string($array['titre']).'",
        action = "'.mysql_real_escape_string($array['action']).'",
        description = "'.mysql_real_escape_string($array['libelle']).'",
        n_doss = "'.mysql_real_escape_string($_POST['n_doss']).'",
        qualite = "'.mysql_real_escape_string($_POST['qualite']).'"
        ';
mysql_query($sql) or die(__LINE__.mysql_error().$sql);

}

但我真的迷路了

实际上我为此使用了一个表格,现在我想提交所有这些数据但没有任何表格,当我有第一个时我想保存它。

问题是我迷路了,因为我不能调用任何像 data[][code_s] 这样的 var。

所以我不知道如何保存这个。我想将它保存在后台,而不是显示已保存的内容。

接受我最崇高的敬意

亲切的问候,

SP。

4

1 回答 1

1

将下部代码块的代码包装成一个函数,并将值数组作为参数传递:

function storeValues ($data) {
    foreach($data as $key => $val) 
    {
        $catalog=sprintf("%s='%s'",$key,$val);
        $sql = sprintf('INSERT INTO agenda SET %s', implode(',',$catalog));
        mysql_query($sql) or die(__LINE__.mysql_error().$sql);
    } // foreach 
} // function storeValues

当您要保存值时调用此函数。所以在从数据库中检索它们之后。您为检索到的每一行调用它并交出如下值:

storeValues ($row);

这将一次存储一行值。显然,这可以优化为使用多重插入。但是,让我们一步一步来……

于 2012-10-04T13:24:51.357 回答