last day (15 days later) » 

4:05 AM
0
Q: Insert statement into custom WordPress database

sleepy_dazeI have a custom database that I want to insert items into from a WordPress front-end interface. When I submit the form, I can get the JSON values just fine, but for some reason, it doesn't seem to be inserting into the database. I do not get any error messages. Here's what I'm doing (please note...

 
vel
Is Committee id primary key?
 
Committee_id is the primary key. I'm getting that value from the auto_increment_id that you get from SHOW TABLE STATUS
 
vel
Add this 'committee_id' => $id first array and remove the second and third array in inser
Remove the committee I'd in query and try
 
Didn't work-- do you mind moving this conversation to chat? I can provide more information about the code
 
vel
Yes. I don't mind
 
4:05 AM
Sorry, can you create the room? Not sure how... first time!
 
vel
Hi
 
Thank you! Whew! First off, I want to say thank you so much for helping-- I've been dealing with this issue for a long time
Okay. Let me get the HTML code
 
vel
Ok
 
Here's a sample:
<tr class="addRow">
<td> <!-- Name -->
<?php $next = $wpdb->get_var($wpdb->prepare(
"SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'ncpagorg_ss_dbname6d3'
AND TABLE_NAME = 'wp_bubu_committees'"));
?>
<form class="add" action="#" method="POST" id="add_<?php echo "{$next}"; ?>">

<input type="text" name="addCommName" class="form-control addbox" id="addCommName_input_
<?php echo "{$next}"; ?>" placeholder="Committee Name">
</td>
<td> <!-- Date Created -->
I think the issue might be that it's not picking up the committee_id
 
vel
you can get commitee id by last id+1
right?
 
4:10 AM
I can get it in the JS by doing:
$(".addButton").click(function() {
var commitAddID = $(this).attr('id');
 
vel
ok.
 
It seems to pass through well to JSON
 
vel
but in insert query you dont need to give commitee id for insert
 
So don't need it in the first array?
 
vel
Yes.
 
4:13 AM
Okay
I've removed it so now it's like this:
 
vel
$wpdb->insert('wp_bubu_committees',
array(
'old_id' => '',
'name' => $name,
'description' => '',
'date_created' => $created,
'status' => $status,
'disbanded' => $disbanded
)
);
 
Trying now
Thanks
 
vel
working?
 
vel
what is the error?
 
4:17 AM
No error, but it does print out my alert in my JS:
jQuery.ajax({
url: addCommittee.ajaxurl,
type: "POST",
data: dataString,
dataType: "json",

success: function (response) {
$("#name_"+commitAddID).html(name);
$("#created_"+commitAddID).html(date_created);
$("#status_"+commitAddID).html(status);
$("#disbanded_"+commitAddID).html(disbanded);
alert("Successful insertion!");
}
});
});
 
vel
exit( var_dump( $wpdb->last_query ) );
add this after insert query
and paste the result here
replace the alert("Successful insertion!"); with console.log(response);
 
Testing
Didn't print anything
Will try again though
Sorry about that
still nothing
I got it now-- the response from var_dump printed:
null
 
vel
what the response in console
?
 
Yeah, the response in the console printed null
 
vel
please confirm ajax calling correct function.
.
 
4:28 AM
Okay
 
vel
and please enable error -reporting
 
It was the wrong function... my bad...
So I've been able to successfully insert
However, it only seems to let me do one.
So if I submit one and then submit a second one, it doesn't insert the second one
Any ideas?
 
vel
Are you using your old code?
that is posted in SO?
?
 
 
14 hours later…
6:25 PM
using this from functions.php:
function addCommittee() {
global $wpdb;

$committee_id = esc_sql($_POST['committee_id']);
$old_id = esc_sql($_POST['old_id']);
$name = esc_sql($_POST['name']);
$created = esc_sql($_POST['date_created']);
$status = esc_sql($_POST['status']);
$disbanded = esc_sql($_POST['disbanded']);
$desc= esc_sql($_POST['description']);


$wpdb->insert('wp_bubu_committees',
array(
'old_id' => '',
'name' => $name,
'description' => '',
'date_created' => $created,
'status' => $status,
'disbanded' => $disbanded
)
);
This from the JS:
var name = $("#addCommName_input_"+commitAddID).val()
var date_created = $("#addCommCreated_input_"+commitAddID).val();
var status = $("#addCommStatus_input_"+commitAddID).val();
var disbanded = $("#addCommDisbanded_input_"+commitAddID).val();
var old_id = commitAddID + 1;
var desc = 'stuff';

console.log(date_created);


var dataString = {
'action': 'addCommittee',
'committee_id': commitAddID,
'old_id': old_id,
'name': name,
'description': desc,
'date_created': date_created,
'status': status,
'disbanded': disbanded
 

  last day (15 days later) »