Jump to content

[SOLVED] db entry based on primary key


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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.