INSERT INTO not working on newest xampp - mysql

$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf) VALUES ($plan_name, $plan_nr, $anz)";
echo "<br />".$query."<br />";
if (!mysql_query($query) && !$error) {
die (mysql_error());
}
mysql-error tells me:
INSERT INTO 'p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')
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 ''p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')' at line 1
Where is the code wrong here?

the error is the wrapping of single quotes around table name.
Table names as well as column names are identifiers. They should be wrap if a name is a reserved keyword with backtick (optional if not). Single quotes are for string literal.
MySQL - when to use single quotes, double quotes, and backticks?

Use this.
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";

$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";

Related

Mariadb / MySQL JAVA Prepared Statement Syntax Error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
// Attempt to get 1 transaction
String sqlQuery = "INSERT INTO '" + this.crawlPageTableName
+ "' (url, status) VALUES (?, 'added')";
PreparedStatement prest = connect.prepareStatement(sqlQuery);
prest.setString(1, this.url);
System.out.println("Querying with: " + sqlQuery);
// Result set get the result of the SQL query
this.resultSet = prest.executeQuery();
I am really not getting what I am doing wrong here to get this error.
The output of printline is
Querying with: INSERT INTO 'crawlPage' (url, status) VALUES (?, 'added')
java.sql.SQLSyntaxErrorException: (conn=1877278) 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 ''crawlPage' (url, status) VALUES ('
I am really lost here as the same query from the printline works directly on the MariaDB server through phpMyAdmin.
I believe you shouldn't have any single quotes wrapping the table name:
String sqlQuery = "INSERT INTO " + this.crawlPageTableName
+ " (url, status) VALUES (?, 'added')";

Inserting data into the mysql database from perl

I am trying to insert data into a MySQL database:
$response = $client->fql->query(
query => '
SELECT name, email, birthday, username, first_name, last_name, pic
FROM user
WHERE uid = me()
',
);
print join "\n Name:", sort map { $_->{name} } #$response;
$dbh->do("
INSERT INTO Users(SNo,Name,Email,Birthday,UserName,FirstName,LastName)
VALUES(1,
sort map { $_->{name} } #$response,
'imm\#gmail.com',
'1987/12/10',
'imm',
'imm',
'Dee')
");
$dbh->disconnect();
used the mysql query in one line.
This above print statement is printing the name correctly but why the above sql insert statement is not working?
I connect the db and after that i am receiving the value and printing in the browser is working.
Why does the mysql statement not accept the value?
When inserting the database is not working?
You should have a look at the official doc
and specially this :
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");
# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

Error "You have an error in your SQL syntax;" near table_name

This is my Coding :
session_start();
include 'Connect.php';
$userid=$_SESSION['userid'];
$tanggal=date('Y-m-d h:i:s');
$status='Pending';
echo $userid;
echo $status;
echo $tanggal;
mysql_query("INSERT INTO 'Order' (IdPelanggan,Tanggal,StatusOrder) VALUES ('$userid', '$tanggal', '$status')") or die (mysql_error());
After i run that coding, i got this error :
`9Pending2013-07-27 11:25:54You 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 (IdPelanggan,Tanggal,StatusOrder) VALUES (9, 2013-07-27 11:25:54, Pending)' at line 1`
I wonder where my mistakes is..
If you want to escape reserved words in MySQL like order then do it with backticks and not with quotes:
INSERT INTO `Order` ...
Quotes are string delimiters.

insert variable into mysql table does not work

I'm trying to insert a variable into mysql table, but it does not do what i want (no error, it simply does not work)
$string = 'this is an example';
mysql_query("insert into tablename (columnname) values ????");
Anyone can help me how to insert $string into table tablename? I tried replacing ???? with ($string), (".$string."), $string, but all of them does not insert the $string variable into the database.
Thanks in advance!
You can use \" (an escaped quotation mark) to include quotes inside your query, like so:
$string = 'this is an example';
mysql_query("insert into tablename (columnname) values (\"$string\")");
you forgot the quote (') and you should use double-quotes a your string delimiter.
$string = "test abc";
mysql_query("insert into t (col) values ('$string')");

MySql INSERT Failure

Why does the following query fails on MySql?
NHibernate: INSERT INTO UserSetting (userId, key, value) VALUES (?p0, ?p1, ?p2);
?p0 = ccda78da-689d-4d86-ba72-d65eaf281edf [Type: Guid (0)], ?p1 = 'Hello' [Type: String (5)], ?p2 = 'World' [Type: String (5)]
With this 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 'key, value) VALUES ('ccda78da-689d-4d86-ba72-d65eaf281edf', 'Hello', 'World')' at line 1"}
key is a reserved word.
Try this:
INSERT INTO UserSetting (userId, `key`, value) VALUES (?p0, ?p1, ?p2)
Key is a reserved word, use ticks, example in link
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html