0

I have table named user with this structure

userid (PrimaryKey) | Names | Email | Phone# |

Then i have a table 2 named investment with this structure

invid (PrimaryKey)(AI) | userid (ForeignKey) | capital | commission | date | upliner | status defaul(0)

I need when i insert a record in table 2 the foreign key should be linked with table 1, This below is INSERT code i'm using

$output = $wpdb->insert ( 'investment', array( 'capital' => $fields['capital'], 'commission' => $fields['commission'], 'date' => $fields['date'], 'upliner' => $fields['upliner'], ));

Code to retrieve logged userid

global $current_user;      
get_currentuserinfo();    
$value1 = $current_user->ID;

$value1 will be holding ID number of logged in user because it is the foreign key in investment table.

So basically i want the data to be inserted where the current logged in userid $value1=userid

OKTV Plus
  • 9
  • 2
  • INSERT statements do not have a WHERE clause. They create _new_ records, a WHERE clause would make no sense at all in that context. If you want to update existing records - then you need to use an UPDATE statement. – CBroe Jan 20 '23 at 13:14
  • https://developer.wordpress.org/reference/classes/wpdb/update/ – CBroe Jan 20 '23 at 13:15
  • @CBroe so if i want to insert a record with with foreign key how can i implement it with out where clause??? – OKTV Plus Jan 20 '23 at 13:29
  • What do foreign keys have to do with this? If you want to refer to a _different_ record elsewhere, in your new record that you are inserting - well then you simply explicitly _specify_ the foreign key value in your insert statement ...? – CBroe Jan 20 '23 at 13:32
  • That's what i'm trying to find out how to specify the foreign key value in my above insert statement, How should the query look a like? – OKTV Plus Jan 20 '23 at 13:42
  • How should we know, when you just show us a mere statement, and give no explanation whatsoever about the actual data model? Which of those columns is supposed to refer to another record then? – CBroe Jan 20 '23 at 13:50
  • Please [edit] your question to show us the definition of your table. You can use `SHOW CREATE TABLE tablename` to get it. – O. Jones Jan 20 '23 at 13:51
  • I'd assume you'd just pass the `userid` into the array that you are inserting into and give it the value of `$current_user->ID`. https://3v4l.org/cO1NfF. Also, `get_currentuserinfo` is deprecated, you are encouraged to use `wp_get_current_user` instead. – Chris Haas Jan 20 '23 at 14:00
  • @CBroe i edited the question try to look at it again, The column supposed to refer to each other is userid in table 1 and userid in table 2. – OKTV Plus Jan 20 '23 at 14:07
  • @O.Jones i edited the question please revise it. – OKTV Plus Jan 20 '23 at 14:07
  • Thanks @ChrisHaas let me try to run the codes – OKTV Plus Jan 20 '23 at 14:08
  • So all you have to do is get the ID of the new `user` table record (see duplicate), and then explicitly use that value in your following `investment` insert statement. – CBroe Jan 20 '23 at 14:11
  • @Cbroe Can you give a sample query? – OKTV Plus Jan 20 '23 at 14:31
  • What do you still need an "example" for at this point? You already _know_ how to make an insert using dynamic values. – CBroe Jan 20 '23 at 14:35

0 Answers0