I am currently working on a piece of code that inserts data from a recent upload in the database. I tried different ways to make it work, however it does not work.
I tried the following at first:
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "Er is iets fout gegaan, excuses voor het ongemak";
} else {
$sql = "INSERT INTO table (id, name) VALUES (1, 'Cake')";
var_dump(error_get_last($sql));
The var_dump(error_get_last($sql));
returns NULL. I do not know why, a connection is not needed since Wordpress automatically makes a connection with the database.
I also tried the following:
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "Er is iets fout gegaan, excuses voor het ongemak";
} else {
// insert data in de database
$sql = "INSERT INTO $wpdb->wpex_programma (id, naam) VALUES (2, 'Country Kickin 2')";
$prepare_query = $wpdb->prepare($sql);
$result = $wpdb->sql($prepare_query);
echo '<pre>';
print_r($result);
Normally, when a user uploads a file, the user can view his file on the page. Because of this code, that was not possible anymore. I do not know how to solve it.
This is the whole if and else statement:
if ((!$conn_id) || (!$login_result)) {
echo "Het spijt ons, er is momenteel geen connectie met de server.";
// echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
// echo "upload is gelukt";
}
// upload het bestand
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "Er is iets fout gegaan, excuses voor het ongemak";
} else {
// insert data in de database
$sql = "INSERT INTO $wpdb->wpex_programma (id, naam) VALUES (2, 'Country Kickin 2')";
$prepare_query = $wpdb->prepare($sql);
$result = $wpdb->sql($prepare_query);
echo '<pre>';
print_r($result);
// weergeef het bestand & download
$contents = ftp_nlist($conn_id, $destination_folder);
// doe dit aan als er ergens een error is: var_dump(error_get_last($contents));
foreach ($contents as $mp3_url) {
$filename = basename($mp3_url, ".mp3");
// dit zorgt ervoor dat de punten niet te zien zijn.
if($filename == "..") {
continue;
print_r($filename);
}
if($filename == ".") {
continue;
print_r($filename);
}
?>
Also, the $result variable returns NULL when var_dump(error_get_last($result));
EDIT:
I changed:
else {
// insert data in de database
$sql = "INSERT INTO $wpdb->wpex_programma (id, naam) VALUES (2, 'Country Kickin 2')";
$prepare_query = $wpdb->prepare($sql);
$result = $wpdb->sql($prepare_query);
echo '<pre>';
print_r($result);
to:
else {
// insert data in de database
global $wpdb
$number_of_rows_inserted = $wpdb->insert('wpex_programma', [
'naam' => 'Country kickin 2'
]);
var_dump($number_of_rows_inserted);
print_r($result);
replace
$sql = "INSERT INTO $wpdb->wpex_programma (id, naam) VALUES (2, 'Country Kickin 2')";
$prepare_query = $wpdb->prepare($sql);
$result = $wpdb->sql($prepare_query);
with
$number_of_rows_inserted = $wpdb->insert('wpex_programma', [
'id' => 2,
'naam' => 'Country kickin 2'
]);
var_dump($number_of_rows_inserted);
The insert
method is doing the prepare so y ou don't need it.
Add global $wpdb;
at the start of your function or file if it's not done. Also, try to remove the line 'id' => 2
as the id should be set to auto increment.