MySQL query doesn't work while using wpdb->prepare - php

I need to insert into two tables at the same time so I'm using BEGIN and COMMIT in the query using $wpdb->query() and $wpdb->prepare() but unfortunately it doesn't work
Testing by [ die( $wpdb->last_error ); ],
Generates [ WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO TABLE_NAME ( name ) VALUES ( 'xxxxxx' ); at line 2] ]
P.S. I copied the printed query and paste it in mySQL Query window and IT WORKS!!!
$wpdb->query( $wpdb->prepare(
"BEGIN;
INSERT INTO {$tbl_accounts} ( name )
VALUES ( %s );
SELECT LAST_INSERT_ID() INTO #last_seller_id;
INSERT INTO {$tbl_apps} ( seller_id, slug, name, token )
VALUES ( #last_seller_id, %s, %s, %s );
COMMIT;",
$seller_name,
$app_name_sanitized,
$app_name,
$token_key )
);

Related

Cannot access newly created wordpress table

I have created a new table in the wordpress database via PhpMyAdmin.
However I have not be able to write into any of the tables that I have created. Writing in the predefined tables such as metapost works just fine with the same code. When I write out the error it seems that wordpress believes that I "forgotten" to enter the table name, like wordpress is not aware that this new table exists. What is going on?
The name of my table is hest
Here is my code:
<?php
$metakey = 'THAT IS';
$metavalue = 'DONE.';
$wpdb->query(
$wpdb->prepare(
"
INSERT INTO $wpdb->hest
( meta_key, meta_value )
VALUES ( %s, %s)
",
$metakey,
$metavalue
)
);
$wpdb->show_errors();
echo $wpdb->last_query;
?>
Here is the show_error message:
As you can see the table name is suddenly gone in the executed query.
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( meta_key, meta_value ) VALUES ( 'THAT IS', 'DONE.')' at line 2]
INSERT INTO ( meta_key, meta_value ) VALUES ( 'THAT IS', 'DONE.')
Thanks a bunch!!
Cheers Chris.
Looking at the documentation it seems that only the default tables are available as properties on the wpdb class. You would need to reference your new table as a string.
$wpdb->query(
$wpdb->prepare(
"
INSERT INTO hest
( meta_key, meta_value )
VALUES ( %s, %s)
",
$metakey,
$metavalue
)
);
You could alternatively use the insert function to insert a row to your new table.
$wpdb->insert(
'hest',
array(
'meta_key' => $metakey,
'meta_value' => $metavalue,
),
array(
'%s',
'%s',
)
);

Unknown SQL syntax error PHP PDO [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I have this code:
try {
$sql = "INSERT INTO order(
user_id,
departure_street,
departure_housenr,
departure_postalcode,
departure_city,
arrival_street,
arrival_housenr,
arrival_postalcode,
arrival_city,
order_date,
lifter,
order_status
)
VALUES(
:user_id,
:departure_street,
:departure_housenr,
:departure_postalcode,
:departure_city,
:arrival_street,
:arrival_housenr,
:arrival_postalcode,
:arrival_city,
:order_date,
:lifter,
:order_status
)";
$stmt = $dbh -> get_instance() -> prepare( $sql );
$stmt -> execute( array( ':user_id' => $_SESSION[ 'user_id' ],
':departure_street' => $street1_parsed,
':departure_housenr' => $streetnumber1,
':departure_postalcode' => $postcode1,
':departure_city' => $city1_parsed,
':arrival_street' => $street2_parsed,
':arrival_housenr' => $streetnumber2,
':arrival_postalcode' => $postcode2,
':arrival_city' => $city1_parsed,
':order_date' => $datetime,
':lifter' => $lifter,
':order_status' => $order_status ) );
}
catch( PDOException $e ) {
echo $e -> getMessage() . '<br />' . $sql;
}
This code keeps giving me this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'order(
user_id,
departure_street, ' at line 1
I don't understand what can be wrong with the syntax. I've used this query so many times and it always works. What's the problem now? I've tried to echo the $sql variable to see what the resulting query looks like, but it doesn't show me the values that are being inserted. Can anyone see the SQL syntax error?
ORDER is mysql key word .it should be enclosed by backticks .if your table name or column name is key word just use backticks order backticks ``
here you can find the mysql keywords list

(#1064) - You have an error in your SQL syntax

I get an error when i import a .sql file. How do i fix it?
Query:
insert into authorized_users values (sha1('".$name."'), sha1('".$password."');
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Here is the code:
create table authorized_users ( name varchar(20),
password varchar(40),
primary key (name)
);
insert into authorized_users values ( 'username',
'password' );
insert into authorized_users values ( 'testuser',
sha1('password') );
insert into authorized_users values (sha1('".$name."'), sha1('".$password."');
You need to edit your code and end the line with a closing parenthesis )
insert into authorized_users values (sha1('".$name."'), sha1('".$password."'));
You have missed ) here, so change
insert into authorized_users values (sha1('".$name."'), sha1('".$password."');
to
insert into authorized_users values (sha1('".$name."'), sha1('".$password."'));
I always prefer to enclose MySql queries inside double-quotes so I can use the variables directly without open and closing tags.
Answering to your question:
You missed the last parenthesis )
$myQuery = "insert into authorized_users values (sha1('$name'), sha1('$password'));";

php -> MYSQL query can't figure out what is wrong getting error #1064

I am trying to insert some values in the table the query is below:
Insert into
auditlog (
event,
desc,
userid,
useripaddress,
audittype
)
VALUES (
'User Authenticated',
'Useradminsuccessfully logged in to the system',
'1',
'127.0.0.1','1'
)
It gives me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,userid,useripaddress,audittype)VALUES ('User Authenticated', 'User admin su' at line 1
However when i run the insert using PHPMYAdmin it does insert a value and the query generated is
INSERT INTO
`auditlog`(
`event`,
`desc`,
`userid`,
`useripaddress`,
`audittype`)
VALUES (
'User Authenticated',
'Useradminsuccessfully logged in to the system',
'1',
'127.0.0.1','1'
)
The only difference i see is the quotes which i dont believe are needed. I don't understand where am i going wrong and am breaking my head now :):)
The backticks are needed around desc because it is a reserved word.
INSERT INTO auditlog (event, `desc`, userid, useripaddress, audittype)
VALUES (
'User Authenticated',
'Useradminsuccessfully logged in to the system',
'1',
'127.0.0.1',
'1'
)
There is also no harm in adding backticks around the other column names if you aren't sure whether or not they are reserved words.
Here is a list of words that are reserved and needs to be backticked: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Getting mysql syntax error and cant find source

I have function that updates log table.
function wslog($userID, $log, $where) {
safe_query("INSERT INTO ".PREFIX."log ( time, userID, log, where ) values( '".time()."', '".$userID."', '".$log."', '".$where."' ) ");
}
And I have this php code:
wslog($userID, 'server|'.mysql_insert_id().'', 'servers');
But I keep getting syntax error:
Query failed: errorno=1064
error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where ) values( '1269208030', '1', 'server|14', 'servers' )' at line 1
query=INSERT INTO ws_DII_log ( time, userID, log, where ) values( '1269208030', '1', 'server|14', 'servers' )
Is it possible that SQL doesn't like your log field name as it is a reserved word?
If so, try putting it is backticks
log ( `time`, `userID`, `log`, `where` )

Categories