-1

我正在尝试创建一个传出链接点击计数器。在网上找到了一些代码,我一生都无法理解为什么它不更新数据库中的数字。我在这里做错了什么?

<?php
    $linkid = $_GET["id"];

    mysql_query("UPDATE research SET out = out + 1 WHERE id='$linkid'");

    $query = "SELECT * FROM research WHERE id='$linkid'";
    $result = mysql_query( $query ) or die ("Error in query: $query. ".mysql_error());
    while($row = mysql_fetch_row( $result ) ) {
        header ("Location:" .$row[2] ); 
    }
?>
4

1 回答 1

0

了解此代码如何工作的最佳选择是学习检查数据,如下所示:

//connect to db here before the rest of your code

if(isset($_GET["id"]){ //only execute if GET is set
   $linkid = $_GET["id"];
   echo 'GET = '.$linkid.' <br/>'; //check the value to check against your database for testing

   mysql_query("UPDATE research SET out= out+1 WHERE id='$linkid'") or die(mysql_error()); 
   //or die helps detect syntax mistakes

if(mysql_affected_rows()){ //if update did occur

 $query = "SELECT fieldname FROM research WHERE id='$linkid'"; 
 //no need to use * just select the on fields you need!

 $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 if(mysql_num_rows($result)>0){ //if a row is found with that id
  $row = mysql_fetch_assoc();
  echo 'Field Value = '.$row['fieldname'];
  //header("location:".$row['fieldname']); - temporarily commented out as headers already sent
 } else { echo 'id does not exist in research table'; }

} else { echo 'update did not occur'; }

} else { echo 'GET not set!'; }

我认为您的语法没有任何问题,但一些检查器可以帮助解释为什么它可能不起作用!

使用我的脚本检查输出并将其与您的数据库进行比较(请务必将字段名检查为字段的实际名称!

它应该强调为什么它不起作用。我添加了注释来解释发生了什么,以包含您对我使用的一些函数名称不熟悉的情况。

于 2013-08-09T00:13:40.077 回答