简体   繁体   中英

How to insert multiple records into MySQL wordpress database?

I'm really struggling to insert multiple records into MySQL. I'm using this reference: https://www.w3schools.com/php/php_mysql_insert_multiple.asp

This is the error i get:

Error: 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', 'Ai', 'ai@aistresscoach.com', '', '::1', '2017-06-01 19:49:15', '2017-06-01 17:49:15', '', '0', '1', '', '', '0', '2')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 ( '25', 'Ai', 'ai@aistresscoach.com', '', '::1', '2017-06-01 19:49:15', '2017-06-01 17:49:15', '', '0', '1', '', '', '0', '2')

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO clk_9d0b4f116f_wp_comments (comment_post_ID, comment_author, comment' at line 16

Here's the code:

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

Any comments and ideas are much appreciated. Kind regards Elias

Your query can be this:

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

Then you can get rid of all that multiquery stuff.

Try to finish your first $sql with ';' like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM