using wpdb to insert into SQL SERVER database with PhP - php

I am attempting to convert a php form script I created from an external MySql database to an external SQL server database. I have properly copied the database over and just seem to be having a problem getting the form to use the WPDB object to use insert into the new SQL server database. The same creation script was used for both databases.
The insert statement:
try {
$externalDB->insert('EmployeeApp.dbo.applicant', array(
'first_name' => $first_name,
'last_name' => $last_name,
'middle_name' => $middle_name,
'suffix' => $suffix,
'nick_name' => $nick_name,
'email' => $email,
'idnumber' => $idnumber,
'street_address' => $street_address,
'city' => $city,
'state' => $state,
'zip' => $zip,
'phone' => $phone,
'ged' => $ged,
'over_18' => $over_18,
'previous_app' => $previous_app,
'previous_app_date' =>$previous_app_date,
'previous_emp' => $previous_emp,
'previous_emp_date' => $previous_emp_date,
'us_citizen' => $us_citizen,
'visa' => $visa,
'us_eligible' => $us_eligible,
'date_avail' => $date_avail,
'name_change' => $name_change,
'name_change_info' => $name_change_info,
'discharged' => $discharged,
'military' => $military,
'military_info' => $military_info,
'high_school_name' => $high_school_name,
'high_school_city' => $high_school_city,
'high_school_state' => $high_school_state,
'highest_grade_completed' => $highest_grade_completed,
'additional_skills' => $additional_skills,
'convicted'=> $convicted,
'convicted_name' => $convicted_name,
'convicted_date' => $convicted_date,
'convicted_charge' => $convicted_charge,
'convicted_sentence' => $convicted_sentence,
));
$lastid = $externalDB->insert_id;
The database connection object:
try{
$externalDB = new wpdb(EMP_DB_USER,EMP_DB_PASS, EMP_DB_NAME, EMP_DB_HOST);
}catch(Exception $e){
echo $e->getmessage();
}
$wpdb has been declared as a global at the top of the script and this was working perfectly before I connected to a SQL server database.
I am wondering if I need to not use $externalDB->insert() with SQL or try a different table call such as just 'dbo.applicant' or just 'applicant'. Not sure about the transition and can’t find a lot of documentation on it.

If you look at the code, you will see that the wpdb class connects only to MySQL databases. This is an acknowledged limitation of Wordpress, and although there have been some attempts by third parties to rectify the situation, nothing official has happened yet!

Related

return insert id on none laravel db set up

I have a database that was not designed for Laravel. I am building a small api using Laravel. Thus I can not useLlaravel structured queries. The query is working but need to be able to retrieve the id number of an insert.
This is the insert I am using.
$results = DB::insert("
SELECT * FROM insrdata.add_user(
:assocID,
:accountID,
:firstName,
:middleName,
:lastName,
:address1,
:address2,
:city,
:state,
:zipCode,
:phone,
:email,
:birthDateMMDDYYYY,
:gradeLevelID,
:commentText
)
",
[
'assocID' => $request->input('assocID'),
'accountID' => $request->input('accountID'),
'firstName' => $request->input('firstName'),
'middleName' => $request->input('middleName'),
'lastName' => $request->input('lastName'),
'address1' => $request->input('address1'),
'address2' => $request->input('address2'),
'city' => $request->input('city'),
'state' => $request->input('state'),
'zipCode' => $request->input(''),
'phone' => $request->input(''),
'email' => $request->input('email'),
'birthDateMMDDYYYY' => $request->input('birthDateMMDDYYYY'),
'gradeLevelID' => $request->input('gradeLevelID'),
'commentText' => $request->input('commentText')
]
);
if($results) {
$return_array = array(
'p_contact_id' => "",
'p_result' => "O.K."
);
}
I need to get the id of the insert.
I think if the insert is successful, you can query for it by accountID (which i suppose unique for each inssured person) and get its id.
This is a little complex without using the Laravel structured queries, as you said. If you can tweak your query a little, and make sure you validate the input data ahead of your insert to prevent SQL injection, you should be able to use insertGetId. You'll have to do some work, but something along the lines of:
$id = DB::table('whateverYourTableNameIs')->insertGetId([
'assocID' => $request->input('assocID'),
'yourSelectField' => DB::raw("yourRawSelect"),
// etc.
]);
This is called "getting last inserted id". Here documentation in Laravel about it.
https://laravelcode.com/post/laravel-55-get-last-inserted-id-with-example

Creating and using a multi-dimensional array from MySQL

Here's the line I'm having issues with:
array('jsonrpc' => '2.0', 'id' => 1, 'method' => 'getExchangeAmount', 'params' => array(array('from' => 'BTC', 'to' => 'LTC', 'amount' => '1'),array('from' => 'BTC', 'to' => 'ETH', 'amount' => '1')))
This is the specific part of the line I'm having issues with:
array(array('from' => 'BTC', 'to' => 'LTC', 'amount' => '1'),array('from' => 'BTC', 'to' => 'ETH', 'amount' => '1'))
Basically I'm using this script to compare prices of cryptocurrencies. The problem is that right now I'm having to enter all of them into the script manually.
However, I have a database table (trade_stats) that I'd prefer to get the details from.
Here's its layout:
id incoming outgoing
1 BTC ETH
2 BCH LTC
3 ETH BCH
What I'm wanting to do is pull the rows from my database and use them in the script, but I can't seem to figure out how to create a multi-dimensional array.
Any help or pointers in the right direction would be great.
I hope this helps you
<?php
$conn=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if (!$conn) {
die("Error Connecting To Database: ".mysqli_connect_error()."<br/>");
}
$sql="
SELECT * FROM trade_stats
";
$result=mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
while($row=mysqli_fetch_assoc($result)) {
$from=$row['incoming'];
$to=$row['outgoing'];
//$amount=$row['???'];
$array=array();
array_push($array, array('from' => $from, 'to' => $to, 'amount' => '1'));
print_r($array);
}
}
?>
I didn't set a value for mysqli connect so you can edit it yourself and suit it for your database.
+I don't know which column is for ((amount)) so I set 1 value for it.You can edit it yourself.
If you had any questions or if the code is not working as you wanted tell me I will edit it :)

