3

我试图将数据插入到名为 Ships 的表中,但遗憾的是它不起作用。我粘贴了代码,以便您更好地了解我是如何编写脚本的。任何帮助深表感谢!

<?php
require_once('../../../wp-config.php');
global $wpdb;

?>

<?php

    $json_data = $_POST['info'];

    if( isset($json_data) ){

        $eniNumber  = $json_data['EniNumber'];
        $name       = $json_data['Name'];
        $startDate  = $json_data['StartDate'];
        $rows       = $json_data['Rows'];

        $table_name = $wpdb->prefix . "Ships";
        $result = $wpdb->insert( $table_name, array(
            'eniNumber' => $eniNumber,
            'name' => $name,
            'startDate' => $startDate
        ) );

        if( $result ){
            echo json_encode("Entered data successfully");
        }else{
            echo json_encode("Insertion failed");
        }

    } else {
        echo json_encode( "What happened?" );
    }

?>
4

2 回答 2

6

添加数据的实际方法是

<?php $wpdb->insert( $table, $data, $format ); ?>

$wpdb->insert( 
'table', 
array( 
    'column1' => 'value1', 
    'column2' => 123 
), 
array( 
    '%s', 
    '%d' 
) 
);

并使用

$wpdb->insert_id

获取插入ID

于 2013-10-17T12:55:55.067 回答
2

您也可以参考这种插入方式

<?php
// if using a custom function, you need this
global $wpdb

$lastn = 'Last Name';
$firstn = 'First Name';
$wpdb->insert( 'clients', array( 'last_name' => $lastn, 'first_name' => $firstn ), array( '%s', '%d' ) )

?>
于 2013-10-17T13:05:14.917 回答