To insert a user info to another wp site using $wpdb - database

I want to insert a user from another wordpress site. I used the bellow code for this. But i got a error.
Call to a member function query() on a non-object
My code for insert the user table
global $mydb;
$mydb = new wpdb(My DB information here...);
$rows = $mydb->get_results("select user_nicename from wp_users");
$sql = "INSERT INTO wp_users (user_login,user_pass,user_pass,user_nicename,user_email,user_url,display_name)
VALUES ('$fields[username]','$fields[password]','$fields[user_nicename]','$fields[user_email]','$fields[wpmem_reg_url]','$fields[wpmem_reg_url]','$fields[display_name]')";
$wpdb->query($sql);
Why cant i insert the data and get the error. Please help me.

You are declaring $mydb as global connection object and executing query with $wpdb
change it to
$sql = "INSERT INTO wp_users (user_login,user_pass,user_nicename,user_email,user_url,display_name)
VALUES ('$fields[username]','$fields[password]','$fields[user_nicename]','$fields[user_email]','$fields[wpmem_reg_url]','$fields[display_name]')";
$mydb->query($sql);

Related

Laravel relationship always returning empty array

I have two models in a Laravel application Ft and Fi.
Ft contains the following code:
protected $table = 'ft';
protected $connection = 'XXX';
protected $keyType = 'string';
public function lines()
{
return $this->hasMany('App\Fi', 'ftstamp', 'ftstamp');
}
Fi contains the following code:
protected $table = 'fi';
In my controller I have the following code
Ft::select($fields)->with(['lines'])
All results have "lines": []
Relationship exists in database.
Database is SQL Server
ft.ftstamp and fi.ftstamp field is type char.
Driver used for connection is sqlsrv.
I'm sure this is a duplicate, but I can't find it. If you only select certain fields from the table, ensure that the primary key is one of them. The eager load is done as a separate query, not a join, so the key values have to be available.
For example, this code:
User::select(["id", "name"])->with("posts")->get();
Runs two database queries:
SELECT id, name FROM users;
SELECT * FROM posts WHERE user_id IN (?, ?, ?, ?...);
The second query is populated with the id values from the first. Without those values, the second query can't be run.
In a normal database, this would mean making sure that the array $fields contains "id" as an element, and I'd provide a code sample. Your database looks frightening however, so I won't try.

Asp.NET MVC Inserting data into mssql database gives table object error

when a user register i want to insert some extra data into a other table AspNetUserStatics. But when the user register i got this error: System.Data.SqlClient.SqlException: The request for procedure 'AspNetUserStatics' failed because 'AspNetUserStatics' is a table object.
I tried multiple ways to add code data into my database with other stack overflow posts. But i can't find the solution. Maybe it is a simple stupid thing that wil solve this but i am new and need to learn things. So it will great if someone can help me.
connection();
SqlCommand com = new SqlCommand("AspNetUserStatics", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#Id", user.Id);
com.Parameters.AddWithValue("#Points", 5);
com.Parameters.AddWithValue("#Day", DateTime.Now.DayOfWeek);
com.Parameters.AddWithValue("#PointsNewUser", 1);
con.Open();
com.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index", "Home");
Here is the the full code: https://pastr.io/view/iPeGBL
Error: The request for procedure 'AspNetUserStatics' failed because 'AspNetUserStatics' is a table object.
Full Error Page: https://pastr.io/view/I59Q2a
I want all the data will be insert into the table AspNetUserStatics.
Regards,
Bart
For what I can see you are trying to send data to a stroed procedure, but "AspNetUserStatics" is a table. you should use an insert statement instead, something like
insert into AspNetUserStatics (Id, points, ....) values (#Id, #Points,....)

Laravel query-builder for insert data from a external db to another db

I tried to send data from external db table to internal db table with query-builder in Laravel 5.4v. could you tell me how to modify below code? thanks.
DB::connection('ext_db')->table('ext_customers')->chunk(1000, function ($All){
foreach ($All as $Data){
DB::connection('inn_db')->table('inn_customers')->insert(
[
column1 => $Data->columnToCopy,
etc..
]);
}};
I had to do the same thing recently. This is the code I used:
$currencies_ext = DB::connection('ext_db')->table('currencies')->get();
foreach ($currencies_ext as $currency_ext) {
$currency = new Currency;
// set the values here
$currency->save();
}
If the model you are inserting to is set to use the internal database there's no need to specify a connection for it.

arrays and mysqli insert

once again thanks for your help in advance im looking to pre fill a mysqli insert statement with data filled from an array and cannot seem to get it to work it just doesnt insert and im kinda stuck and looking for ideas
this is what im chasing as a end result as for code but cannot seem to get it to work
$table_colums= array("db_colum1","db_colum2");
$forms_data = array("form_data1","form_data2");
$sql = INSERT into some_table ($table_colums) VALUES ($forms_data)
obviously if i do it the old fashion way it will work but i need to have it get its values from the arrays because there dynamic and filled from a database
$sql = INSERT into some_table (db_colum1,db_colum2) VALUES ('form_data1','form_data2')
I can suggest you a library I wrote, SafeMysql, which is doing exactly what you want - adding arrays to mysql (among many other wonderful things).
Though you need another kind of array, but I suppose it would be even simpler:
include 'safemysql.class.php';
$db = new safemysql();
$data = array(
"db_colum1" => "form_data1",
"db_colum2" => "form_data2",
);
$db->query("INSERT INTO some_table SET ?u", $data);
Nothing could be easier.

How do you use delete, update, insert in joomla mvc from the file you create

I have been able to create simple MVC app to display the form and use information from the database. [ so sql I use SELECT ].
However, when I use 'update' or 'delete' the data in the database stay the same. I don't understand!?!
[ I use controller.php call model and pass id to match with the row in the table]
I check data that pass in there, and it did echo the value of id.
The page I load from the controller that call method in model has complete without the error (it could be that the syntax is right, but logic is wrong).
so i start to change sql statement to SELECT and it gives me the value that match with $id param'
I don't understand? any clue?
$query = JFactory::getDbo()->getQuery(true);
$db =& JFactory::getDBO();
$query = "DELETE FROM `menutables` WHERE `id2` = $id ";
$db->setQuery($query);
$db->query(); // this line I also take it out to test, it is fine but no data change either
Do you try to modify a Joomla database or a personal one?
Look at this pages :
http://docs.joomla.org/How_to_connect_to_an_external_database
http://docs.joomla.org/How_to_use_the_database_classes_in_your_script#Tips.2C_Tricks_.26_FAQ
try to use either
$query = "DELETE FROM #__menutables WHERE id2 = '$id' ";
or
$query = "DELETE FROM #__menutables WHERE id2 = '".$id."' ";
it'll work fine then.
Deleting a Record
delete query in Joomla 2.5.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// delete all custom keys for user 1001.
$conditions = array(
$db->quoteName('user_id') . '=1001',
$db->quoteName('profile_key') . '=\'custom.%\''
);
$query->delete($db->quoteName('#__user_profiles'));
$query->where($conditions);
$db->setQuery($query);

Resources