首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将多个记录插入到MySQL wordpress数据库中?

如何将多个记录插入到MySQL wordpress数据库中?
EN

Stack Overflow用户
提问于 2017-06-01 17:57:38
回答 2查看 453关注 0票数 0

我真的很难在MySQL中插入多个记录。我使用的是这个参考资料:multiple.asp

这是我得到的错误:

错误:插入clk_9d0b4f116f_wp_comments (comment_post_ID,comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_date_gmt,comment_content,comment_karma,comment_approved,comment_agent,comment_type,comment_parent,user_id)值( '1','Ai',‘ai@aipresscoach.com’,'',‘:1’,'2017-06-01 19:49:15',在clk_9d0b4f116f_wp_comments (comment_post_ID,comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_date_gmt,comment_content,comment_karma,comment_approved,comment_agent,comment_type,comment_parent,user_id)中插入'2017-06-01 17:15‘,'0','1',’1‘,'','0',’2‘)值( '25','Ai',‘ai@aipresscoach.com’,'',‘:1’,'2017-06-01 19:49:15','2017-06-01 17:49:15','','0','1','0','2')

您的SQL语法出现了错误;请检查与MySQL服务器版本对应的手册,以获得在第16行“插入到clk_9d0b4f116f_wp_comments (comment_post_ID,comment_author,注释”)附近使用的正确语法。

下面是代码:

代码语言:javascript
复制
<?php
$servername = "xxxx";
$server_username = "xxxx";
$server_password = "xxxx";
$dbname = "xxxx";

$comment_id = 5;
$comment_id += 1;
$content = $_POST["blogPostPost"];// "this is the client answer";
$clientCur = $_POST["clientCurrent"];// "this is the client current 
situation";
$clientAlt = $_POST["clientAlternative"];// "this is the client alternative 
answer";
$title = "unity blog post";
$timelocal = date('Y-m-d H:i:s');
$timegmt = gmdate("Y-m-d H:i:s");
$table = "clk_9d0b4f116f_wp_comments";

// Create connection
$conn = mysqli_connect($servername, $server_username, $server_password, 
$dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO $table (comment_post_ID, comment_author, 
comment_author_email, comment_author_url, comment_author_IP, comment_date, 
comment_date_gmt, comment_content, comment_karma, comment_approved, 
comment_agent, comment_type, comment_parent, user_id)
        VALUES (
        '1',
        'Ai',
        'ai@aistresscoach.com',
        '',
        '::1',
        '".$timelocal."',
        '".$timegmt."',
        '".$content."',
        '0',
        '1',
        '',
        '',
        '0',
        '2')";

$sql .= "INSERT INTO $table (comment_post_ID, comment_author, 
comment_author_email, comment_author_url, comment_author_IP, comment_date, 
comment_date_gmt, comment_content, comment_karma, comment_approved, 
comment_agent, comment_type, comment_parent, user_id)
        VALUES (
        '25',
        'Ai',
        'ai@aistresscoach.com',
        '',
        '::1',
        '".$timelocal."',
        '".$timegmt."',
        '".$clientCur."',
        '0',
        '1',
        '',
        '',
        '0',
        '2')";

if (mysqli_multi_query($conn, $sql)) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

任何意见和想法都是非常感谢的。亲切问候伊利亚斯

EN

回答 2

Stack Overflow用户

发布于 2017-06-01 18:37:03

您的查询可以是:

代码语言:javascript
复制
$sql = "
INSERT INTO $table 
( comment_post_ID
, comment_author
, comment_author_email
, comment_author_url
, comment_author_IP
, comment_date
, comment_date_gmt
, comment_content
, comment_karma
, comment_approved
, comment_agent
, comment_type
, comment_parent
, user_id) VALUES 
(1,'Ai','ai@aistresscoach.com','','::1','$timelocal','$timegmt','$content',0,1,'','',0,2),
(25,'Ai','ai@aistresscoach.com','','::1','$timelocal','$timegmt','$clientCur',0,1,'','',0,2);
";

然后你就可以摆脱所有那些多查询的东西了。

票数 2
EN

Stack Overflow用户

发布于 2017-06-01 18:08:28

尝试用';‘来完成您的第一个$sql

代码语言:javascript
复制
$sql = "INSERT INTO $table (comment_post_ID, comment_author, 
comment_author_email, comment_author_url, comment_author_IP, comment_date, 
comment_date_gmt, comment_content, comment_karma, comment_approved, 
comment_agent, comment_type, comment_parent, user_id)
        VALUES (
        '1',
        'Ai',
        'ai@aistresscoach.com',
        '',
        '::1',
        '".$timelocal."',
        '".$timegmt."',
        '".$content."',
        '0',
        '1',
        '',
        '',
        '0',
        '2');";
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44314195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档