sanchez77 Posted August 2, 2009 Share Posted August 2, 2009 so I am query one table for a value, and then i want to use that value as part of where clause to update a record in another table. but for some reason that i what i am doing wrong? //setting value $masterid = mysql_query("SELECT masterid FROM lockers WHERE lockerno = '01' LIMIT 0, 30"); //update table with equal value mysql_query("UPDATE lockersmaster SET timeout = '$timeout', dateout = '$dateout', storetimeout = '$storetimeout' WHERE id = '$masterid'"); Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/ Share on other sites More sharing options...
sanchez77 Posted August 2, 2009 Author Share Posted August 2, 2009 Can i join them? Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889109 Share on other sites More sharing options...
Fadion Posted August 2, 2009 Share Posted August 2, 2009 mysql_query() doesn't return a usable value. The code should be: <?php $results = mysql_query("SELECT masterid FROM lockers WHERE lockerno = '01' LIMIT 0, 30"); while($values = mysql_fetch_array($results)){ //results from mysql are put in an array and each row is looped //set $timeout, $dateout and $storetimeout $resultsU = mysql_query("UPDATE lockersmaster SET timeout = '$timeout', dateout = '$dateout', storetimeout = '$storetimeout' WHERE id=" . $values['id']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889125 Share on other sites More sharing options...
sanchez77 Posted August 2, 2009 Author Share Posted August 2, 2009 Should it be $values['masterid'] ? When i tried that, it gave me a blank page and no tables were updated Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889139 Share on other sites More sharing options...
ldougherty Posted August 2, 2009 Share Posted August 2, 2009 That is exactly correct, it should be.. <?php $results = mysql_query("SELECT masterid FROM lockers WHERE lockerno = '01' LIMIT 0, 30"); while($values = mysql_fetch_array($results)){ //results from mysql are put in an array and each row is looped //set $timeout, $dateout and $storetimeout $resultsU = mysql_query("UPDATE lockersmaster SET timeout = '$timeout', dateout = '$dateout', storetimeout = '$storetimeout' WHERE id=" . $values['masterid']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889145 Share on other sites More sharing options...
sanchez77 Posted August 2, 2009 Author Share Posted August 2, 2009 i appreciate your help, but it's not working for, just keeps giving me a blank page, Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889153 Share on other sites More sharing options...
ldougherty Posted August 2, 2009 Share Posted August 2, 2009 Try adding this to your code.. ini_set('display_errors', 1); error_reporting(E_ALL); This will show you any errors in your script when executing the page. If it doesn't work just check thoroughly through your code for syntax and try outputting variables before the UPDATE query to see if they are coming through the first query as expected. IE echo $values['masterid'] Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889155 Share on other sites More sharing options...
Philip Posted August 2, 2009 Share Posted August 2, 2009 Or, just in a single query: UPDATE table1 join table2 on table1.id=table2.id SET table1.some_val = table2.value Which when applied should look something like (untested of course): UPDATE lockersmaster JOIN lockers ON lockersmaster.id=lockers.masterid SET lockersmaster.timeout = '$timeout', lockersmaster.dateout = '$dateout', lockersmaster.storetimeout = '$storetimeout' Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889158 Share on other sites More sharing options...
sanchez77 Posted August 2, 2009 Author Share Posted August 2, 2009 that worked. i used it like so: mysql_query("UPDATE lockersmaster JOIN lockers ON lockersmaster.id=lockers.masterid SET lockersmaster.timeout = '$timeout', lockersmaster.dateout = '$dateout', lockersmaster.storetimeout = '$storetimeout'"); Cheers, Quote Link to comment https://forums.phpfreaks.com/topic/168548-solved-posting-data/#findComment-889172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.