SQL: Use results of one query in another query - mysql

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.

Related

SELECT * FROM WHERE Query isn't retrieving any results

I'm using a SELECT * FROM "" WHERE "" = "" query and I'm not sure what I'm doing wrong with it. I'm trying to select an item based on its PO which is a completely unique identifier to one row in the table. Here's my process in doing so:
$jobnumber = $_GET['jref'];
$query = "SELECT * FROM `po_10152796` WHERE `po` = " .$jobnumber;
$results = mysqli_query($conn,$query) or die(mysqli_error($conn));
$rowitem = mysqli_fetch_array($results);
$jobname = $rowitem['Job Name'];
$phone = $rowitem['phone'];
Things i know are correct:
The "jobnumber" is retrieved correctly and matches up with an element in the table
The table is named "po_10152796" and there is a column named "po"
Forgot to post this, redid the code with prepared statements and it works, not sure what exactly I changed but here it is anyhow:
$jobnumber = $_GET['jref'];
$stmt = $conn->prepare( "SELECT `Job Name`, `Address`, `phone`, `description`, `materials` FROM po_10152796 WHERE po = ?");
$stmt->bind_param("i", $jobnumber);
if($stmt->execute()){
$stmt->bind_result($jobname, $address, $phone, $description, $materials);
$stmt->fetch();
}
try this line for the query
$query = "SELECT * FROM `po_10152796` WHERE `po` = '" .$jobnumber. "' ";

how to run two update queries in one?

I am trying to run 2 update queries as one `$result, Is this possible, and if so can someone please show me where am I going wrong with the below query?
$query = "INSERT INTO ptb_users (id, user_id, first_name, last_name, email, password )
VALUES('NULL','NULL','" . $firstname . "','" . $lastname . "','" . $email . "',MD5('" . $password . "'))";
mysql_query($query) or dieerr();
$result = mysql_query("UPDATE ptb_users SET ptb_users.user_id=ptb_users.id UPDATE ptb_users SET ptb_users.account_type= \"Client\"");
Mysql* functions are deprecated. Use PDO/mysqli instead.
As a security measure, mysql* functions wont allow you to execute multiple queries. You can with Mysql* however, you should prepare your statement before executing.
Since you have no WHERE clause on your UPDATEs, you can combine them like this:
UPDATE ptb_users SET ptb_users.user_id = ptb_users.id,
ptb_users.account_type = 'Client'
EDIT: If you want to UPDATE the row, you just INSERTed, then just add WHERE ptb_users.id = $id (you can use mysql_insert_id() to get the ID).
$query = "INSERT INTO ptb_users (id, user_id, first_name, last_name, email, password )
VALUES('NULL','NULL','" . $firstname . "','" . $lastname . "','" . $email . "',MD5('" . $password . "'))";
mysql_query($query) or dieerr();
$id = mysql_insert_id();
$result = mysql_query("UPDATE ptb_users SET ptb_users.user_id = ptb_users.id,
ptb_users.account_type = 'Client' WHERE ptb_users.id =".$id);

SET operator use in mysql. Explanation?

Allright, i see that without 1st line, the second query will not work. But I don't understand why. Why its not working without SET? What set does mean ?
$query = "update messages set $name=$name+1 where id='$idd'";
$db->setQuery( $query );
$db->query( $query ) or die('blogai');
$query2 = "select up,down from messages where id='$idd'";
$db->setQuery( $query2 );
$db->query( $query2 ) or die('blogai');
Check your sql request in your database query editor :
select up,down from messages where id='yourValue'

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()";