my life, it's ****ed.

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by mywebradio12, Sep 22, 2011.

  1. mywebradio12

    mywebradio12 New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi all

    why does this piece of code not work :s
    <?php
    mysql_query("UPDATE `articles` SET likes = likes+'".$_SESSION['userid'].",' WHERE id = '".mysql_real_escape_string($_GET['id'])."'") or die('error liking an article lol sorry');
    ?>

    what it needs to do is simple:
    adding "<user id number>," to the existing table row "likes".

    nvm: with the comma to separate the id's.

    Thank you for your time in advance.

    Kind Regards,
    mywebradio12
     
    Last edited: Sep 22, 2011
  2. your_death_1994

    your_death_1994 New Member

    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    0
    Do you see any errors when you ran this code?

    and what is the term in bold? if its a variable shouldn't it be $likes or something.
    likes = likes+'".$_SESSION['userid'].",'

    Lol, im learning php, so yeah i don't know a lot. :(
     
    Last edited: Sep 22, 2011
  3. bdistler

    bdistler Well-Known Member Prime Account

    Messages:
    3,534
    Likes Received:
    196
    Trophy Points:
    63
    I ASSUME THIS IS A TEST PHP SCRIPT
    AND WILL NOT BE INCORPORATE INTO YOUR SITE
    UNTIL YOU HANDLE ERRORS AND TEST YOUR DATA
    AND RESULTS IN THE APPROPRIATE WAY

    I would do
    PHP:
    $tempID mysql_real_escape_string($_GET['id'])
    then use $tempID in the query

    I would make up the query in a var and use it like
    PHP:
    $query "UPDATE `articles` SET likes =....."
    $result mysql_query($query) or die.......
    then to get the error info replace - starting at die with
    PHP:
    ... die ("error liking an article lol sorry<br />\n MySQL error ==>" mysql_errno() . "<== : ==>" mysql_error() . "<== <br />\n\$query ==>" $query "<==<br />\n ");
    others will be here to tell you to use PDO
     
    • Like Like x 1
  4. resty_rizal99

    resty_rizal99 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    how about change your code like this
    PHP:
    // Get the current value of likes from articles with a match id of $_GET['id']
    if(!$result mysql_query('SELECT `likes` FROM `articles`  WHERE `id` = "'.mysql_real_escape_string($_GET['id']).'"')) die('Invalid query: ' mysql_error());
    $row mysql_fetch_row($result);
    $likes $row[0];

    // Update articles likes with the value of likes plus value of $_SESSION['userid']
    mysql_query("UPDATE `articles` SET `likes` = '".($likes+$_SESSION['userid'])."' WHERE `id` = '".mysql_real_escape_string($_GET['id'])."'") or die('error liking an article lol sorry');
     
  5. gomarc

    gomarc Member

    Messages:
    516
    Likes Received:
    18
    Trophy Points:
    18
    Hello mywebradio12,

    As long as the value of $_SESSION['userid'] is numeric, your query as posted works. Before the query, test this value to ensure its numeric.

    I also suspect that server temporal issues may have affected your results.

    Earlier today, I was having some connection issues with MySQL (my free account server is chopin) but all is working fine now.

    As pointed out by bdistler, you should definitively move away from the old mysql extension. Please read this post by essellar: http://x10hosting.com/forums/progra...ql-start-moving-your-development-pdo-now.html
     
  6. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    Additionally:
    • Use
      PHP:
      , [html] or [codetags (as appropriatewhen posting to separate and format code.

      [*][
      URL="http://www.phpfreaks.com/blog/or-die-must-die"]Don't use [c]die[/c][/URL] when outputting HTML. You'll get invalid HTML.

      [*]
      Columns in relational tables should have scalar (aka "simple" or "atomic"valuesnot compound values (aka "repeating groups"). A comma-separated list is a compound valueThis is a basic property of RDBs called "first normal form"Instead, use a table to represent the relationship:
      [
      code]CREATE TABLE likes (
          `
      articleINT UNSIGNED NOT NULL,
          `
      userINT UNSIGNED NOT NULL,
          
      UNIQUE KEY (`article`, `user`),
          
      FOREIGN KEY `articleREFERENCES `articles` (`id`),
          
      FOREIGN KEY `userREFERENCES `users` (`id`)
      Engine=InnoDB;
      [/
      code]

      To get the users that like an articlejoin the "articles" and "users" tables using the "likes" table.
      [/list]
     
    • Like Like x 1
  7. theone48

    theone48 New Member

    Messages:
    237
    Likes Received:
    7
    Trophy Points:
    0
    Yikes, this is a temperamental thread. What does death have to do with programming?

    Oh, maybe, 'die' has some alternative cyber meaning for programming function.
     
  8. lemon-tree

    lemon-tree x10 Minion Community Support

    Messages:
    1,420
    Likes Received:
    46
    Trophy Points:
    48
    The die() function causes the currently running script to end execution. If you don't like that terminology, you can use exit() instead. A quick search of the PHP manual would have told you precisely this.
     
    Last edited: Sep 28, 2011
  9. theone48

    theone48 New Member

    Messages:
    237
    Likes Received:
    7
    Trophy Points:
    0
    Thanks, thought it had something to do with programming. Sorry for the stupid question, but I don't even know what 'PHP' is. :(
     

Share This Page