Jump to content

Problem with UPDATE a field in the mySQL table

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Hi all, I'm back with another question.....so sometimes I think: "Am I a big '?' ??" :lol:

I'm trying to update a field on my database and it works good, but not like the way I want.
$already_added_query = "UPDATE MyTable SET id_already_voted = $current_user->ID  WHERE id='$id_news' ";
//            echo $already_added_query;
$already_added_exec = mysql_query($already_added_query);
I want to add to my database field the userID of the people that voted to the poll, just because I have problem setting cookie :thumbdown:
Like I said it works, but it adds only one userID as below:

Quote

State 0 (no vote)
id_already_voted
0

State 1 (1 vote from userID=10)
id_already_voted
10

State 2 (another vote from userID=3)
id_already_voted
3
Instead I want something like this:
State 0 (no vote)

Quote

id_already_voted
0

State 1 (1 vote from userID=10)
id_already_voted
10

State 2 (another vote from userID=3)
id_already_voted
10,3
Any hint please?

#2
Programnnd

Programnnd

    Newbie

  • Members
  • Pip
  • 4 posts
Try the following code:

<?php

mysql_query("UPDATE `MyTable` SET `id_already_voted` = '".$current_user->ID."' WHERE id = '".$id_news."'")

or die(mysql_error());

?>



#3
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
mmmmm the code is the same of mine (I think) and in fact it works in the same way. :crying:

#4
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
I see what the issue is.
The second query is exactly the same as the first with the exception of the surrounding single quotes, which is considered good practice but I do not like to use a lot of concatenation... I prefer this approach
$already_added_query = "UPDATE MyTable SET id_already_voted = '{$current_user->ID}'  WHERE id='{$id_news}'";
This isn't going to fix the issue, it's just my two cents about the single query.

The problem here is that you want to append the id in a single row but your query is designed to just overwrite the data with the new ID. You have two ways to solve this issue.
  • Update the query to append to the value instead of overwrite it
  • Modify the code to work with a more efficient check

I can't recall exactly off the top of my head how to append a string value; for an integer its something like
id_already_voted = id_already_voted+1
to increment a value by 1. Maybe this will push you in the right direction.

My opinion would be to use method 2. Modify the code and the table, if you need, to allow for multiple rows. This way you could run a quick query on the table for the specific item id and return a result of all user IDs that were added to the table. This is exactly the same thing you are trying to do but a little different. The reason I feel this is better is performance, it is faster to query the item id and return an array of user IDs instead of having to search the single row for an id. I could be wrong but this is my understanding. Instead of a update you would just add a new row. Should be much easier to write and work with.
"Life would be so much easier if we only had the source code."

#5
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts

SoN9ne said:

I see what the issue is.
The second query is exactly the same as the first with the exception of the surrounding single quotes
So I was right.

By the way thank you for the hints, I'm trying to modify my code, just because I had an idea after this reading ;)

#6
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
I thought to use something like this:
$already_added_query = "UPDATE MyTable SET id_already_voted = CONCAT(id_already_voted, $current_user->ID)  WHERE id='$id_news' ";
//            echo $already_added_query;
$already_added_exec = mysql_query($already_added_query);  
It works but write this:
351027
Instead of 3 5 10 27

P.S.: little add, the field is id_already_voted INT(9)

#7
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Try this:
$already_added_query = "UPDATE MyTable SET id_already_voted = CONCAT(id_already_voted, ' ', '{$current_user->ID}')  WHERE id='{$id_news}'";

This won't work if the field is an INT, it must be a varchar or equivalent. Hence the reason I suggested method 2 since it won't cripple the app with limitations like this method will.
"Life would be so much easier if we only had the source code."

#8
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Wonderful this way works.
For the moment I'll leave it as it is now, but I'll modify all asap.

Thank you very much.
A beer for you :)