将逗号分隔字符串中的数组值插入 mysql
Insert array values from comma separated string into mysql
我有这个逗号分隔的数据,其中行由 ;
分隔
04:03:49,https://foo.bar/1; 04:03:34,https://foo.bar/2; 04:03:24,https://foo.bar/3; 04:03:09,https://foo.bar/4; 04:03:07,https://foo.bar/5; 04:03:07,https://foo.bar/6; 04:02:41,https://foo.bar/7;
还有一个像这样的 mysql table:
time
link
04:03:49
https://foo.bar/1
04:03:34
https://foo.bar/2
所以我使用这段代码将数据从 $_POST 转换为数组:
$data=@$_POST['array'];
$array=explode(';', $data);
这导致:
Array (
[0] => 04:03:49,https://foo.bar/1
[1] => 04:03:34,https://foo.bar/2
[2] => 04:03:24,https://foo.bar/3
[3] => 04:03:09,https://foo.bar/4
[4] => 04:03:07,https://foo.bar/5
[5] => 04:03:07,https://foo.bar/6
[6] => 04:02:41,https://foo.bar/7
[7] =>
)
所以,我需要使用一列中的时间和另一列中的 link 将该数据插入到我的数据库中,尝试了几个示例但似乎无法找到答案提前谢谢寻求帮助。
我试过使用这个查询:
$consulta= "DECLARE @array varchar(max) = '($array)'
SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '')
DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO hora (hora,link) VALUES ' + @array
EXEC (@SQLQuery)";
但它出错了:
Warning: Array to string conversion in C:\xampp8\htdocs\plantas\index.php on line 138
您可以循环遍历 $array
,然后使用 ,
分隔符再次分解它。然后你可以 运行 MySQL 插入查询将数据插入你的 table.
喜欢
foreach($array as $row){
$data = explode(',', $array);
//Run MySQL query here to insert this data in your table
$sql_query = "INSERT INTO hora (hora,link) VALUES ('".$data[0].'", '".$data[1].'") ";
//Something like
$mysqli->query($sql_query);
}
切记在将用户数据插入数据库之前清理用户数据
更新:如果使用 mysqli
$query = $mysql->prepare("INSERT INTO hora (hora,link) VALUES (?, ?)");
$query->bind_param("ss", $data[0], $data[1]);
$query->execute();
这将阻止 SQL Injection attacks。
我有这个逗号分隔的数据,其中行由 ;
04:03:49,https://foo.bar/1; 04:03:34,https://foo.bar/2; 04:03:24,https://foo.bar/3; 04:03:09,https://foo.bar/4; 04:03:07,https://foo.bar/5; 04:03:07,https://foo.bar/6; 04:02:41,https://foo.bar/7;
还有一个像这样的 mysql table:
time | link |
---|---|
04:03:49 | https://foo.bar/1 |
04:03:34 | https://foo.bar/2 |
所以我使用这段代码将数据从 $_POST 转换为数组:
$data=@$_POST['array'];
$array=explode(';', $data);
这导致:
Array (
[0] => 04:03:49,https://foo.bar/1
[1] => 04:03:34,https://foo.bar/2
[2] => 04:03:24,https://foo.bar/3
[3] => 04:03:09,https://foo.bar/4
[4] => 04:03:07,https://foo.bar/5
[5] => 04:03:07,https://foo.bar/6
[6] => 04:02:41,https://foo.bar/7
[7] =>
)
所以,我需要使用一列中的时间和另一列中的 link 将该数据插入到我的数据库中,尝试了几个示例但似乎无法找到答案提前谢谢寻求帮助。
我试过使用这个查询:
$consulta= "DECLARE @array varchar(max) = '($array)'
SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '')
DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO hora (hora,link) VALUES ' + @array
EXEC (@SQLQuery)";
但它出错了:
Warning: Array to string conversion in C:\xampp8\htdocs\plantas\index.php on line 138
您可以循环遍历 $array
,然后使用 ,
分隔符再次分解它。然后你可以 运行 MySQL 插入查询将数据插入你的 table.
喜欢
foreach($array as $row){
$data = explode(',', $array);
//Run MySQL query here to insert this data in your table
$sql_query = "INSERT INTO hora (hora,link) VALUES ('".$data[0].'", '".$data[1].'") ";
//Something like
$mysqli->query($sql_query);
}
切记在将用户数据插入数据库之前清理用户数据
更新:如果使用 mysqli
$query = $mysql->prepare("INSERT INTO hora (hora,link) VALUES (?, ?)");
$query->bind_param("ss", $data[0], $data[1]);
$query->execute();
这将阻止 SQL Injection attacks。