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.
Related
I need to insert a POINT value into my MySQL table using Fat Free Framework. I was hoping to do this using the F3 Mapper, but I got the impression that is not possible.
So I tried to use $db-exec()
This is my current code, based on various searches here and on google.
$geopoint = "POINT($lat $long)";
$db->exec("INSERT INTO event_dates ('eventGeoPoint') VALUES ($geopoint)");
This throws an error:
PDOStatement: 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 '52.8583742))' at line 1
I have also tried
$db->exec("INSERT INTO event_dates ('eventGeoPoint') VALUES (GeomFromText($geopoint))");
Please tell me how to correctly insert a POINT() value into my database using Fat Free Framework, either the mapper or exec
You need to correct your INSERT statement, which you are mixed with UPDATE statement.
INSERT INTO mytable SET myGeoPoint = 'GeomFromText($geopoint)
should be
INSERT INTO mytable(myGeoPoint) values (GeomFromText($geopoint))
Also, you need to use Prepared Statement to avoid SQL Injection.
Searching on after #Ravi's comments I found the answer in this post.
I changed my statement to
$result= $db->exec("INSERT INTO mytable (GeoPoint) VALUES (PointFromText(CONCAT('POINT(',$lat,' ',$long,')')))");
And it works!
Firstly, I know that this system shouldn't be using mysql_ functions anymore, but it does.
The system is connecting to two databases. Before running a query, it is selecting the correct database on which to run the query using mysql_select_db(). The correct database is 'database2'. The connection to the database itself works, and the mysql_select_db function is returning true/1.
The code works locally but does not work on the server...
Here is an example of a failing query...
global $DBtwo;
mysql_select_db('database2', $DBtwo);
$sql = "INSERT INTO table
(Column1, Column2, Column3)
VALUES
('Value1', 'Value2', 'Value3')";
$result = mysql_query($sql, $DBtwo);
die(mysql_error());
This script returns the following output...
Table 'database1.table' doesn't exist
The error suggests that the query is being executed on database1, not database2. However database2 is being successfully selected before the query is run.
As mentioned, this error only occurs on the server. Running locally the queries work and the correct databases are used.
Any suggestions or pointers would be very welcome. Cheers.
The comment from Marc B solved my problem so credit goes to him.
The mysql_error function was returning misleading info as it was not being passed a resource.
The mysql_select_db was actually working. It gave the impression of not working via a combination of not passing the correct resource to mysql_error. On top of that a mysql_insert_id() was not working for the same reason, leading the function overall to return false.
I now feel stupid, but far less annoyed. Many thanks!
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.
I've been inserting entries into a database using PHP for about a year now, then all of a sudden today they stop inserting. I can't work out why. Nothing has changed (as far as I know anyway!) Can anyone tell me what's wrong please:
include('db_functions.php');
connect_to_db();
$query="insert into mixtable (date, djname, title, description, music, tracklist, deleted) values ('".$date."','".$djname."','".$title."','".$description."','".$music."','".$tracklist."', 0)";
$result=mysql_query($query);
if(mysql_affected_rows()==1){
Header("Location:admin.php?op=admin&mix=added&news=null");
}
else{
die("there was a problem");
}
The db_functions work fine, I know this because on another page I call up the database info using the db_functions and it works fine. All of the variables exist as do the table entries. Like I said, this has been working fine for about a year now.
The problem I get is the "there was a problem" error. I've tried showing all errors, but that doesn't show anything either. I can't work out what it is. Any ideas?
Thanks in advance.
try to put "date" in single-quotes because date is a keyword in the current version of MySQL. The error might have happened because of database version upgrade (probably from 4 to 5).
If your table has an auto-increment ID, have you got to the maximum number allowed in that field?
There are few things which you need see.
-> See the below query
"insert into mixtable (date, djname, title, description, music, tracklist, deleted) values ('{$date}','{$djname}','{$title}','{$description}','{$music}','{$tracklist}', 0)";
When you are using double quote you need not use "." to append strings.
-> From the above explanation its quite clear that your query is not executing, try echoing the query and check it in sql editor you should be able to debug it quickly.
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