Excecuting mysql querys in wordpress - php

im using the insert php code plugin in wordpress and im trying to do inserts and mysql querys, but there's an error that appears me:
Error: INSERT INTO porschec_clientes.clientes(
ID,
NAME,
LAST_NAME,
EMAIL,
PHONE,
PORSCHE,
REFERENCE,
STATUS
CODE,
)
VALUES (NULL,’name’,’last_name’,’email#gmail.com’,’123123′,’911′,’name’, 0, ‘cdcc34cd554621097f9a6fdc3b2cc728′)
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 ‘CODE,
)
look that in "CODE" there's this quote symbol " ‘ " (i dont know if the correct translation is quote but... whatever haha) but in my php code i have it this way
VALUES (NULL,'name','last_name','email#gmail.com','123123′,'911′,'name', 0, 'cdcc34cd554621097f9a6fdc3b2cc728')";
wordpress is changing the quote symbol when i update the page, there's any way that i can avoid this? thank you

You missed a ,after STATUS, that's what the error tells you. Usually it shows the part after the error.
REFERENCE,
STATUS, <--
CODE,
Beside that let it change the quotes. That's all right.

Related

Why $wpdb->insert() makes sql syntax error?

I am using wpdb->insert to insert a row to the table,
like this
$wpdb->insert($table_name, array("title"=>"foo"));
$wpdb->last_query returns the below query
INSERT INTO `some_table` (`title`) VALUES ('foo')
But i get a error in sql syntax
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 '1' at line 1
The error is little confusing, i passed a string 'foo' in to insert statement, but somehow it is showing '1' in the error.
This error occurs when i am running the $wpdb->insert inside phpunit, i copied the query and ran it on my test wordpress database and this query works correctly in the different method in the same test suite, it works fine.Any idea how to fix this error?
Please try this code. The recommended way (as noted in codex):
$wpdb->insert($table_name,array('title'=>'foo'),array('%s'));

PHP Error with mySQLi

I'm learning PHP and MySQL and made a small website to help me learn. While I was implementing MySQLi, I kept getting an error with the code. I tried various things like trying to query all the tables contents but I get 0 rows even though I have a few already there. I'm using WAMP Server 2.5 and The connection is successful but anything else seems to not work.
When I use the code:
$sql = "USE mydb
INSERT INTO persons (Name, User, Pass)
VALUES ('John', 'Doe', 'johnexample');";
if ($conn->query($sql) === TRUE){
echo "Success";
}else{
echo "Error, Please Try Again Later<br>".$conn->error;
}
$conn->close();
I'm surprised this doesnt work because it's almost the exact same as the example on the W3 Schools webiste. I've seen other posts with this error but their solutions dont work. The error I get is:
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
persons (Name, User, Pass) VALUES ('John', 'Doe', 'johnexample')' at line 2
Any help is appreciated! Thank You!
The issue is mysqli::query doesn't support multiple queries, and even if it did, it would require the ; terminator. This is causing your SQL syntax error.
If you want to change the current database for the connection, you can use:
$conn->select_db('mydb');
Then you can do your INSERT query without the USE part, which will be applied to mydb.
Be aware that the fieldnames are case-sensitive! please check if Name, User, Pass are really start with a capital.

Stumped on why MySQL won't accept my query?

Okay, so I'm currently using mysqli_real_escape_string to escape my SQL queries before sending them to MySQL via PHP. Yet, for some reason my queries aren't processing, and when I outputted the MySQL query and pasted it in to PHPMyAdmin, it gave 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 'WHERE ind={A$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQg' at line 1
Now, the following is my query:
INSERT INTO `db`.table(`colheader`) VALUES ('{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}') WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'
Now, I know that the string assigned to 'ind' has some issues, but I tried putting a slash before every period and every dollar sign and it still doesn't work. I tried putting the whole thing in double quotes, even brackets. Nothing. Could anyone point out what I'm clearly missing? I've looked at the documentation and can't seem to find anything. Thank you in advance!!
WHERE serves to filter which records will be affected or retrieved by your query, and INSERT servers to append a whole new record to a table.
An INSERT can never affect existing records, therefore its nonsense to have a WHERE clause. INSERT does not support WHERE.
If you are trying to edit the value of a field on an existing record, use UPDATE instead.
Take a look at the MySQL Reference Manual for details about its usage.
if your trying to make an update to the specified index use
UPDATE `db`.table SET `colheader` = '{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}' WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'

