php - 从数组保存数据而不提交任何表单php

标签 php mysql arrays while-loop save

我正在尝试保存数组,因此我有以下代码:

<?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。

最佳答案

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

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

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

关于php - 从数组保存数据而不提交任何表单php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12726677/

相关文章:

php - react 本地axios ios不针对apache/php进行身份验证

javascript - 无法从extjs ajax接收post参数

python - 无法使用 python Pymysql 更新 SQL 数据库

arrays - 在没有内置 ruby​​ 函数的情况下合并两个排序数组

javascript - 用随机字母替换字符串中的任意 3 个字母

javascript - JS二维数组错误: "TypeError: Cannot read property ' 0' of undefined"

php - RouteCollection.php 第 218 行中的 MethodNotAllowedHttpException :4

php - 左连接mysql问题

sql - MYSQL 选择值的 "all"而不是其中的 "any"?

php - 在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前...