How to update many fields at once in mongoDB PHP? [duplicate]

The following code should work. I could have missed something, but right now I have it as 2 separate update statements and have decided to ask here why this line isn't working.
$this->db->settings->update(array('_id' => $mongoID),
array(
'$set' => array('about' => $about),
'$set' => array('avatar' => $avatar)
)
);
Did I miss something when reading guides or is it only possible to do with separate update statements?
The third argument to MongoCollection::update is an array of options for the update operation.
$this->db->settings->update(
array('_id' => $mongoID),
array('$set' => array('about' => $about, 'avatar' => $avatar))
);

Set multiple fields with one update query

The following code should work. I could have missed something, but right now I have it as 2 separate update statements and have decided to ask here why this line isn't working.
$this->db->settings->update(array('_id' => $mongoID),
array(
'$set' => array('about' => $about),
'$set' => array('avatar' => $avatar)
)
);
Did I miss something when reading guides or is it only possible to do with separate update statements?
The third argument to MongoCollection::update is an array of options for the update operation.
$this->db->settings->update(
array('_id' => $mongoID),
array('$set' => array('about' => $about, 'avatar' => $avatar))
);

Codeigniter adding data to database and then process it

I am using codeigniter to built a type of online shop.
I want to create a process order function in order to verify the details of the order the clients puts in.
I am stuck though because on the last page i have the data to submit and when i click i go to main/process_order where i insert the data in the table and then use curl to comunicate with another server.
My question is: when i hit submit and then stop on the process_order page if i reload it 1000 times, the table will be filled with the same 1000 lines, so this can be a security issue. Also if i make a function to add the data to db and then redirect to process_order it will be another issue because i still need my data that was posted.
What's the best way to solve this. I hope i made it as clear as i can.
Code:
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
$this->db->insert('orders_temp', $data);
Maybe something like this can help. Do a check on the userid and the time before you insert the data. You would need make sure to do a search on database fields that are unique (unless the time field is the full time)
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
if (<!--CONDITIONAL STATEMENT-->)
{
$this->db->insert('orders_temp', $data);
}

Categories