salman_ahad@yahoo.com Posted October 28, 2009 Share Posted October 28, 2009 My "topics" table contains 10 entires *--------------* topicid topic ------ *--------------* 01 Bye 02 Hi 03 Hello ..... 10 Morning *--------------* <?php //topics.php require_once("includes/connection.php"); .... .... ttopics[]= //I have 10 strings here which have changed by now. foreach ($ttopics as $ttkey) { //I am trying to update my "topics" table whenever these strings change, based on the topicid (primary key). //Please correct my code. $id = mysql_real_escape_string('topicid'); $topic = mysql_real_escape_string('topic'); $result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$id}' and topic='{$topic}' "); if($result) { $num = mysql_num_rows( $result ); if ($num == 0) { $querypost = mysql_query ("INSERT INTO trendingtopics (topic) VALUES ('$ttkey')"); } } I am getting an error. Lets say the position of Morning is first and Bye is last. It should update the database accordingly. If there is a new entry, say "phpfreaks" in first position. It should update according. Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/ Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 Something like that.. foreach ($ttopics as $ttkey=>$topic){ $result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$ttkey}'"); if($result) { $num = mysql_num_rows( $result ); if ($num == 0) { $querypost = mysql_query ("INSERT INTO trendingtopics VALUES ('$ttkey', '$topic')"); } else{ $row = mysql_fetch_array($result); if($row['topic']!=$topic) $querypost = mysql_query ("UPDATE trendingtopics SET topic = '$topic' WHERE topicid='$ttkey'"); } Haven't test the code. You might want to add mysql_real_escape_string if you don't trust your hard-coded ttopics array. heh Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-945987 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 //I am still not able to update <?php ... foreach ($ttopics as $ttkey=>$topic){ ttkey has the topics (strings) $result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$ttkey}'"); Then why are checking for "topicid" nstead of topics if($result) { $num = mysql_num_rows( $result ); if ($num == 0) { $querypost = mysql_query ("INSERT INTO trendingtopics VALUES ('$ttkey', '$topic')"); } else{ $row = mysql_fetch_array($result); if($row['topic']!=$topic) $querypost = mysql_query ("UPDATE trendingtopics SET topic = '$topic' WHERE topicid='$ttkey'"); } <? current table hard coded entry topicid topic --------------------- 1 a 2 b .. .. 10 j Now we need to update with the strings present in $ttkey Please help Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946004 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 current table hard coded entry topicid ------topic --------------------- 1-------------a 2 -------------b .. .. 10 -------------j //I have to update this table with strings present in $ttkey <?php ... foreach ($ttopics as $ttkey=>$topic){ $ttkey has the topics (10 strings). These need to be updated instead of a,b ...j $result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$ttkey}'"); if($result) { $num = mysql_num_rows( $result ); if ($num == 0) { $querypost = mysql_query ("INSERT INTO trendingtopics VALUES ('$ttkey', '$topic')"); } else{ $row = mysql_fetch_array($result); if($row['topic']!=$topic) $querypost = mysql_query ("UPDATE trendingtopics SET topic = '$topic' WHERE topicid='$ttkey'"); } <? There is something wrong in my syntax. Please help Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946009 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 Ok, maybe I've assumed the structure of your $ttopics array wrongly. Is it declared something like this? $ttopics = array(); $ttopics[] = "topic1"; $ttopics[] = "topic2"; ... If so, $ttkey should hold the key in the array:0 to 9 within the foreach loop. $topic will hold the topic itself. "something wrong" isn't very helpful. What error do you get/what problem are you facing? Try "or die(mysql_error)" next to the mysql queries also Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946016 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 <?php require_once("includes/connection.php"); $json = file_get_contents('http://some_api'); Some api I am retrieving the names $data = json_decode($json); $ttopics = array(); foreach ($data->trends as $trend) { $ttopics[] = $trend->name ; those names I am storing in array } foreach ($ttopics as $ttkey=>$topic){ for each array entry as $ttkey $result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$ttkey}'"); if($result) { $num = mysql_num_rows( $result ); if ($num == 0) { $querypost = mysql_query ("INSERT INTO trendingtopics VALUES ('$ttkey', '$topic')"); } else{ $row = mysql_fetch_array($result); if($row['topic']!=$topic) $querypost = mysql_query ("UPDATE trendingtopics SET topic = '$topic' WHERE topic='$ttkey'"); } } } ?> the topics(ttkey) are not inserting. Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946026 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 seanlim: Please help....I am stuck here Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946042 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 Please describe both the tables you are using. As I've mentioned previously, place "or die (mysql_error());" after your mysql queries to help in debugging: mysql_query("...") or (die mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946063 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 You are correct ttopics[] = array () //it holds the strings $ttopics[] = "topic1"; $ttopics[] = "topic2"; .... $ttkey should hold the key in the array:0 to 9 How do we do it? Ok, maybe I've assumed the structure of your $ttopics array wrongly. Is it declared something like this? $ttopics = array(); $ttopics[] = "topic1"; $ttopics[] = "topic2"; ... If so, $ttkey should hold the key in the array:0 to 9 within the foreach loop. $topic will hold the topic itself. "something wrong" isn't very helpful. What error do you get/what problem are you facing? Try "or die(mysql_error)" next to the mysql queries also Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946068 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 When I say "tables", I mean database table, MySQL tables etc. I believe an array would fall under the more generic term of "data structure". Again, what I'm asking for is the structure of `table` and `trendingtopics`. Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946071 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 Again, what I'm asking for is the structure of `table` and `trendingtopics`. table name = trendingtopics col1 = topicid //I assigned the id 1 to 10 (hard coded) col2 = topic //These 10 topics should be updated which are present in $ttopics[] P.S: We are not getting topicid from that api. Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946077 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 So why are you using the code "SELECT topicid, topic FROM table..."? does a table named `table` exist? Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946087 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 Seanlim: you are best I regularly have concerns...will look forward for your help in future!! Quote Link to comment https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/#findComment-946091 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.