【问题标题】:Enter each element of an array as a record of each mysql column输入数组的每个元素作为每个mysql列的记录
【发布时间】:2018-02-23 01:05:56
【问题描述】:

我想要的是在我的数据库的一个字段中输入数组的每个元素这是我的结构示例表记录有四个字段 value1、value2 value3、value4 我想输入从以下代码生成的以下记录

arraynew = array("5878-1","8978-12","2523-1");
$dato1 = "00320555555";
$dato2 = "22/02/2018";
$dato3 = "Maria Mercedes del Barrio";
$dato4 = $arraynew;

$data = array($dato1,$dato2,$dato3,$dato4);

$nuevo = array();
for($i= 0; $i < count($dato4); $i++ ){
    array_push($nuevo, array(
        $data[0],
        $data[1],
        $data[2],
        $data[3][$i]
    ));
}
echo print_r($nuevo);
echo var_dump($nuevo);

这是数组的结构

Array ( [0] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 5878-1 ) [1] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 8978-12 ) [2] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 2523-1 ) ) 1
array (size=3)
  0 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '5878-1' (length=6)
  1 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '8978-12' (length=7)
  2 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '2523-1' (length=6)

我需要输入每个数组,将其与相应的字段一起输入一行

【问题讨论】:

  • 一个简单的插入查询?至少尝试一下
  • 我的头好痛。
  • 输入数据是否来自用户输入?还是这是受信任/安全的数据?
  • 你好,我已经尝试过我所做的就是为每个 annulated 和在最后一个中间为每个创建 sql 语句,我唯一输入的是最后一个值数组和我需要的所有这些数据我从表单中获得,但最后一个不重复的字段是从用户输入的动态输入中获得的,因为它们是 N 个不同的注册

标签: php mysql arrays


【解决方案1】:

我假设您正在处理用户提供的数据,因此需要一个 mysqli 准备好的语句。我的方法将对数据库进行 3 次调用,并记录查询产生的受影响行数。

(我已经在我的服务器上测试过这段代码是成功的。)

代码:

$mysqli = new mysqli(...$fourIndexedCredentials);
$stmt = $mysqli->prepare("INSERT INTO tablename (value1, value2, value3, value4) VALUES (?,?,?,?)");
$stmt->bind_param("ssss", $dato1, $dato2, $dato3, $dato4);

$dato1 = "00320555555";
$dato2 = "22/02/2018";
$dato3 = "Maria Mercedes del Barrio";
$arraynew = ["5878-1", "8978-12", "2523-1"];
$tally = 0;
foreach ($arraynew as $dato4) {  // the declaration of $dato4 is all that is required for binding
    if ($stmt->execute()) {
        ++$tally;
    }
}
echo $tally;  // output: 3

【讨论】:

  • 谢谢你,我今天会努力工作,我正在分析确定变化,非常感谢 mickmackusa
  • 如果您对 php 或 db 表结构有疑问,尽管问我。
  • drive.google.com/file/d/1wJnJyqTQ81AAGfU4OdD_y-V5fuPtpV0Q/…这是我的数据库结构请帮帮我
  • Estructura 视图对我来说更重要。 id 列需要是主键,并且应该自动递增。我不知道你的技术水平,你卡在哪里了?
  • 这是唯一一个西班牙语项目drive.google.com/drive/folders/…感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多