gpt4 book ai didi

php - 如何将php数组插入到mysql表中

转载 作者:行者123 更新时间:2023-11-29 10:39:13 24 4
gpt4 key购买 nike

我有一个php数组,我试图插入到mysql表中但没有成功,表结构有两个字段,id(int自动递增主键)和name(varchar),代码是:

<?php


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//get values for array
$domains_test = $client->sites_web_domain_get($session_id, array('domain' =>
'%'));




foreach($domains_test as $domains) {

$arr = explode('.', $domains['domain']);
unset($arr[0]);
$arry = implode('.', $arr);


$testsql = "INSERT INTO table1 (id, name) VALUES ('','".$arry."')";

if (mysqli_query($conn, $testsql)) {
echo "New record created successfully";
} else {
echo "Error: " . $testsql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

}

?>

查询将第一个值插入表中,但出现以下错误:

> New record created successfully
Error: INSERT INTO table1 (id, name) VALUES ('','john')
Error: INSERT INTO popular_domains2 (id, name) VALUES ('','fred')
Error: INSERT INTO popular_domains2 (id, name) VALUES ('','james')

我插入的记录可能包含重复值。感谢您的帮助!

最佳答案

换行

$testsql = "INSERT INTO table1 (id, name) VALUES ('','".$arry."')";

$testsql = "INSERT INTO table1 (name) VALUES ('".$arry."')";

关于php - 如何将php数组插入到mysql表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com