Can't insert data into my local mysql server [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
Hi I'm having problems inserting into my local database, I have an established connection as I can query using "SELECT * FROM contact", but I cannot insert using the following code
$query1 = "INSERT INTO 'contact' ('e-mail', 'subject', 'body')
VALUES('email#gmail.com', 'another question', 'Just asking?')";
This is the error it's giving me -
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 ''e-mail', 'subject', 'body')
If anyone could kindly assist?

Try this. no need for ' around table name
$query1 = "INSERT INTO contact (`e-mail`, `subject`, `body`)VALUES('email#gmail.com', 'another question', 'Just asking?')";

Related

In PHP, how to make SQL query with # in? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
I have problem. in my database, I have a column email. When I make SQL query I get following 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 '#gmail.com)' at line 1
This is my code:
$sql = "SELECT ID_Dijak from dijak WHERE (Email=".$mejl.")";
If I try to do query in php my admin it works if I put ' ' between my email, but how to do it in php? Thank you.
I'm sorry I made a mistake earlier... I tested this way and it should now work
$sql='SELECT ID_Dijak from dijak WHERE (Email = "' . $mejl . '")';
take datatype varchar() for email
I would suggest using a prepared statement in PHP, also to prevent injection attacks.
$stmt = $dbc->prepare("SELECT ID_Dijak from dijak WHERE Email=?");
$stmt->bind_param("s", $mejl);
$stmt->execute();

sql syntax error using variable's value as column [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I'm trying to update a value in my table, but i have a syntax-error and i can't find the error.
This is my php code:
$data = new MysqlClass();
$data->connect();
$result_sql = $data->query("UPDATE iscrizioni SET '".$matricola."' = 'si' WHERE 'COD'=".$cod);
it returns me:
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 ''805710' = 'si' WHERE 'COD'=1' at line 1
Have you any idea where the error is?
Thank you very much!
You are using column's names as strings, use ` instead of '. Or don't use it at all.
$result_sql = $data->query("UPDATE `iscrizioni` SET `".$matricola."` = 'si' WHERE `COD` = ".$cod);

why am i getting this query failed syntax error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I can not find out why this is not working.....
i keep getting this message
Query failed: 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 ''laketaho_benny'.'tblPictures' SET 'caption' = 'uuuuuuuuuupppp' WHERE 'tblPic' at line 1
and this is my code
$sql = "UPDATE `laketaho_benny`.`tblPictures` SET `caption` = `$caption` WHERE `tblPictures`.`pictureID` =$pictureID;";
Single quotes are for strings. You're thinking of backquotes ( ` ).

getting query error for special characters ie " ' " tag [duplicate]

This question already has answers here:
Mysql + php with special characters like '(Apostrophe) and " (Quotation mark)
(8 answers)
Closed 9 years ago.
Query Error : CALL spCheckUserMailAccount('way2waymail#gmail.com',
'2', 'D'cruz', 'way2waymail#gmail.com') Details: 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 'cruz',
'way2waymail#gmail.com')' at line 1
Query Error : CALL spAddMailAccount('way2waymail#gmail.com', '2',
'D'cruz', 'way2waymail#gmail.com', '123456', 'smtp.gmail.com', '465')
Details: 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 'cruz', 'way2waymail#gmail.com', '123456', 'smtp.gmail.com',
'465')' at line 1
Try encapsulating values with double quotes. Some thing like CALL spCheckUserMailAccount("way2waymail#gmail.com", "2", "D'cruz", "way2waymail#gmail.com"). If you still get errors, try using mysql_real_escape_string.
Ref: http://php.net/manual/en/function.mysql-real-escape-string.php
Whenever inserting fields into a db always check for single quotes. If you have already inserted them then I believe you will have to use the REPLACE function to retrieve the proper value
SELECT REPLACE(Customer_Name, '\'', '') FROM table

php variable used as mysqli column name syntax error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 9 years ago.
This is where I think it´s the problem...
$sql1 = "SELECT `puntos_globales`, '$juego'
FROM `lista_jugadores` WHERE `id_jugador`='$noTop'";
This is the error message:
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 "blackOps2'='1' WHERE `id_jugador` = '10" at line 1
$juego .. is a php variable that holds the column name; in this case blackOps2. I don´t know why in the error says blackOps2'='1'??
from the error it seems you didnt provide the true query and it looks you have two where clause
blackOps2='1' WHERE `id_jugador` = '10"
try do it like that
WHERE `id_jugador` = '10' AND blackOps2='1'

Categories