PHP/AJAX issue inserting to mysql database

I'm having a problem inserting some data to a mysql database. I have used the same method with other features on the site, and this is the only one causing problems. It's meant to input into 3 field in the database (To, From, Message). As you can see it's a very basic messaging system.
I have the data coming into PHP via AJAX. But the problem is within the INSERT. I have messed around with it for over an hour now - no luck! Here is the code to insert:
mysql_query("INSERT INTO messages (To, From, Message) VALUES('$to','$loggedin','$message') ")
or die(mysql_error());
And here is the SQL syntax 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 'To, From,
Message) VALUES('Ryan','Ryan','hhh')'
at line 1
I have tried adjusting a lot of things, no luck! :(
"TO" and "FROM" are reserved keywords, it's not wise to use them as column names. You have to escape them with a back-tick "`". Try this:
INSERT INTO messages (`To`, `From`, `Message`)
See the list with reserved words: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

php, MySQL and variables containing å, ä and ö escape problem

I'm having some problem with my SQL syntax/escaping variables on my LAMP server.
The command I want to use is the following:
$sql=mysql_query("INSERT INTO '$table' (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, Price, Retailer, Checksum)
VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[startdate]','$_POST[timestroke]','$duedate','$_POST[duration]','$price','$_SESSION[name]','$random')");
The problem is that sometimes the $table variable contains characters like å, ä and ö.
Hence I need to put ' ' around $table to make sure it stays the same. However when doing that recieve the error:
"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 ''tablename' (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, P' at line 1".
Looks like the escaping by ' ' creates a problem.
I've tried with replacing the query with a mysql_real_escape_string:
"$sql=sprintf("INSERT INTO '".mysql_real_escape_string($table)."' (FirstName, [...]"
but that doesnt help me either.
Is there a way to keep the data in the variable intact and still be able to run the query? Or do I have to accept that å,ä,ö is banned from php/MySQL?
This is do to with character-encoding. Check out http://www.sitepoint.com/blogs/2006/03/15/do-you-know-your-character-encodings/
Put header('Content-Type: text/html; charset=utf-8'); at the top of your page
Also try doing mysql_set_charset('utf8'); before insert/reading from DB. Then you should put the following on your form that's posting to your PHP file:
<form action="/your-post-controller.php" method="post" accept-charset="utf-8">
Notice the accept-charset="utf-8 -- this is extremely important otherwise your header will report to the PHP file its in latin1
It should work then.
Also take a look at http://www.phpwact.org/php/i18n/charsets -- was trying to find the link, definitely worth a read for anyone interested in getting character encoding right, see the Iñtërnâtiônàlizætiøn string for testing your PHP&MySQL table
You would use backticks (`) to surround the table name. Nothing to do with Character-encoding:
$sql=mysql_query("INSERT INTO `$table` (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, Price, Retailer, Checksum)
VALUES ('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['startdate']}','{$_POST['timestroke']}','$duedate','{$_POST['duration']}','$price','{$_SESSION['name']}','$random')");
A couple of side notes here:
A: you should really user mysql_real_escape_string on any input coming in from an unknown source to avoid someone destroying your database with SQL Injection.
B: You should use ' around array associative indexes, reason being is that these all would throw notice undefined constant (or something to that matter) errors. Which will fill up your error log and make finding more critical errors a bit harder if you ever need to go back.

Categories