Reputation: 161
I tried to insert data to a table named Ships, but sadly it doesn't work. I pasted the code, to give you a better view about how I wrote my script. Any help is much appreciated!
<?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?" );
}
?>
Upvotes: 3
Views: 5287
Reputation: 423
You can also refer this way of inserting
<?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' ) )
?>
Upvotes: 2
Reputation: 8819
actual way to add data is
<?php $wpdb->insert( $table, $data, $format ); ?>
$wpdb->insert(
'table',
array(
'column1' => 'value1',
'column2' => 123
),
array(
'%s',
'%d'
)
);
and use
$wpdb->insert_id
to get insert id
Upvotes: 6