Error in updating database row in MYSQL - mysql

I have a table
items: id, userid, item_name, item_description
I want to update a row and used the following sql statement for it.
$updateQuery = "UPDATE items SET item_name = '$item_name',
item_desc = '$item_desc' WHERE userid = '$userid'
AND item_name = '$old_name'";
But it fails. Is it because I used the item_name field, which is to be updated, for selecting the row?

I think I see the problem
item_desc = '$item_desc'
"4 columns id, userid, item_name, item_description."
Change your query to
$updateQuery = "UPDATE items SET item_name = '$item_name', item_description = '$item_desc' WHERE userid = '$userid' AND item_name = '$old_name'";

you not update item_name because you used it in where clause
or
you can echo this string and run in database terminal to verify.
Try :
$updateQuery = "UPDATE items SET item_name = '" . $item_name . "', item_desc = '" . $item_desc . "' WHERE userid = " . $userid . " AND item_name = '" . $old_name . "';"

Please notice, in your query, you are referring the last column as "item_desc" which does not exist, as the actual column name is "item_description" .
MySQL is treating "item_desc" as a separate column in your table, but unable to find it, and hence the error.
Also, it is a good idea to pay attention to how you are concatenating your variable to your query. After equal to(=) sign, always use this notation ' ".$variable_name." ' to concatenate. Example:
select column1, column2 from table1 where (column1 = ' ".$variable_name." ' && column2 = ' ".$variable_name." ') ";

You have to concatenate the strings.
$updateQuery = "UPDATE items SET item_name = '" . $item_name . "', item_desc = '" . $item_desc . "' WHERE userid = " . $userid . " AND item_name = '" . $old_name . "'";

Instead of item_desc, it should be item_description.

Related

Select value in table if it exists in another

I am trying to get all the values from a users table where a specific company name is found.
I also need to get the rows in the users table where that same company name is in another table and matches the name in users table
I have 2 tables like this:
Table: Users.
USERNAME COMPANY EMAIL PHONE
Table: Dispatchers.
USERNAME DISPATCH_CLIENT
I need to get all the rows in the Users table that have a specific company name
I also need to get all the rows in the Users table where the USERNAME and DISPATCH_CLIENT fields in the Dispatch table match the USERNAME AND COMPANY field in Users table.
I have looked at some questions on here using EXISTS, but it either didn't work in my situation or i'm not smart enough to understand it.
This is what I have at the moment.
SELECT *
FROM Users
WHERE Users.COMPANY = '" . $company . "'
AND EXISTS
(SELECT USERNAME
FROM Dispatchers WHERE DISPATCH_CLIENT = '" . $company . "')";
Also tried doing it this way but no records are returned:
SELECT
*
FROM
Users
INNER JOIN
Dispatchers
ON Dispatchers.USERNAME = Users.USERNAME
AND Dispatchers.DISPATCH_CLIENT = Users.COMPANY
WHERE
Users.COMPANY = '" . $company . "'"
Try this
SELECT *
FROM Users
WHERE Users.COMPANY = '" . $company . "'
OR Users.USERNAME IN
(SELECT USERNAME
FROM Dispatchers WHERE DISPATCH_CLIENT = '" . $company . "')";
I think you missed the join condition in "exists" phase. Please try this.
SELECT *
FROM Users
WHERE Users.COMPANY = '" . $company . "'
AND EXISTS
(SELECT USERNAME
FROM Dispatchers
WHERE Dispatchers.DISPATCH_CLIENT = Users.COMPANY
and Users.USERNAME = Dispatchers.USERNAME)

SQL: Use results of one query in another query

I have a select statement and an update statement. What I would like to do in the update statement is set the value of 'recipes_saved' to the result of the select statement. I have tried to use this:
$query = "UPDATE `users` SET `recipes_saved` = ('SELECT `recipe_1_name` FROM `carbohydrates`') WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
$data= mysqli_query($dbc,$query) or die('Query failed: ' . mysqli_error());
but the query fails.
Any help would be much appreciated.
I think you have an extra ' in your 'SELECT and also in your FROM carbohydrates' and use LIMIT again like:
Try to copy the query below:
$query = "UPDATE `users` SET `recipes_saved` = (SELECT `recipe_1_name` FROM `carbohydrates` LIMIT 1) WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
You could of course remove the back tick if you want to make it less cluttering, like:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates LIMIT 1) WHERE user_id = '" . $_SESSION['user_id'] . "'";
As far as I know, you do not need so many quotes in your query. Try:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='" . $_SESSION['user_id'] . "'";
It would also be useful to log in to your database directly (either command line or a GUI client) and try running the query:
UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='username'
and see if that works.

