Page 1 of 1

voting problem

Posted: Thu Dec 28, 2006 7:45 pm
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());
}
}

?>

Posted: Thu Dec 28, 2006 8:58 pm
by hawleyjr
Your syntax looks okay, what error are you getting?

Edit:

On second thought...look into mysql_num_rows()

Posted: Thu Dec 28, 2006 11:00 pm
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.

Posted: Fri Dec 29, 2006 4:58 am
by timvw
Imho, it would be a lot cleaner if you simply used INSERT ON DUPLICATE KEY.

Posted: Fri Dec 29, 2006 1:02 pm
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());

}

?>