UPDATE SET gets MySQL error - php

Hi i have a reads counter, but i always get an MySQL error:
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 ''reads') VALUES ('2') WHERE id = '20'' at line 1
$reads = $row['reads']+1;
$newsid = $row['id'];
if(!$query = $db->query("UPDATE cmsss_news_articles SET reads = '$reads' WHERE id = '$newsid'")) {
echo "<center><b>Error, cant update row</b></center>";
}
Can you please help me where is the mistake?

reads is a reserved word in MySQL. Escape it with backticks.
UPDATE cmsss_news_articles
SET `reads` = '$reads'
...

Reads is a reverse key word in MySQL, hence put that in backquotes.
try this:
if(!$query = $db->query("UPDATE cmsss_news_articles SET `reads` = '$reads' WHERE id = '$newsid'")) { ^^
echo "<center><b>Error, cant update row</b></center>";
}

You can also loose the increment variable to gain some performance and simplicity.
$newsid = $row['id'];
if(!$query = $db->query("UPDATE cmsss_news_articles SET `reads` = `reads` + 1 WHERE id = '$newsid'")) {
echo "<center><b>Error, cant update row</b></center>";
}

Related

How to use GROUP BY method in php-mysql

I’m trying to use "group by" instead of "DISTINCT" in my php file to select some rows that all of them have an specific column value and it’s "idchat".
And I want to get more than one columns
please help me!
I’ve checked every pages but I didn’t understand enything
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat FROM database where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
mysqli_close($connection);?>
and this code gives me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/---/test.php on line 12
See next output:
mysql> SELECT * FROM database;
ERROR 1064 (42000): 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 'database' at line 1
mysql> SELECT * FROM mytable;;
ERROR 1146 (42S02): Table 'test.mytable' doesn't exist
ERROR:
No query specified
mysql>
The first query is not understood because database is a reserved word. You should not use that to name a table. A work around is to add backquotes around the name:
mysql> SELECT * FROM `database`;
ERROR 1146 (42S02): Table 'test.database' doesn't exist
mysql>
"Could you correct my codes please?": Ok, but untested:
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat,id FROM `database` where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
else {
echo mysqli_error($connection);
}
mysqli_close($connection);?>
}
I also did add id to the query because of comment from #Raymond

PHP MYSQL "UPDATE"

I don't know why, but for some reason the code below is not working as intended
$SQL = "UPDATE characters SET
name = '$char_name',
status = '$char_status',
gender = $char_gender,
pos.x = $char_posx,
pos.y = $char_posz,
shards = $char_money,
level = $char_level,
exp = $char_exp,
hair = $char_hair,
color.r = $char_color_r,
color.g = $char_color_g,
color.b = $char_color_b,
spawn = $char_spawn
WHERE username = '$nick'";
mysql_query($SQL) or die("ERRORCODE 04 - DB QUERY FAIL");
echo "saved";
it's always giving me the "ERRORCODE 04.." meaning that the query failed..
FYI: setting pos.y db value to the char_posz is correct as the axes are different from the Form to the actual database
EDIT: code now changed a bit due to some comments, looks now like this:
$SQL = "UPDATE characters SET
name = '$char_name',
status = '$char_status',
gender = $char_gender,
pos_x = $char_posx,
pos_y = $char_posz,
shards = $char_money,
level = $char_level,
exp = $char_exp,
hair = $char_hair,
color_r = $char_color_r,
color_g = $char_color_g,
color_b = $char_color_b,
spawn = $char_spawn
WHERE username = '$nick'";
mysqli_query($dbcon, $SQL) or die(mysqli_error($dbcon));
echo "saved";
this is the error I get:
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 '
pos_x = ,
pos_y = ,
shards = ,
' at line 4
Try to put single quotes around all variables in the query

Updating my record

