voting problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

voting problem

Post by corillo181 »

this code work find to update a record that already exist, but the insert query doesn't work at all..

Code: Select all

<?php
$a_t_id=$artist['artist_id'];

$getcounter=mysql_query("SELECT counter FROM tra_music_top10 WHERE artist_id='$a_t_id'")or die(mysql_error());

$count=mysql_fetch_array($getcounter);

if(!$count['counter']){
echo 0;
}else{
$i=$count['counter'];
echo $i;
}

if($_POST['Submit']){
$voted=($i+1);

$update=mysql_query("UPDATE tra_music_top10 SET counter='$voted' WHERE artist_id='$a_t_id'")or die(mysql_error());

if(!$update){
$newrecord=mysql_query("INSERT into tra_music_top10(artist_id,counter)VALUES('$a_t_id','1')")or die(mysql_error());
}
}

?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Your syntax looks okay, what error are you getting?

Edit:

On second thought...look into mysql_num_rows()
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i got it to wokr by putting a 0 when a artist name is included, so every artist got a vote of 0 so when someone vote for the artist it adds 0+1=1 wich works fine. thankx for the help.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Imho, it would be a lot cleaner if you simply used INSERT ON DUPLICATE KEY.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i tried this but now everythings update at the same time..

Code: Select all

<?php
$a_t_id=$artist['artist_id'];

$getcounter=mysql_query("SELECT counter FROM tra_music_top10 WHERE artist_id='$a_t_id'")or die(mysql_error());

$count=mysql_fetch_array($getcounter);

$i=$count['counter'];
echo $i;

if($_GET['vote']){
$voted=($i+1);

$update=mysql_query("UPDATE tra_music_top10 SET counter='$voted' WHERE artist_id='$a_t_id'")or die(mysql_error());

}

?>
Post Reply