Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    db entry based on primary key

    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.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $id mysql_real_escape_string('topicid');
    $topic mysql_real_escape_string('topic'); 
    What is the point of using string constants here? I think you mean to use variables, not constants.

    Also make sure you check each and every one of your calls to mysql_query() to make sure the query worked and if it didn't, display error details!

    p.s. Please wrap your code in [ php] tags when posting here; it's much easier to read the code.

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 //also help me with mysql_real_escape_string
    ...
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •