UPDATE query not functioning in MySQL 5.0 - mysql

I'm not completely sure what I'm doing wrong here I've looked through again and again for incorrectly placed quotes but I cannot for the life of me get this UPDATE query code to work... and I've troubleshooted it down to the query itself and it gives an incorrect syntax error, yet I have no idea what is wrong with it as it is identical to the manual.
$change = mysql_query("UPDATE Images SET Group='$group' WHERE ID = '$imgid'") or
die(mysql_error());

you might need to do :
"UPDATE Images SET `Group`='$group' WHERE ID = '$imgid'"
as group is a keyword in MySQL .

Well... if the update does not work this might have two reasons. Either your syntax is wrong; this should result in MySQL throwing an error which should show up with your die(mysql_error()).
Or there is simply no record matching your WHERE-condition. To check if any record was updated at all you might want to take a look at mysql_affected_rows(). And you could run a SELECT-query on that table, using the same WHERE-condition.

try this ..
"UPDATE Images SET Group='".$group."' WHERE ID = ".$imgid

Related

Selecting from a table sql

I am trying to select some rows from a table that I have in my database but I keep obtaining null $result. I think the problem is with my sql code.
The query is the following:
$result = mysqli_query($this->conexion, $sql);
The conexion is working fine I have checked so the problem is with $sql. My vareiable $sql is:
"SELECT * FROM `invent` WHERE `id_system` IN (500,504,502,498,499)"
My table is invent and I have a row named id_system, I have checked all the names and are correctly spelt and the column id_system contains integers and all ones of the query exist in the table.
I keep getting:
$result={"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
I don't know why is it not working, it might be because of the spelling in $sql.
Could someone help me?
Thanks in advance
I don't know if this will help, but this code might work for you:
SELECT 'id_system'
FROM 'invent'
WHERE 'id_system'=500
OR 'id_system'=504
And then you could add the rest of the values you need

why ami getting this #1305 - FUNCTION homeshopping.partID does not exist

mysql php admin table query enter image description here
I don't see why it says partID is giving me a problem? it is in the table and i think i have them linked correctly. I Have changed a few things i replaced comma with the and statement which cleared up alot of my errors. But since I've done that it continues to give me #1305 - FUNCTION homeshopping.partID does not exist. I have even looked at the structures an designer to make sure column names an tables are correct.
Don't forget to post your query as text also.
Because it's missing the keywork IN to have a query like this:
SELECT CONCAT(hscust.first,' ', hscust.last AS Customer,hsitems.description,hsitems.price,hsitems.itemCalss
FROM hscust,hsorders,hslineitem,hsitems
WHERE hslineitems.orderId = hsorders.orderId AND hsitems.partID = hslineitem.partNum AND hslineitem.price = hsitems.price AND partID IN ('CB03', 'CZ82');

Update a column on a table from a table on another schema MySql

I accidentally updated far more records than I should have with the wrong information. I have the correct information in another schema but I cannot figure out how to update the incorrect one.
I've tried something like -
Insert into schema1.table1 set columnName = (Select statement????) Where ?????
And then I get lost.
I've read around and tried a few answers out and I keep either getting a syntax error, or nothing happens.
Any help would be appreciated.
update schema1.table1
inner join schema2.table2
on schema1.table1.IDColumn = schema2.table2.IDColumn
set schema1.table1.column1 = schema2.table2.column2
This Worked for me

Whats wrong with this MySQL Update Query?

1st I'll give you the query, and then I'll tell you what I am trying to achieve, as I could be soo wrong or soo close.
mysql_query("UPDATE link_building SET
ID=$ID,Site=$Site,Date=$Date,Target_Site=$Target_Site,
Target_Contact_Email=$Target_Contact_Email,
Target_Contact_Name=$Target_Contact_Name,
Link_Type=$Link_Type,Link_Acquired=$Link_Acquired,
Notes=$Notes,Link_URL=$Link_URL WHERE ID=" . $ID);
What am I trying to achieve?
I want to update the fields
("ID","Site","Date","Target_Site","Target_Contact_Email","Target_Contact_Name",
"Link_Type","Link_Acquired","Notes","Link_URL")
in the table link_building with the values stored in the variables
("$ID","$Site","$Date","$Target_Site","$Target_Contact_Email","$Target_Contact_Name",
"$Link_Type","$Link_Acquired","$Notes","$Link_URL")
But I only want to update the record whos Id is equal to $ID.
UPDATE: I DO NOT SEE ANY ERROR. ITS REDIRECTS TO link_building.php and displays success message but doesn't change the data in the MySQL table.
Try escaping the data and removing the update of the ID since its already in your conditions:
mysql_query("UPDATE link_building SET Site='".mysql_real_escape_string($Site)."',Date='".mysql_real_escape_string($Date)."',Target_Site='".mysql_real_escape_string($Target_Site)."', Target_Contact_Email='".mysql_real_escape_string($Target_Contact_Email)."', Target_Contact_Name='".mysql_real_escape_string($Target_Contact_Name)."', Link_Type='".mysql_real_escape_string($Link_Type)."',Link_Acquired='".mysql_real_escape_string($Link_Acquired)."', Notes='".mysql_real_escape_string($Notes)."',Link_URL='".mysql_real_escape_string($Link_URL)."' WHERE ID=" . intval($ID));
For one, you're forgetting that you still need to quote your strings;
mysql_query("UPDATE link_building SET Site='$Site', Date='$Date',".
"Target_Site='$Target_Site', Target_Contact_Email='$Target_Contact_Email',".
"Target_Contact_Name='$Target_Contact_Name', Link_Type='$Link_Type',".
"Link_Acquired='$Link_Acquired', Notes='$Notes', Link_URL='$Link_URL' ".
"WHERE ID=$ID");
Note the added 's around all strings.
Bonus remark; you should really be using mysql_real_escape_string() on your strings before passing them on to the database.
if your columns are named like Target Site (with a space in it), you should adress it like that in your query (wich will force you to add backticks to it). also, you'll have to add quotes to colums that store anything else that strings. your query should look like:
UPDATE
link_building
SET
ID = $ID,
Site = '$Site', // single quotes for values
Date = '$Date', // ...
´Target Site´ = '$Target_Site' // and ´ for fields
[...]
this should solve why the query doesn't work (in addition: not how a bit or formatting makes it much more readable).
you havn't given information about that, but please note that you should always sanitize your variables before using it (your code doesn't look like you do) to avoid sql-injections. you can do this using mysql_real_escape_string or, even better, start using prepared statements.

is this the correct way to optimize MySQL Query?

$sql="update users_contain
set
wood_max = (Select building_production from building_level where merge_id=$subPrimaryKey and empire_id=$user_empireID) ,
iron_max = wood_max,
clay_max = wood_max
where user_id = $user_id";
Now there is a question.
will wood_max will always be updated first than iron_max and clay_max. so it is safe to use this way??
i do not want to use inner query for updating the iron_max and clay_max when i know it has same value for all three fields..
According to this documentation, your UPDATE statement works as you want it to: http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-update.html
Test it to be sure, but I think you're fine.