如何在MySQL wordpress数据库中插入多条记录?

时间:2017-06-01 17:57:38

标签: php mysql wordpress records

我真的很难在MySQL中插入多条记录。我使用此引用:https://www.w3schools.com/php/php_mysql_insert_multiple.asp

这是我得到的错误:

错误:INSERT INTO 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)VALUES(' 1',&# 39; Ai',' ai@aistresscoach.com','',' :: 1',' 2017-06-01 19 :49:15',' 2017-06-01 17:49:15','',' 0',' 1&# 39;,'','',' 0',' 2')插入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)VALUES(' 25',' Ai',' ai@aistresscoach.com& #39;,'',' :: 1',' 2017-06-01 19:49:15',' 2017-06- 01 17:49:15','',' 0',' 1','' ,'' 0',' 2')

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在' INSERT INTO clk_9d0b4f116f_wp_comments(comment_post_ID,comment_author,comment'第16行

附近使用正确的语法

以下是代码:

<?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);
?>

非常感谢任何评论和想法。 亲切的问候伊莱亚斯

2 个答案:

答案 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);
";

然后你可以摆脱所有那些多重的东西。

答案 1 :(得分:-1)

尝试使用';'完成第一个$ 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');";