Jump to content

[SOLVED] db entry based on primary key


salman_ahad@yahoo.com

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/179282-solved-db-entry-based-on-primary-key/
Share on other sites

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

//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

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

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

<?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.

 

 

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

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.