0

Ok basically I need to select the submitted_link row in image 1 based on the tab_id from image 1 and id from image 2 (the join). After this is selected I need to update the tab_content row in image 2 for each of the id's in image 2. Below the images is what I have code wise right now.

Image 1:

Database Help http://bekustom.com/Untitled-1.jpg

Image 2:

Database Help http://bekustom.com/Untitled-2.jpg

My Code:

$sql = ("SELECT submitted_links.submitted_link, submitted_links.tab_id, users_tabs.id FROM users_tabs, submitted_links WHERE submitted_links.tab_id=users_tabs.id AND submitted_links.user_id=users_tabs.user_id");

$result = mysqli_query($db, $sql);
while($row = $result->fetch_array()) {
$rows[] = $row;
}

foreach($rows as $row) {
$tab_content[] = $row['submitted_link'];
$tab_id = $row['tab_id'];

$tab_implode = implode(",", $tab_content);
$sql2 = ("UPDATE users_tabs SET tab_content='".$tab_implode."' WHERE id='".$tab_id."'");
$result2 = $db->query($sql2);
}

What I'm getting in tab_content currently:

ID 17: http://google.com,http://twitter.com,http://google2.com
ID 18: http://google.com,http://twitter.com

Desired data in tab_content:

ID 17: http://google.com,http://google2.com
ID 18: http://twitter.com
5
  • what your are getting? update is not working?
    – Kumar V
    Jan 5, 2014 at 13:39
  • Can be more clear regarding the final state of database that you want?
    – Manu
    Jan 5, 2014 at 13:40
  • why can't move your update query inside the while loop?
    – Kumar V
    Jan 5, 2014 at 13:43
  • The update works but does not give me the desired data into the tab_content column for each id in image 2. The tab_content column should read http://google.com,http://google2.com for id 17, and http://twitter.com for id 18 in image 2.
    – BeKustom
    Jan 5, 2014 at 13:44
  • Updated question to show current results in tab_content vs desired results in tab_content.
    – BeKustom
    Jan 5, 2014 at 13:47

2 Answers 2

1

You need to change your PHP code!

Try this...

$sql = "SELECT 
submitted_links.submitted_link, 
submitted_links.tab_id, 
users_tabs.id, 
GROUP_CONCAT( submitted_link ) AS all_links 
FROM 
users_tabs, submitted_links 
WHERE 
submitted_links.tab_id = users_tabs.id 
AND submitted_links.user_id=users_tabs.user_id 
GROUP BY submitted_links.tab_id";

$result = mysqli_query($db, $sql);
while($row = $result->fetch_array()) 
{
    $sql2 = "UPDATE 
             users_tabs 
             SET 
             tab_content='" . $row['all_links'] . "' 
             WHERE id='" . $row["tab_id"] . "'";

    $result2 = $db->query($sql2);
}
8
  • No errors being reported but it's not updating the tab_content column.
    – BeKustom
    Jan 5, 2014 at 14:26
  • It looks correct to me logically... try to debug and see where the problem is!
    – Manu
    Jan 5, 2014 at 14:30
  • Check what is the output of the first query.. After that check echo $sql2 in the loop!!
    – Manu
    Jan 5, 2014 at 14:32
  • This worked after I tracked down the issue which was just a typo: Was: GROUP_CONCAT( submitted_links ) AS all_links Should be: GROUP_CONCAT( submitted_link ) AS all_links
    – BeKustom
    Jan 5, 2014 at 14:38
  • Ohh!! Ok.. I have updated the answer to reflect the same.! :)
    – Manu
    Jan 5, 2014 at 14:41
0

Try this query in your mysql environment. If it is worked, then no need of while , foreach loops

UPDATE users_tabs  AS c   
    join (SELECT o.tab_id, GROUP_CONCAT(o.submitted_links SEPARATOR ',' ) as  'links'  
     FROM submitted_links AS o  GROUP BY o.tab_id) as mysub_tab
    on c.id= mysub_tab.tab_id
      SET c.tab_content = mysub_tab.links
8
  • MySQL gave this error: You can't specify target table 'c' for update in FROM clause
    – BeKustom
    Jan 5, 2014 at 13:55
  • This look like oracle syntax to update select. Try inner join as shown on this question: stackoverflow.com/questions/11247982/… Jan 5, 2014 at 14:00
  • @kumar_v also generated an error: #1064 - 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 '(SELECT o.tab_id, GROUP_CONCAT(DISTINCT o.submitted_links SEPARATOR ',' ) as 'li' at line 2
    – BeKustom
    Jan 5, 2014 at 14:09
  • @BeKustom Updated my answer again. I couldn't test due to some problem in my system.
    – Kumar V
    Jan 5, 2014 at 14:14
  • @kumar_v Another error: #1054 - Unknown column 'o.tab_id' in 'field list'
    – BeKustom
    Jan 5, 2014 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.