drsxobip501258
2013-12-06 13:31
浏览 29
已采纳

信息未在数据库中更新

once again it is me. I'll promise after this one I am done bothering you for a while!:)

I have this rating system, which would allow user to rate an article. It is somewhat working, but the thing is that it doesn't update the data in the database and I have no idea why. Any help would be appreciated. :)

// Connects to your Database 
 mysql_connect("URL", "username", "password") or die(mysql_error()); 
 mysql_select_db("db_name") or die(mysql_error()); 


     //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating  
 if(isset($_submit['voted'])) { 
 mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id"); 

        Echo "Your vote has been cast <p>"; 
        } 



 //Puts SQL Data into an array
 $data = mysql_query("SELECT * FROM vote") or die(mysql_error()); 

 //Now we loop through all the data 
 while($ratings = mysql_fetch_array( $data )) 
 { 
 //This outputs the sites name 
 Echo "Name: " .$ratings['name']."<br>"; 

 //This calculates the sites ranking and then outputs it - rounded to 1 decimal 
 $current = $ratings['total'] / $ratings['votes']; 
 Echo "Current Rating: " . round($current, 1) . "<br>"; 




 //This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item 
 Echo "Rank Me: "; 
 Echo "<a href='index.php?site=kumu'?mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
 Echo "<a href='index.php?site=kumu'?mode=vote&voted=2&id=".$ratings['id'].">2</a> | "; 
 Echo "<a href='index.php?site=kumu'?mode=vote&voted=3&id=".$ratings['id'].">3</a> | "; 
 Echo "<a href='index.php?site=kumu'?mode=vote&voted=4&id=".$ratings['id'].">4</a> | "; 
 Echo "<a href='index.php?site=kumu'?mode=vote&voted=5&id=".$ratings['id'].">5</a><p>"; 
 } 


 ?>

Thanks guys!: )

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doutou7549 2013-12-06 13:38
    已采纳

    Relace this line

    if(isset($_submit['voted'])) { 
     mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id"); 
    
           echo "Your vote has been cast <p>"; 
            } 
    

    With this

    if(isset($_POST['voted'])) { 
     mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id"); 
    
            echo "Your vote has been cast <p>"; 
            } 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqgo99177 2013-12-06 13:38

    there is No $_SUBMIT superglobal variable in php

    use $_POST or $_GET

    Read here

    if you still have problem check for errors using mysql_error()

    mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id") 
    or  die(mysql_error()); 
    

    or echo your query and run it in your local database.

    and then share your error with us.

    Dont use mysql_ as they are depracated.*

    评论
    解决 无用
    打赏 举报
  • doushan1850 2013-12-06 13:42

    you should use $_GET

         //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating  
     if(isset($_GET['voted'])) { 
     mysql_query ("UPDATE vote SET total= total+$_GET['voted'], votes = votes+1 WHERE id = $_GET['id']"); 
    
            Echo "Your vote has been cast <p>"; 
            } 
    
    评论
    解决 无用
    打赏 举报
  • douzhi6365 2013-12-06 13:46

    First: use $_GET['voted'] instead of $_submit['voted'].

    In addition the links you built are wrong. Change

     Echo "<a href='index.php?site=kumu'?mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
     Echo "<a href='index.php?site=kumu'?mode=vote&voted=2&id=".$ratings['id'].">2</a> | "; 
     Echo "<a href='index.php?site=kumu'?mode=vote&voted=3&id=".$ratings['id'].">3</a> | "; 
     Echo "<a href='index.php?site=kumu'?mode=vote&voted=4&id=".$ratings['id'].">4</a> | "; 
     Echo "<a href='index.php?site=kumu'?mode=vote&voted=5&id=".$ratings['id'].">5</a><p>"; 
    

    to

     Echo "<a href=index.php?site=kumu&mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
     Echo "<a href=index.php?site=kumu&mode=vote&voted=2&id=".$ratings['id'].">2</a> | "; 
     Echo "<a href=index.php?site=kumu&mode=vote&voted=3&id=".$ratings['id'].">3</a> | "; 
     Echo "<a href=index.php?site=kumu&mode=vote&voted=4&id=".$ratings['id'].">4</a> | "; 
     Echo "<a href=index.php?site=kumu&mode=vote&voted=5&id=".$ratings['id'].">5</a><p>"; 
    

    UPDATE

    Replace the statement:

     if(isset($_submit['voted'])) { 
     mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id"); 
    
            Echo "Your vote has been cast <p>"; 
            } 
    

    With:

     if(isset($_GET['voted']) && is_numeric($_GET['voted'])) { 
     mysql_query ("UPDATE vote SET total= total+ " . $_GET['voted'] . ", votes = votes+1 WHERE id = " . $_GET['id']); 
    
            Echo "Your vote has been cast <p>"; 
            } 
    

    Of course, a better parameter validation and escaping is mandatory in order to prevent any SQLInjection.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题