register data from erlang to table mysql

I have a table in mysql named person
this a simple code of insertion of data in the table person
$id = "1";
$firstname = "afif";
$lastname = "kaled";
$test = mysql_connect("localhost", "root", "root");
if ($test) {
mysql_select_db("basetest", $test);
}
$sql = " INSERT INTO `person` SET
`id` = '" . $id . "',
`firstname` = '" . $firstname . "',
`lastname` = '" . $lastname . "' ";
#mysql_query($sql, $test);
I want to modify this function
test()->
Id ="11",
Firstname ="afif",
Lastname ="kaled",
%% here I want to register this data in the table person .
so the table person will have this data
11 afif kaled
I want to know if is it possible to register data from erlang to table mysql
I have already done an example of transfer data from erlang to txt file with this code :
exporttxt()->
F = fun() -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],person) end,
{atomic,L} = mnesia:transaction(F),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
#person{id = F1,firstname = F2,lastname = F3} <- L]).
but now as I already said I want to know is it possible or not to send data from erlang to a table in mysql
Of course it is possible.
Try to use a search before asking questions.

My Sql queries inorder to update it

How can I update over this query
$sqlquery = UPDATE("conference",
array('area_of_expertise' => $area_of_expertise_id1,
'long_name' => $_POST['longname'],
'short_name' => $_POST['shortname'],
'description' => $_POST['textarea'],
'creator_id' => $get_id
)
);
I inserted all the need data in the conference table while making sure that it was the same data the user had chosen.
Your UPDATE query syntax is wrong.
You're not saying what table you want to update and which column of that table.
You're just saying UPDATE.
Syntax should be like :
UPDATE tableName SET column = value [ WHERE someColumn = someValue ]
Reference :
http://www.w3schools.com/php/php_mysql_update.asp
I assume you're also using PHP. Is 'UPDATE' a self-defined function? I've never come across it before.
$update = mysql_query("UPDATE conference SET area_of_expertise = '" . $area_of_expertise_id1 . "', long_name = '" . $_POST["longname"] . "', short_name = '" . $_POST["shortname"] . "', description = '" . $_POST["textarea"] . "' WHERE creator_id = " . $get_id);
I'm only assuming your table and column names by the way.
$query = "UPDATE conference SET area_of_expertise='$area_of_expertise_id1', long_name='$_POST['longname']', short_name='$_POST['shortname']', description='$_POST['textarea']' WHERE creator_id='$get_id'");
$update_value = mysql_query($query);
Hope that Helps.

mysql use fields value while insert a record

A simple question!:
Can I use my field value in an insert query?
For example, I've a field named id, it is an auto_increment field. i want to add this field value to another field. while I'm inserting it.
A simple php code of my need:
$query_1 = mysql_query("INSERT INTO table (name) VALUES ('abcd')"); // id automatically increment
$query_2 = mysql_query("SELECT id FROM table WHERE name = 'abcd'"); // selects previous id
// fetches result //
$query_3 = mysql_query("UPDATE table SET code = '" . $id + 1000 . "' WHERE id = '" . $id . "');
Can you convert it to just 1 query?
Thanks ...
I don't think you can do it in one query but you can do it in 2 for sure:
mysql_query("INSERT INTO table (name) VALUES ('abcd')");
$id = mysql_insert_id();
mysql_query("UPDATE table SET code = '" . $id + 1000 . "' WHERE id = '" . $id . "');
Here's how you do it with one query:
$query_1 = "INSERT INTO table (note) VALUES ('abcd'); UPDATE table SET code = LAST_INSERT_ID() + 1000 where id = LAST_INSERT_ID()";
I can't remember if mysql allows multiple queries in one command - I think maybe not, so try this:
$query_1 = "INSERT INTO table (note) VALUES ('abcd')";
$query_2 = "UPDATE table SET code = id + 1000 where id = LAST_INSERT_ID()";