I am trying to do a batch of insert to mysql. I got this code to work without the inner select question. How do I do a bulk insert in mySQL using node.js But when i add the select i get "ER_PARSE_ERROR".
var arr = [[1, '123456789', '2000-01-01', 1],[2, '123456789', '2000-01-02', 1],[3, '123456789', '2000-01-03', 1]];
objConn.query("INSERT INTO stats (`name`, `phone`, `dayOfCall` , `calls`) \
VALUES ((SELECT `name` as myname FROM `contact` WHERE `id` = ? \
LIMIT 1), ?, ?, ?)",
[arr],
function (err, row, fields){
if(err)
logger.info(err);
}
);
Related
I have a MySQL query that filters all emails from a csv file and combine multiple records before inserting them to a custom datatable in wordpress. This works:
$cntSQL = "SELECT count(*) as count FROM {$tablename} WHERE email='".$email."'";
but i need a multiple condition like following but this is not working
$cntSQL = "SELECT count(*) as count FROM {$tablename} WHERE email='".$email."' AND phone='".$phone."'";
I also tried this but not working.
$cntSQL = "SELECT count(*) as count FROM {$tablename} WHERE {email='".$email."'} AND {phone='".$phone."'}";
Debugging
[09-Oct-2020 18:49:07 UTC] WordPress database error for query INSERT INTO `wp_lubuvna_subscribers` (`first_name`, `last_name`, `email`, `phone`, `birthday`, `gender`, `customer_type`, `id_company_number`, `street_address`, `address_line_2`, `city`, `state_area`, `zip`, `customer_from`, `groups`, `last_visit`, `send_sms`, `send_email`, `join_date`, `post_author`) VALUES ('Joshua', 'Hodges', 'guj#wote.bj', '(603) 280-6605', '04/25/1953', 'Male', '499242365', '79808607399', 'Jomuz Street', 'Alser Grove', 'Custipeva', 'MO', '41789', 'tada', 'pacseztoj', '08/20/1988', 'TRUE', 'TRUE', '01/10/1920', 1) made by do_action('wp_ajax_new_subscriber_batch'), WP_Hook->do_action, WP_Hook->apply_filters, maybe_insert_new_subscriber_batch_database_table
the results i am getting from follwoing:
$record = $wpdb->get_results($cntSQL, OBJECT);
results:
Array ( [0] => stdClass Object ( [count] => 0 ) )
Based on your comment you need an OR operator not AND operator.
$cntSQL = "SELECT count(*) as count FROM {$tablename} WHERE email='".$email."' OR phone='".$phone."'";
I'm trying to insert data into MySQL via Express, the database table is created as such:
create table foods (name VARCHAR(100), typval DECIMAL(8, 2), unit VARCHAR(50), calories DECIMAL(5, 2), carbs DECIMAL(5, 2), fat DECIMAL(5,2), protein DECIMAL(5, 2), salt DECIMAL(5, 2), sugar DECIMAL(5, 2));
I have a form which collects user data and the function to post looks something like this:
app.post("/addnewfood", function (req,res) {
console.log(req.body.name, req.body.typval, req.body.unit, req.body.calories, req.body.carbs, req.body.fat, req.body.protein, req.body.salt, req.body.sugar);
// saving data in database
let sqlquery = "INSERT INTO foods (name, typval, unit, calories, carbs, fat, protein, salt, sugar) VALUES (?,?)";
// execute sql query
let newrecord = [req.body.name, req.body.typval, req.body.unit, req.body.calories, req.body.carbs, req.body.fat, req.body.protein, req.body.salt, req.body.sugar];
db.query(sqlquery, newrecord, (err, result) => {
if (err) {
return console.error(err.message);
}else
res.send("New" + " " + req.body.name + " was added to the database.");
});
});
I'm not sure where I'm going wrong since I counted there's 9 different fields I need to fill in for the 9 different columns. I've checked the commas and I can't see anything out of place.
When I try to enter data like: "McDonalds Hamburger, 1.0, Burger, 100.00, 10.00, 10.00, 10.00, 1.0, 10.00"
The console.log prints it out fine and it should work however, I get:
ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
Help?
Your INSERT query needs a placeholder for every column you insert. You only have two placeholders -- two ? items -- here.
VALUES (?,?)
You need nine, one for each column.
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
Trying to create a new WordPress user directly through MySQL by running a query. I keep seeing an error message popup :
1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'user_registered' at row 1
How can include what is missing?
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('adminuser2', MD5('adminuser2password'), 'adminfirstname adminlastname', 'adminuser2#mywebsite.com', '0');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
Follow this syntax, it's the recommended way to insert data while you're using WordPress
$wpdb->insert( $table_name, array('column_name_1'=>'hello', 'other'=> 123), array( '%s', '%d' ) );
define a variable:
$blogtime = current_time( 'mysql' ); then add 'user_registered' column to your query
$wpdb->insert( 'wp_users' , array(
'user_login' => 'adminuser2',
'user_pass' => MD5('adminuser2password'),
'user_nicename'=> 'adminfirstname adminlastname',
'user_email'=> 'adminuser2#mywebsite.com',
'user_status'=> '0',
'user_registered'=> '$blogtime'
));
My rails code is something simple like (code and sql output changed to make it generic)
Router.new(
:name => params[:data][:name],
:subid => params[:data][:subid],
:info => params[:data][:info],
:parent => #parent)
Rails generated INSERT sql
INSERT INTO `mydb` (`name`, `subid`, `info`, `parent_id`, `created_at`, `updated_at`)
VALUES (?, ?, ?, ?, ?, ?) [["name", "myname"], ["subid", ""], ["info", ""], ["parent_id", 2], ["created_at", "2016-09-09 06:06:37"], ["updated_at", "2016-09-09 06:06:37"]]
This is the error I am getting:
Mysql::Error: Incorrect integer value: '' for column 'parent_id' at row 1: INSERT INTO mydb (name, subid, info, parent_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)
Looks like mysql doesn't like this type of syntax. I have version 5.6.32.
The values look work when I modify the command to the values directly into "VALUES". Is there a way to go back to the format where the values are inserted at "VALUES" or get the insert working without having to recreate the INSERT statement.
EDITED: I think it is not the actual the SQL. Is there a way to print the actual SQL?
I'm running the following query:
INSERT INTO deal (`site_id`, `status`, `slug`, `title`, `description`, `condition`, `sale_price`, `shipping`, `deal_url`, `buy_url`, `start`, `added`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE `site_id` = ?,`status` = ?,`slug` = ?,`title` = ?,`description` = ?,`condition` = ?,`sale_price` = ?,`shipping` = ?,`deal_url` = ?,`buy_url` = ?,`start` = ?
Built by this code:
$this->db->query('INSERT INTO
deal
(`'.implode('`, `', array_keys($insert)).'`, `added`)
VALUES
('.implode(', ', array_fill(0, count($insertValues), '?')).', NOW())
ON DUPLICATE KEY UPDATE
'.implode(',', array_keys($update)), array_merge($insertValues, array_values($update)));
The issue with this is that my query isn't properly escaping....
INSERT INTO deal (`site_id`, `status`, `slug`, `title`, `description`, `condition`, `sale_price`, `shipping`, `deal_url`, `buy_url`, `start`, `added`) VALUES ('2', 1, http://www.woot.com/sale/sony-dash-personal-internet-viewer-7, 'Sony Dash Personal Internet Viewer', 'Finally, someone made me my own, personal internet!', 'New', '69.99', $5 shipping, http://www.woot.com/sale/sony-dash-personal-internet-viewer-7, , 1305867600, NOW()) ON DUPLICATE KEY UPDATE `site_id` = '2',`status` = 1,`slug` = http://www.woot.com/sale/sony-dash-personal-internet-viewer-7,`title` = 'Sony Dash Personal Internet Viewer',`description` = 'Finally, someone made me my own, personal internet!',`condition` = 'New',`sale_price` = '69.99',`shipping` = $5 shipping,`deal_url` = http://www.woot.com/sale/sony-dash-personal-internet-viewer-7,`buy_url` = ,`start` = 1305867600
I have XSS filtering setup.... am I missing something?
Odd. What's the database definition? The only thing I can think of is that the DB layer is concluding that some of those columns don't need escaping.