Dear friend i am trying to update the recode but the following message always come up
" 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 9"
the code looks fine but i do not understand what i am doing wrong can someone help.
thanks in Advance.
<?php
if(isset($_POST['edit'])){
// this id wil be pulled from the URL above.
$hot_id = $_GET['hotl'];
$hotel_name = escape_value($_POST['title']);
$hotel_star = escape_value($_POST['category']);
$shortdes = escape_value($_POST['shortdes']);
$country = escape_value($_POST['country']);
$address = escape_value($_POST['address']);
$pcode = escape_value($_POST['pcod']);
$city = escape_value($_POST['city']);
$query = "UPDATE Hotels SET
hotel_name = '{$hotel_name}',
star ='{$hotel_star}',
description = '{$shortdes}',
country = '{$country}',
hotel_address = '{$address}',
hotel_postal_code = '{$pcode}',
hotel_city = '{$city}'
WHERE hotel_id = {$hot_id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1){
//Success
}else{
die("Some thing wrong with the Upadate: ". mysql_error());
}
}else{
//error ocurred
}
?>
i am posting my question in both PHP and Mysql Forum because i do not know exaectly where is the problem.
The easiest way to debug SQL statements (as mentioned above) is to echo out your query before you submit it and see exactly what you are sending to the database. That being said, why don't you try putting some quotes(' ') around your $hot_id var:
WHERE hotel_id = '{$hot_id}'
Somewhere in the update there should be $hot_id:
"UPDATE Hotels SET
hotel_name = '{$hotel_name}', hotel_id = '{$hot_id}', etc

Using Update Query to Copy Column Data

I need to copy the value in a column named TEAM from one row into another row. Both rows need to have the same team name. This is my query that doesn't work:
$query = "UPDATE profiles SET team = (SELECT team FROM profiles WHERE id = '$coach_id') WHERE id = '$player_id'";
I have tried removing single quotes, removing "FROM profiles", changing value to table.value, tried to give a newdata.clan alias, and I have even tried changing the values to integers instead of parameters. Nothing works, and this is what I get:
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 'WHERE id = '') WHERE id = ''' at
line 3
$query1 = "SELECT team FROM profiles WHERE id = '$coach_id'";
/* get the value of the first query and assign it to a variable like $team_name */
$query2 = "UPDATE profiles SET team = '$team_name' WHERE id = '$player_id'";
Also, you should surround your PHP variables in curly braces:
$query = "UPDATE profiles SET team = \"(SELECT team FROM profiles WHERE id = '{$coach_id}')\" WHERE id = '{$player_id}'";
From the MySQL manual:
"Currently, you cannot update a table
and select from the same table in a
subquery."
Source: http://dev.mysql.com/doc/refman/5.0/en/update.html
Use the method that FinalForm wrote:
<?
$coach_id = 2;
$player_id = 1;
$query1 = "SELECT team FROM profiles WHERE id = '$coach_id'";
$rs = mysql_query($query1);
if ($row = mysql_fetch_array($rs)) {
$team_name = $row['team'];
$query2 = "UPDATE profiles SET team = '$team_name' WHERE id = '$player_id'";
mysql_query($query2);
// Done, updated if there is an id = 1
} else {
// No id with id = 2
}
?>

Problem in using UPDATE command in PHP

I am have some problem in updating my qotwVote1a table's Vote1a field through PHP. Could you please have a look at the code, and tell me what am i doing wrong in here.
$result = mysql_query("SELECT * FROM qotwVote1a WHERE QuestionId='".$questionId."' AND MemberId='".$id."'");
while($row = mysql_fetch_array($result))
{
$originalVote=$row['Vote1a'];
$newVote=$originalVote + $vote;
//echo ($newVote);
}
$sql = <<<END
UPDATE qotwVote1a
SET Vote1a = '$newVote',
WHERE QuestionId = '$questionId' AND MemberId = '$id'
END;
mysql_query($sql);
if (mysql_error()) {
die("Error executing query '$sql': " . mysql_error());
}
Using this code I got an error:
"Error executing query 'UPDATE qotwVote1a SET Vote1a = '2', WHERE QuestionId = '57' AND MemberId = 'zee'': 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 'WHERE QuestionId = '57' AND MemberId = 'zee'' at line 3"
Regards
Zeeshan
You have a comma after $newVote. Remove it, and you'll be peachy-keen.
Also, you don't need to wrap numbers in quotation marks, and don't do it if your column is an integer or float type. Doing so just causes those to get converted to numbers, anyway, so it's not a big deal.
UPDATE qotwVote1a
SET Vote1a = '$newVote'
WHERE QuestionId = '$questionId' AND MemberId = '$id'
There’s a comma in you MySQL query after the SET clause that’s misplaced. So try this:
$sql = <<<END
UPDATE qotwVote1a
SET Vote1a = '$newVote'
WHERE QuestionId = '$questionId' AND MemberId = '$id'
END;
It's looks like you are missing some code as the query supplied is not the query giving the error. The problem in the query is an extra comma after the "Vote1a = '2'" statement though.

Categories