mysql snytax error in foreach loop - mysql

I run the page and this error comes up. I still cannot find out where is the problem:
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 'right,createtime) VALUES ('test10', 1, 1, now())' at line 2
foreach($array as $value){
//insert record
$sql2 = "INSERT INTO projectright
(generalusername,projectid,right,createtime)
VALUES
('$_POST[username]',
".$value.",
1,
now())";
if (!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
}

right is reserved keyword
you should escape it by backticks like that
INSERT INTO projectright
(generalusername,projectid,`right`,createtime)
.....
.....
take a look to reserved keywords when creating/playing by columns , and escape them by backticks.
try this:
foreach($array as $value){
$sql2 = "INSERT INTO projectright
(generalusername,projectid,`right`,createtime)
VALUES
(".$_POST['username'].",
".$value.",
1,
now())";
if (!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
}
as strawberry said: Better to not take reserved keywords.

$value is an array and this is the cause of the error most probably. Try something like:
$sql2 = "INSERT INTO projectright
(generalusername,projectid,right,createtime)
VALUES
('$_POST[username]',
".implode($value).",
1,
now())";

Related

Query Failed: You have an error in your SQL syntax

Database 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 'LIMIT 1' at line 1
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
?>
Try to replace all the query thing by this:
$query = "
SELECT *
FROM subjects
WHERE id = $subject_id
LIMIT 1";
I'd be looking at what your passing into $subject_id.
Please please please don't use SELECT *. Even if you want all of the columns, write them out. If your tables change and get more columns added then your pulling along more data.

SQL syntax error 1064

If someone can explain why doesnt work, i would apriciate :)
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 ''categorias'' at line 1
<?php
include "includes/connection.php";
$query = "SELECT * FROM 'categorias' ";
$result = mysql_query($query) or die (mysql_error());
while ($categorias = mysql_fetch_array($result)) {
echo "<p>" . $categorias ['descricao'] . "</p>";
}
?>
Remove the single quotes from your table name. Instead use back ticks if needed.
$query = "SELECT * FROM categorias ";
Or
$query = "SELECT * FROM `categorias` ";

MySQL server version for the right syntax to use near

I am getting the following 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 ' `DeweyEdition`
BLOB NOT NULL, , `DeweyNumber` BLOB NOT NULL)' at line 1
<br />Error No: 1064<br />ALTER TABLE tt_hj_import ADD COLUMN
(`CountryOfPub` BLOB NOT NULL, , `DeweyEdition` BLOB NOT NULL, ,
`DeweyNumber` BLOB NOT NULL) in /var/www/system/database/mysql.php on line 50"
My code:
public function alterImportTable($new_fields) {
if (!empty($new_fields)) {
$sql = "ALTER TABLE " . DB_PREFIX . "hj_import ADD COLUMN ";
$fields_sql = array();
foreach ($new_fields as $field) {
$fields_sql[] = '`' . $field . "` BLOB NOT NULL, ";
}
$sql .= '(' . implode(', ', $fields_sql) . ')';
$sql = str_replace(', )', ')', $sql);
$this->db->query($sql);
}
}
How do I fix this?
I think there is an extra space in the query, try removing , after NULL, like this:
$fields_sql[] = '`' . $field . "` BLOB NOT NULL ";
You need one ADD COLUMN per column.
I had the same issue in my program, i just change the query version
mysql_query($sql,$con)
TO
mysqli_query($sql, $con)

PDO MySQL Error - SQLSTATE [42000]: by UPDATE(ING) integer to table

I can update text strings (varchar) types to my Mysql database with PDO without any problems. but with integer (int(11)) types - my PDOstatement has some big problems and also can not write the integer value to the database.
here you can see the error message I get by putting integer values for UPDATE:
ERRNO:42000 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 'age = '800' WHERE id = '1'' at line 1
I am getting the values from $_POST like this:
foreach ($_POST['changed'] as $SubArray) {
foreach ($SubArray as $key => $value) {
if ($key === 'recid') continue;
$sql = "UPDATE clients SET $key = :value WHERE id = :recid";
$STH = $DBH->prepare($sql);
try {
$STH->execute(array(':value' => $value, ':recid' => $SubArray['recid']));
} catch (PDOException $e) {
$code = $e->getCode();
$file = $e->getFile();
$line = $e->getLine();
$msg = $e->getMessage();
echo "$file:$line ERRNO:$code ERROR:$msg";
}
}
}
echo urlencode($sql) => UPDATE+clients+SET+age+%3D+%3Avalue+WHERE+id+%3D+%3Arecid
setting up the query ($sql) like this, does the trick.
$sql = "UPDATE `clients` SET `$key` = :value WHERE id = :recid";
before and after table and column name use backticks
`

MYSQL re-fetch error

I just got error from MYSQL saying "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"
After submiting form on the page i got that messenge.
Any ideas?
Here is sql code that i used
$sql = "SELECT * FROM member ".
"WHERE termId='$term' ".
"AND year='$year' ".
"AND familyId='$familyId' order by memberId";
$rs = mysql_query($sql) or die(mysql_error());
You may need to replace single quotes in your query with backticks as follows.
$sql = "SELECT * FROM member ".
"WHERE termId=`$term` ".
"AND year=`$year` ".
"AND familyId=`$familyId` order by memberId";
$rs = mysql_query($sql) or die(mysql_error());
Your final SQL is
SELECT * FROM member_class WHERE termId='2' AND year='' AND familyId='' order by memberId
I suspect at least familyID, maybe also year are number fields, so this is a syntax error.
Always use mysql_real_escape_string to escapes special characters in a string.
$term=mysql_real_escape_string($term);
if(isset($year,$familyid,$term))
{
$myear=intval($year);
$mfamilyid=intval($familyid);
$sql = "SELECT * FROM member WHERE termId='$term'
AND year='$myear'
AND familyId='$mfamilyId'
order by memberId";
$rs = mysql_query($sql) or die(mysql_error());
}