Skip Navigation

[Resolved] cred_save_data : How to write in the database for non std tables

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.45 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#615517

Pat

Hello,
I have a Cred user form that creates new users and allow them a specific role. In the same time, I need to add some information to a specific table (different from wp_user or wp_usermeta)
Prior to create the cred_save_data into the function.php, I'm trying to test the code by creating a specific page and call it directly. So, I created the wpupdate.php page in the directory root and can lunch it thnaks to hidden link

Now, here is the code I have inserted in this page :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Titre</title>
</head>
<body>
<script>
global $wpdb;
$wpdb->update('wp_special_role_user', array('cf_1'=>391132800, 'cf_2'=>'Monsieur', array('wpuser_id' => '9')));
</script>
</body>
</html>

My table 'wp_special_role_user' has this structure :

wpuser_id : int(10)
cf_1 : int(20) (unix date)
cf_2 : varchar(255) utf8_general_ci

There are some other fields in this table, but they are not important for this function.

I'm getting no error in the console with this code, but the table has not been updated. Did I miss something?

Regards
Pat

#615577

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Pat

You have wrapped your PHP code to insert content into a custom table using script tags. These are used in HTML to include JavaScript.

You switch between HTML and PHP in a PHP template using <?php to start a PHP block and ?> to end it.

See hidden link

#615580

Pat

Hi Nigel,

Thanks for your feedback.
I had also tryed this

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Titre</title>
</head>
<body>

<?php
global $wpdb;
$wpdb->update('wp_special_role_user', array('cf_1'=>391132800, 'cf_2'=>'Monsieur'), array('wpuser_id' => 9),array( '%d','%s'), array( '%d' ) );
?>

</body>
</html>

and got this error :
Fatal error: Call to a member function update() on null in /home/my_site/wpupdate.php on line 15 (line 15 is the line with the update function)

Any idea?
Regards
Pat

#615628

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Pat

Your test page isn't loading WordPress.

It is just a PHP file at a URL that happens to be in the directory where you have WordPress installed, but you accessing that file directly and WordPress is not invoked.

Try using the plugin Code Snippets. You can add PHP snippets to it and it recently added the ability to run some code just once, for cases like this where you need to update something in the database on a one time basis.

#615631

Pat

Hi Nigel,

I understand. In fact, I dont need to use a page as the aim is to test a function before placing it into a cred_save_data
What would be the best way to manage this test without managing a full page for this?

Regards
Pat

#615638

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Use Code Snippets and run your code one time only, to test the function so that you can inspect the database and see whether it was updated as you expect.

https://wordpress.org/plugins/code-snippets/