Mysql syntax error issue - php

I've got a message while i'm run following sql query...
"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 'group = 'dfdfd' WHERE id = '39'' at line 1"
Sql query:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '$surname_g', group =
'$g_g' WHERE id = '$id'");

Please use ` to enclose group, it is being treated as special (group by keyword of SQL) by mysql
Use the following:
UPDATE addcontacts SET surename = '$surname_g', `group` = '$g_g' WHERE id = '$id'
Note `group` and not group

Try:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '".$surname_g."', `group` = '".$g_g."' WHERE id = '".$id."'");

Your id might be an integer and you are enclosing it with two single quotes (') and that would really produce the error.
$sql_update = mysql_query("UPDATE addcontacts SET surename = '{$surname_g}', group =
'{$g_g}' WHERE id = {$id}");
Thank you :)

Related

Same query works different

I made php script that updates database:
<?php
include 'config.php';
$PauseID = "2";
$ProductionID = "1411979966";
$sql = "SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' AND Id = #max;";
mysql_query($sql) or die(mysql_error());
mysql_close($connect);
?>
While executing this script it returns 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 'UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID
= '1411979966' A' at line 1
But if I try same update query execute by command line it works
SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID = '1411979966' AND Id = #max;
I don't understand how the same thing works differently.
You can not run multiple queries as one statement with PHP. Try like this:
$sql = "UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' ORDER BY Id DESC LIMIT 1;";
You don't need this variable anyway.
Its is because your PHP interpreter treats your $sql as string. Change your code to
$sql = "UPDATE tblproductionbreaks SET IDPause = '{$PauseID}' WHERE ProductionID = '{$ProductionID}' AND Id = (SELECT MAX(Id) FROM tblproductionbreaks)";
Also you can debug it by echo $sql and see what actually the $sql returning

Increment of field default value in mysql/php

$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
When i check the database the value didnt increase.
Help me out.
yuo have a sintax mistake in your update query, you are using quotest instead of backtick. Use backticks for column names and quotes for values try to change as follow
$saa = "update `aspirantdt` set `vote` = (`vote`+1) where `post_id` = '".$id."' ";
Just remove quotes or use Backtics for column names.
$saa = "UPDATE aspirantdt SET vote = vote + 1 where post_id = '$id' ";
Or with backticks
$saa = "UPDATE `aspirantdt` SET `vote` = `vote` + 1 where `post_id` = '$id' ";
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
means that 'vote' and 'post_id' are literal strings, not table names (that is, it will compare $id to the actual string post_id instead of the value of the post_id column).
What you want is backticks to quote them as a column/table name instead;
$saa = "update `aspirantdt` set `vote` = `vote`+1 where `post_id` = '$id' ";
You don't need quotes around vote as its a column name, not a string.
Your SQL is probably trying to put vote1 in an int column.

SET and SELECT query not executing when run in PHP code

I have this php code -:
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0'; SELECT f.* FROM feed as f"; $r = mysqli_num_rows($q);.
This results in 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 'SELECT f.* FROM feed as f' at line 3.
But surprisingly, when the same mysql query is run in phpmyadmin it runs as needed.
Whats the matter? Thanks for help...
your problem is the semicolone ; before select
try replace it by comma ,
like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' , SELECT f.* FROM feed as f";
$r = mysqli_num_rows($q);
or you ould also separate your query like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' ";
$q .= "SELECT f.* FROM feed as f";

Error: You have an error in your SQL syntax; check the manual that corresponds .. syntax to use near '1' at line 1

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 code is a follows where i get the error and that is come going from http://cms2.br-de.tk/editinfo.php to http://cms2.br-de.tk/updateinfo.php
<?php
mysql_connect("mysql10.000webhost.com","******_12","*******") or die("Error:".mysql_error());
mysql_select_db("******_1");//add your dbname
//get the variables we transmitted from the form
$Title = $_POST['Title'];
$Author = $_POST['Author'];
$Date = $_POST['Date'];
$Content = $_POST['Content'];
//replace TestTable with the name of your table
//replace id with the ID of your user
$sql = "UPDATE `posts` SET `Tilte` = '$Tilte',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID' 1 ";
mysql_query($sql) or die ("Error: ".mysql_error());
echo "Database updated. <a href='editinfo.php'>Return to edit info</a>";
?>
You have added additional 1 at the end of query. It should be like this:
$sql = "UPDATE `posts` SET `Tilte` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID'";
You have a spare 1 at the end of your statement.
UPDATE `posts` SET `Tilte` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID';"
As Grigore correctly spotted, you might also have a typo in your statement depending on your column names.
UPDATE `posts` SET `Title` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID';"
`Tilte` = '$Title'
maybe this is title not tilte, besides that there's a "1" right at the ending of the query

Unsolvable MySQL 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 'repeat = 'week', location = 'Patowmack Farm', location_link = 'http://maps.googl' at line 1
I keep getting this message for both my update script (show above), and my insert script. I cannot find why it's doing this! Anyone available to help?
My update code:
foreach($_POST['enabled'] as $key => $value ) {
$key = mysql_real_escape_string($key);
if ($_POST['delete'][$key]=='1') {
mysql_query("DELETE FROM upcoming WHERE id='$key'") or die(mysql_error());
}
else {
$title = mysql_real_escape_string($_POST['title'][$key]);
$date = mysql_real_escape_string(($_POST['date'][$key]));
$repeat = mysql_real_escape_string($_POST['repeat'][$key]);
$group = mysql_real_escape_string($_POST['group'][$key]);
$group_link = mysql_real_escape_string($_POST['group_link'][$key]);
$location = mysql_real_escape_string($_POST['location'][$key]);
$location_link = mysql_real_escape_string($_POST['location_link'][$key]);
$notes = mysql_real_escape_string($_POST['notes'][$key]);
$enabled = mysql_real_escape_string($_POST['enabled'][$key]);
mysql_query("UPDATE upcoming SET title = '$title', date = '$date', repeat = '$repeat', location = '$location', location_link = '$location_link', group = '$group', group_link = '$group_link', notes = '$notes', enabled = '$enabled' WHERE id = '$key' LIMIT 1") or die(mysql_error());
}
}
Have you tried changing the query to:
mysql_query("UPDATE `upcoming` SET `title` = '$title', `date` = '$date', `repeat` = '$repeat', `location` = '$location', `location_link` = '$location_link', `group` = '$group', `group_link` = '$group_link', `notes` = '$notes', `enabled` = '$enabled' WHERE `id` = '$key' LIMIT 1") or die(mysql_error());
Edit: And as others have stated; you are using reserved words. I recommend always using the ` symbol. (This can be found at the top left for most keyboards: under the escape key, above the tab key, to the left of the number 1 key.)
GROUP and REPEAT are reserved keywords in MySQL so you have to "escape" it with backticks:
`group` = '$group'
`repeat` = '...'
Also I'm making an assumption here, but you shouldn't wrap $key in quotes because it is an integer value. Also make sure you type cast it to an int by doing int($key).
repeat is a keyword in MySQL use back ticks repeat to use this.
Repeat is a mySQL reserved word: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
Try surrounding your column names with backticks.

Categories