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

mywebradio12

New Member
Messages
1
Reaction score
0
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:

your_death_1994

New Member
Messages
21
Reaction score
1
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:

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
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
 

resty_rizal99

New Member
Messages
2
Reaction score
0
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');
 

gomarc

Member
Messages
516
Reaction score
18
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
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Additionally:
  • Use
    PHP:
    , [html] or [code] tags (as appropriate) when 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") values, not compound values (aka "repeating groups"). A comma-separated list is a compound value. This is a basic property of RDBs called "first normal form". Instead, use a table to represent the relationship:
    [code]CREATE TABLE likes (
        `article` INT UNSIGNED NOT NULL,
        `user` INT UNSIGNED NOT NULL,
        UNIQUE KEY (`article`, `user`),
        FOREIGN KEY `article` REFERENCES `articles` (`id`),
        FOREIGN KEY `user` REFERENCES `users` (`id`)
    ) Engine=InnoDB;
    [/code]
    
    To get the users that like an article, join the "articles" and "users" tables using the "likes" table.
    [/list]
 

theone48

New Member
Messages
237
Reaction score
7
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.
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
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.
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:

theone48

New Member
Messages
237
Reaction score
7
Points
0
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.

Thanks, thought it had something to do with programming. Sorry for the stupid question, but I don't even know what 'PHP' is. :(
 
Top