Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多条记录插入MySQL wordpress数据库?_Php_Mysql_Wordpress_Records - Fatal编程技术网

Php 如何将多条记录插入MySQL wordpress数据库?

Php 如何将多条记录插入MySQL wordpress数据库?,php,mysql,wordpress,records,Php,Mysql,Wordpress,Records,我真的很难在MySQL中插入多条记录。我正在使用此引用: 这是我得到的错误: 错误:插入clk_9d0b4f116f_wp_评论帖子ID、评论作者、评论作者电子邮件、评论作者url、评论作者IP、评论日期、评论日期gmt、评论内容、评论业力、评论批准、评论代理、评论类型、评论家长、用户ID值“1”、“Ai”和“1”ai@aistresscoach.com', '::1', '2017-06-01 19:49:15',‘2017-06-01 17:49:15’、‘0’、‘1’、‘0’、‘2’插入c

我真的很难在MySQL中插入多条记录。我正在使用此引用:

这是我得到的错误:

错误:插入clk_9d0b4f116f_wp_评论帖子ID、评论作者、评论作者电子邮件、评论作者url、评论作者IP、评论日期、评论日期gmt、评论内容、评论业力、评论批准、评论代理、评论类型、评论家长、用户ID值“1”、“Ai”和“1”ai@aistresscoach.com', '::1', '2017-06-01 19:49:15',‘2017-06-01 17:49:15’、‘0’、‘1’、‘0’、‘2’插入clk_9d0b4f116f_wp_评论评论帖子ID、评论作者、评论作者电子邮件、评论作者url、评论作者IP、评论日期、评论日期gmt、评论内容、评论业力、评论批准、评论代理、评论类型、评论家长、用户ID值‘25’、‘Ai’, 'ai@aistresscoach.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,comment”附近使用的正确语法

代码如下:

<?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);
?>
任何意见和想法都将不胜感激。
好心的Elias

试着用“;”来完成你的第一个$sql像

$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 
(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);
";

然后,您就可以摆脱所有的多查询内容。

您需要将SQL语句与;请在引号“2”中检查sql语句是否没有分号;在结束引号前插入分号,如2';;为什么不做一次插入?!?!?!?!?非常感谢,这是一个很好的解决方案!:有一个由来已久的传统,就是“向上投票”的答案是经过深思熟虑和有益的,而“接受”那些真正解决问题的答案。欢迎加入!谢谢你的解决方案,但是它没有解决我的问题。